Announcement

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

  • LOS summing

    Greetings Statalist users. I need to sum "days8dtotal" per patient across unique "stay". Based on descriptive merged data, many patients will have many rows of data with identical data. The following is a cut of these 2 variables for one patient. Stay = 1 has 21 rows of data and Stay=2 only 1 row. I really just need an effective way to sum total days for each patient across stays. So, for this particular patient, 2 distinct stays within 8D for a total of 13 days in 8D. Thanks.



    days8dtotal stay

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    8 1

    5 2

  • #2
    Well, the simplest way would be to remove the duplicate observations and then calculate your sum. But on the assumption that there is some good reason you need to retain the duplicate obesrvations, you can also do it like this:

    Code:
    by patient_id stay (days8dtotal), sort: assert days8dtotal[1] == days8dtotal[_N]
    egen flag = tag(patient_id stay)
    by patient_id (stay), sort: egen wanted = total(cond(flag, days8dtotal, .))
    Replace patient_id by the actual name of whatever variable(s) identify patients in your data.


    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.


    Comment

    Working...
    X