Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Formatting date from MM/DD/YYYY.

    I'm trying to format my date but I keep getting a type mismatch error. My dates are labeled as "MM/DD/YYYY".

    What I did was type
    Code:
    gen date2 = date(date1, "MDY")
    How do I get it to recognize my date and solve this mismatch error?

    EDIT: I went ahead and formatted in Excel as mmm dd, yyyy, but I still want to know how to get STATA to recognize it from MM/DD/YYYY

    Sorry, I'm a newbie.
    Last edited by Jake Siu; 06 Dec 2018, 09:00.

  • #2
    sounds as though date1 already is numeric; if you had, as the FAQ calls for, supplied a -dataex- example we would know

    you might also want to show us the result, in CODE blocks (see the FAQ) of
    Code:
    describe date1

    Comment


    • #3
      If "labeled" means "has a value label", then indeed that won't work.

      The help for date() starts

      Code:
       date(s1,s2[,Y])
             Description:  the e_d date (days since 01jan1960) corresponding to s1
                           based on s2 and Y
      
                           s1 contains the date, recorded as a string, in virtually
                           any format.
      The first argument must be a string.

      This dialogue shows the principle:

      Code:
      . clear
      
      . set obs 1
      number of observations (_N) was 0, now 1
      
      . gen whatever = 1
      
      . label def whatever 1 "12/06/2018"
      
      . label val whatever whatever
      
      . gen ddate = daily(whatever, "MDY")
      type mismatch
      r(109);
      
      . decode whatever, gen(wanted)
      
      . l
      
           +-------------------------+
           |   whatever       wanted |
           |-------------------------|
        1. | 12/06/2018   12/06/2018 |
           +-------------------------+
      
      . gen ddate = daily(wanted, "MDY")
      
      . format ddate %td
      
      . l
      
           +-------------------------------------+
           |   whatever       wanted       ddate |
           |-------------------------------------|
        1. | 12/06/2018   12/06/2018   06dec2018 |
           +-------------------------------------+
      Note: I strongly recommend you use daily() not date(). It's the same function, really, so why the recommendation? Using daily() helps to flag to yourself and to anyone else reading your code, what it does, create a daily date, not any kind of date.

      Comment


      • #4
        Thanks for the help guys. I'm sorry it was a dumb question.

        Comment

        Working...
        X