Announcement

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

  • how to extract the year from this date format

    Dear all, I would like to ask for your assistance for extracting the year from the following date format 03jul2007 00:00:00 (see dataex below). I have tried using
    Code:
    gen yr1 = year(date)
    gen yr2 = yofd(dofm(date))
    
    
    generate double time=Clock(date,"DMY hms")
    format time %tC
    
    generate double newdate=date(date,"DMY hms")
    format date %td
    but they give all missing, or error "type mismatch". Any hint on how to do it?
    Thanks for the help.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double date
      1.49904e+12
    1.5639264e+12
    1.7614368e+12
    1.7614368e+12
    end
    format %tc date

  • #2
    Code:
    gen year = yofd(dofc(date))

    Comment


    • #3
      Justin Blasongame gave a great answer, but what's left is why the answers in #1 are wrong.

      Code:
       
       gen yr1 = year(date)
      year() pulls years out of daily dates, but date is not a daily date, but a clock date-time.
      Code:
      gen yr2 = yofd(dofm(date))
      dofm() pulls daily dates out of monthly dates, but date is not a monthly date.
      Code:
      generate double time=Clock(date,"DMY hms") format time %tC
      Clock() pulls a clock date-time variable out of a string, but date already is such a variable, and the types mismatch, which is what Stata notices.
      Code:
      generate double newdate=date(date,"DMY hms") format date %td
      date() pulls a daily date out of a string, but date already is numeric, and the types mismatch, which is what Stata notices.

      Comment


      • #4
        Dear Justin and Nick, thanks a lot for your help. It worked perfectly, and thanks for the explanation, it is quite helpful.

        Comment

        Working...
        X