Announcement

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

  • converting a number to a date

    Dear statalist,

    I need to convert numbers to dates (format: 1/1/2019). The numbers are a string variable, and are in the following format (mdy):

    datenumber
    12232019
    12202019
    12192019
    12192019
    12192019
    12182019
    12182019
    12162019
    10122019
    05122019
    05122019
    03122019
    11252019
    11182019
    11182019
    11152019
    06112019
    ...

    Notice that the months and days always exist of two digits (so sometimes it starts with a zero).
    I used the code:

    gen date= date(datenumber, "MDY")

    But only missing values were generated. I would be very thankful if you can provide me with guidance or code to solve this issue. Eventually, I want the dates to be in the format: m/d/y: so 1/1/2019. Thank you very much in advance.

    Floris Bedkom

  • #2
    the user-written command -numdate- makes this fairly easy:
    Code:
    numdate daily date2=datenumber, pattern("MDY") format(%td)
    obviously you can change the name of the new variable and/or its format

    to install -numdate-, use -search- or -findit- and follow the instructions

    Comment


    • #3
      Thank you very much, this is exactly right.

      Comment

      Working...
      X