Announcement

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

  • Extracting subset date from a timestamp date variable

    Dear Colleagues

    Please help: I am trying to extract the first 8 characters (NEWdate) from a date format (OLDdate) which has timestamp in it.
    OLDdate's format is %tc

    OLDdate
    14jan2011 00:00:00


    NEWdate
    14jan2011

    I have tried the two commands below and they dont seem to work:

    gen NEWdate = substr(OLDdate, 1, 9))
    format NEWdate %td


    gen NEWdate = dt(OLDdate_Date, 1, 9)
    format NEWdate %td


    Thanks for your help.

    Regards

    Fadzai

  • #2
    If OLDdate is formatted %tc it's already a Stata datetime variable, which is in fact numeric rather than a string variable, which is why you cannot use string functions like substr(). A datetime variable contains the number of milliseconds since 1 January 1960.

    Stata internal form (SIF) date/time variables have their own functions. To extract the date from a datetime variable you can use the dofc() function:
    Code:
    gen new_date = dofc(OLDdate)
    I highly recommend to have a look at help datetime if you're planning on working more with date/time variables.

    Comment


    • #3
      This question was also asked, and answered with an example, at

      https://www.statalist.org/forums/for...-date-variable

      Comment

      Working...
      X