Announcement

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

  • Extracting months from dates with atypical format (%tcdd-Mon-CCYY_HH:MM)

    Dear Stata users,

    I have an elementary problem that I somehow fail to solve. It's already been a few days of struggle.

    I have patient-level data with hospital admission dates. For example,
    12-Nov-2019 17:34

    Stata acknowledges the date as shown by this in "Format" of the variable
    %tcdd-Mon-CCYY_HH:MM

    I want to include months fixed effects into my model to see what will happen. Thus I need to extra month from these dates. When I use the usual command, such as
    gen monthly_date = mofd(hospAdmTime)
    I get dots.


    Here is 0.1 of my date variable.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double hospAdmTime
        1898421420000
        1896442080000
        1.8888276e+12
    1891769819999.998
    1890307319999.998
    1891146300000.002
        1901499480000
    1903475999999.998
    1888123560000.002
    1890399300000.002
        1908565440000
    1906909800000.002
        1890471660000
        1.8888516e+12
        1896013860000
    1899059699999.998
    1892028899999.998
        1905972780000
        1902095280000
    1889199299999.998
    1883128560000.002
        1893848940000
    1898796300000.002
        1901622540000
    1904807099999.998
        1.8903219e+12
    1889264099999.998
    1900870499999.998
        1.8857934e+12
        1887447660000
        1884433440000
        1900547760000
    1903526459999.998
        1.8893646e+12
        1.8953304e+12
        1891432440000
    end
    format %tcdd-Mon-CCYY_HH:MM hospAdmTime
    Kind regards,
    Sergey Alexeev | ​The University of Sydney
    https://alexeev.pw/

  • #2
    At least two ways of doing it, pick the one you find more useful:

    Code:
    gen mm = clockpart(hospAdmTime,"month")
    gen yy = clockpart(hospAdmTime,"year")
    
    gen mmyy = mofd(dofc(hospAdmTime))
    format %tm mmyy

    Comment


    • #3
      Thank you, Hemanshu Kumar,

      I can confirm that your code performs as expected.
      Kind regards,
      Sergey Alexeev | ​The University of Sydney
      https://alexeev.pw/

      Comment

      Working...
      X