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:
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:
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!
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]
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
Thanks again!
Comment