Announcement

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

  • Substracting a date variable from another to find out duration of episode

    Hi! I'm working with a database that has two date variables in the following format:
    Year (4 positions), month (2 positions) and day (2 positions).

    So if a contract began on December 31 of 1985, the number in that variable would be: 19851231 (YYYYMMDD).

    Now, those two date variables pinpoint the moment an episode began and when it ended. I want to find out the duration of the episode, so I would like to subtract one from the other. I'm kind of lost, since making a simple substraction is not possible, as it's not a natural number or anything, but rather a sort of ordinal date.

    Thank you so much for your help!

  • #2
    use -datetime- to get them into the correct Stata internal date format; if the variable is currently numeric, use -tostring- before:
    Code:
    gen bdate=daily(date1,"YMD")
    gen edate=daily(date2,"YMD")
    if you want them formatted so they look like dates to humans:
    Code:
    format bdate edate %td
    or whatever format you would like

    remember to replace bdate and edate with names you want
    remember to replace date1 and date2 with the actual variable names

    these are now usable to get, e.g., duration of an episode by simple subtraction
    see
    Code:
    h datetime
    h format
    note that things I guessed at above would have been obvious if you, as per the request in the FAQ, had provided a -dataex- example

    Comment

    Working...
    X