Hello, I am looking to delete all initial observations of VAR, within each ID group, until we hit the first 0. If none of the values within an ID group are 0, then I want to remove all of them.
Here is a example of the data I have. I would like to remove observations 1, 10, 11, 12, 13 and 14
Thanks in advance!
observation | ID | VAR |
1 | 1 | 1 |
2 | 1 | 0 |
3 | 1 | 1 |
4 | 1 | 0 |
5 | 1 | 1 |
6 | 1 | 1 |
7 | 2 | 0 |
8 | 2 | 1 |
9 | 2 | 0 |
10 | 3 | 1 |
11 | 3 | 1 |
12 | 3 | 1 |
13 | 4 | 1 |
14 | 4 | 1 |
15 | 4 | 0 |
16 | 4 | 1 |
data want;
set have;
by id;
retain flag;
if first.id then flag = 0;
if var = 0 then flag = 1;
if flag;
drop flag;
run;
data want;
do until (last.var);
set have;
by id var notsorted ;
if first.id and var=1 then flag=1;
if flag ne 1 then output ;
end;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.