Announcement

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

  • convert %10.0g to %td stata

    I have a long varialable in format %10.0g, which in an integer showing monthdayyear, ie it could be 61289, or 221189
    I need to convert this to a %td ie daily date, so I can work out an age using two dates
    I am struggling to work out the right code to do this, and any help would be really appreciated?

  • #2
    Just to add to this, I have tried the following code:

    tostring mydob, gen(datevar)
    gen date2 = date(datevar, "MDY")

    and unfortunately date2 just comes up with missing values

    Comment


    • #3

      Stata dates are SIF values, so you have to find a function that does the conversion.

      Code:
      help datetime
      Code:
      clear
      input float date
      61289
      221189
      160223
      end
      
      gen wanted= daily(string(date, "%06.0f"), "DMY", 2023)
      format wanted %td
      Res.:

      Code:
      . l
      
           +--------------------+
           |   date      wanted |
           |--------------------|
        1. |  61289   06dec1989 |
        2. | 221189   22nov1989 |
        3. | 160223   16feb2023 |
           +--------------------+

      Comment


      • #4
        Thanks so much for your help, this is still coming up with missing values. Anything else you would recommend?

        Comment


        • #5
          Showing an example using dataex.

          Comment

          Working...
          X