Announcement

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

  • Splitting a date variable into two

    Hi. I have the current date variable that records the date and time of the doctor consultation within a single variable. The data is imported from excel where the format was "1/1/2022 12:00:00 AM". I'd like to split it into this variable into two variables, one for date and one for time. Any help would be great. So, basically date==1/1/2022 and time==12:00:00 AM


    Code:
    input double appt_date
    
    1.9566144e+12
    1.9566144e+12
    1.9566144e+12
    1.9566144e+12
    1.9566144e+12
    1.9566144e+12
    1.9566144e+12
    1.9566144e+12
    1.9567872e+12
    1.9567872e+12
    1.9567872e+12
    1.9567872e+12
    1.9567872e+12
    1.9567872e+12
    1.9567872e+12
    1.9567872e+12

  • #2
    This should do the job. Taken from: https://www.statalist.org/forums/for...datae-and-time

    Code:
    gen day = day(dofc(appt_date))
    gen month = month(dofc(appt_date))
    gen year = year(dofc(appt_date)) 
    gen double time = mod(appt_date, 24 * 60 * 60000)
    gen date = mdy(month, day, year)
    format date %td
    format time %tcHH:MM:SS

    Comment


    • #3
      #2 is helpful but can be slimmed down:

      Code:
      gen date = dofc(appt_date)
      gen double time = mod(appt_date, 24 * 60 * 60000)
      format date %td
      format time %tcHH:MM:SS

      Comment


      • #4
        Thank you, Luca Calianno and Nick Cox. Both those worked perfectly!

        Comment

        Working...
        X