Hi all,
I would like to remove a group of observations from my dataset if the first value does not meet a certain criteria. For example, I would like to delete all entries for a certain subject ID when the first QC value is 0. If the first QC value is 1, then I do not want to delete any entries from this subject ID.
Here is an example of what my data looks like (envision additional columns with various outcome variables):
ID QC
1 1
1 0
1 1
1 1
2 0
2 0
2 1
2 1
3 1
3 0
3 1
I would like achieve the following:
ID QC
1 1
1 0
1 1
1 1
3 1
3 0
3 1
*subject with ID=2 was deleted because the first observation did not meet quality control (QC=0).
Thanks in advance!
data have;
input ID QC ;
cards;
1 1
1 0
1 1
1 1
2 0
2 0
2 1
2 1
3 1
3 0
3 1
;
data want;
set have;
by id;
retain flag;
if first.id then call missing(flag);
if first.id and qc=1 then flag=1;
if flag;
drop flag;
run;
data have;
input ID QC ;
cards;
1 1
1 0
1 1
1 1
2 0
2 0
2 1
2 1
3 1
3 0
3 1
;
data want;
set have;
by id;
retain flag;
if first.id then call missing(flag);
if first.id and qc=1 then flag=1;
if flag;
drop flag;
run;
Thanks so much!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.