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;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.