Announcement

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

  • Select/Tag observations in panel data if in year "x" the variable "y" took value 1.

    Dear Stata experts,

    My question is very simple: I'd like to select only a subset of respondents in my panel data on the basis that they all died at home at a certain point in their lives (I have in fact multiple longitudinal observations of the same respondents).

    In other words, I would like to retain ALL the observations relating ONLY to those respondents who died at home.
    Conversely, I'd like to delete all the observations relating to respondents who died elsewhere.

    What I do not want to have is ending up with a cross-section containing only the observations of deaths at home.

    Is there a way to tag the unique identifier of those respondents who will die at home? Thus selecting only the observations relating to people who will die at home?

    To do this manually would take a lot of time as I have 50k observations.

    Let's say that the identifier is ID and homed==1 identifies a death home.


    Thank you very much for your help,
    Diana

  • #2
    Diana:
    welcome to this forum.
    Do you mean something along the following lines?
    Code:
     use "http://www.stata-press.com/data/r15/nlswork.dta"
    (National Longitudinal Survey.  Young Women 14-26 years of age in 1968)
    
    . egen flag=group( idcode year union) if union==1
    (24024 missing values generated)
    
    *save you original dataset with a different name*
    
    keep if flag!=.
    However, I would prefer to -keep- all the observations in my dataset and use the -if- qualifier to perform statistical analysis on a given subsample.
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Sounds like

      Code:
      bysort ID: egen diedathome = max(homed==1)
      keep if diedathome
      See e.g. https://www.stata.com/support/faqs/d...ble-recording/ for discussion.

      Comment


      • #4
        Thank you very much to both of you!

        Comment

        Working...
        X