Announcement

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

  • Gen newvar = 1 if var1==1 and all subsequent observations (long data)

    Hello, I am trying to determine how to generate a new variable when the criteria is met, and all following observations for that study id in a long formatted dataset. Essentially censoring the data beyond the set time point, but as a 0/1 dummy variable.

    I have tried codes along the lines of:
    Code:
    bysort id: gen newvar = 1 if var1==1 & var1[_n+1]==0
    
    or just
    
    bysort id: gen newvar = 1 if var1==1 & var1[_n+1]
    However, I cannot figure out how to get stata to identify subsequent observations for that ID.

    Thank you so much.



    EDIT:

    I THINK I was able to find the answer on another forum. A user suggested:

    Code:
    bysort id (visit): gen newvar = sum(var1[_n-1]) > 0  
    
    replace newvar = 1 if var1==1
    Which seems to have worked, but I don't really understand why. If anyone could explain this, or have a different suggestion, I'd greatly appreciate it.

    Thanks again!
    Last edited by Nate Fitzpatrick; 04 Nov 2021, 14:20.

  • #2
    Your set-up appears to be that you have an indicator variable visit in panel data and your single criterion appears to be that you want a marker that switches from 0 to 1 when you pass the first value of 1.

    Code:
    bysort id (visit) : gen wanted = sum(visit) > 0
    would do that as a the cumulative sum of a series of 0s remains zero but the cumulative sum jumps up at the first occurrence of 1.

    When cross-referencing please give the URL of your source.

    See also https://www.stata.com/support/faqs/d...t-occurrences/ for more explanation.

    Comment

    Working...
    X