Hi all,
I had this situation where I need to exclude all the ID's with history is 1 on the first observation. For example, in the below scenario there are multiple observations for each ID. I want to delete all those ID's which had history of disease (=1) on the first observation.
Data I have
| ID | Order of occurrence | History |
| 1 | 1 | 1 |
| 1 | 2 | 1 |
| 1 | 3 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 0 |
| 3 | 2 | 1 |
| 4 | 1 | 0 |
| 5 | 1 | 0 |
| 5 | 2 | 1 |
Data i want to keep
| ID | Order of occurrence | History |
| 3 | 1 | 0 |
| 3 | 2 | 1 |
| 4 | 1 | 0 |
| 5 | 1 | 0 |
| 5 | 2 | 1 |
I tried so many time but i can't get the result I want. Please help me. Thanks in advance for the support.
Assuming your data are already sorted properly, try this:
data have;
input ID Order_of_occurrence History;
cards;
1 1 1
1 2 1
1 3 1
2 1 1
3 1 0
3 2 1
4 1 0
5 1 0
5 2 1
;
run;
data want;
do until(last.ID);
set have;
by ID;
if first.ID = history then _N_ = 0;
if _N_ then output;
end;
run;
Bart
Assuming your data are already sorted properly, try this:
data have;
input ID Order_of_occurrence History;
cards;
1 1 1
1 2 1
1 3 1
2 1 1
3 1 0
3 2 1
4 1 0
5 1 0
5 2 1
;
run;
data want;
do until(last.ID);
set have;
by ID;
if first.ID = history then _N_ = 0;
if _N_ then output;
end;
run;
Bart
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.