Announcement

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

  • Generate new date variable by ID for panel data

    Hello,
    I am trying to identify specific sets of consecutive dates by individual over the course of several years. I can calculate the length of stay between two dates, but I would like to generate (and add new rows/observations) for the days in between date1 and date2.

    For example:

    ID START END DURATION
    1 2013-11-27 2013-11-29 3


    I would like to have an expanded dataset that can list specific days, from var START:

    ID START END DURATION
    1 2013-11-27 2013-11-29 3
    1 2013-11-28 2013-11-29
    1 2013-11-29 2013-11-29


    The expand function will only give me:

    ID START END DURATION
    1 2013-11-27 2013-11-29 3
    1 2013-11-27 2013-11-29 3
    1 2013-11-27 2013-11-29 3


    The goal is to identify days, consecutive "in-hospital" days - versus - single-visit days. My thinking is that if I can identify each unique "in-hospital" day, then I can categorise and calculate them appropriately.

    Any suggestions or alternative approaches would be greatly appreciated!
    Thank you.

  • #2
    Google dm0068 Stata Journal

    Comment


    • #3
      Assuming that your date variables are true Stata internal format date variables, not just string variables that appear as dates to human eyes, you just have to add one last step to what you've already done:
      Code:
      by ID START, sort: replace START = START[1] + _n - 1 if _n > 1

      Comment


      • #4
        You should provide a data example using the dataex command as the specific code details will depend on whether your date variable is a proper date variable and whether the variable "duration" is numeric. Assuming that these are the case:

        Code:
        expand DURATION, g(new)
        bys ID START (new): replace START= START+_n-1 if new
        Note: Crossed with #2 and #3.
        Last edited by Andrew Musau; 19 Dec 2022, 15:26.

        Comment


        • #5
          Incredible! Thank you! Worked perfectly.

          Comment

          Working...
          X