Announcement

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

  • Help with a basic loop

    Greetings Statlist users,

    I could use some assistance with a seemingly simple loop.

    As the merged datasets that I'm working with are panel, some values may be repeats. In this case I am essentially trying to loop through each unique patient (designated by "PAT_ENC_CSN") and tally the total number of days spent in 8D (designated by "days8D"). However, I also need to consider unique stays/time spent within 8D as some patients come and go through the unit during their overall stays (designated "stay").

    So, essentially need a loop that totals days8D per unique "PAT_ENC_CSN" and unique "stay".

    For the example patient below, total days in 8d would equal 15.
    PAT_ENC_CSN adm_date dc_date days8D stay
    2.51E+08 31-Jan-19 31-Jan-19 1 1
    2.51E+08 8-Feb-19 18-Feb-19 11 2
    2.51E+08 23-Feb-19 25-Feb-19 3 3
    2.51E+08 23-Feb-19 25-Feb-19 3 3
    2.51E+08 23-Feb-19 25-Feb-19 3 3
    2.51E+08 23-Feb-19 25-Feb-19 3 3
    2.51E+08 23-Feb-19 25-Feb-19 3 3
    2.51E+08 23-Feb-19 25-Feb-19 3 3
    Thanks,
    Todd

  • #2
    I'm pretty sure a loop is not necessary. If I understand you correctly this will do it:
    Code:
    egen stay_distinct = tag(PAT_ENC_CSN stay)
    egen wanted = total(days8D * stay_distinct), by(PAT_ENC_CSN)

    Comment


    • #3
      Excellent. Thanks Wouter. I can then get the desired total sum by this: total wanted, over (stay_distinct) and taking the value where stay_distinct==1

      Cheers!
      Todd

      Comment

      Working...
      X