Announcement

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

  • Separating date and time

    Hi All: I'm having difficulty separating the date and time from each other. I tried yearly(), date (), and year(dofm()) with no success. Any clue how I get just the year as its own variable?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double ct_datetime_num
    1.8099198e+12
    1.8009297e+12
    1800772320000
    1.7995776e+12
    1798796940000
    end
    format %tc ct_datetime_num


  • #2
    Code:
    gen int date = dofc(ct_datetime_num)
    format date %td
    gen int year = year(date)
    Or, if you don't actually need the whole date, just the year, you can do it in one line as:
    Code:
    gen int year = year(dofc(ct_datetime_num))
    Added: by the way, yearly(), date (), and year(dofm()) are functional expressions that calculate a numeric year from a string variable containg only a year, a Stata internal format numeric daily date variable from a string containing a human-readable date, and the year extracted from a Stata internal format monthly date variable, respectively.
    Last edited by Clyde Schechter; 06 Jan 2023, 16:15.

    Comment


    • #3
      Thank you Clyde!

      Comment

      Working...
      X