Hi guys,
I have a problem with repeated measure data. I need to remove people entirely if they lack both values (A and B) in all three variables.
HAVE:
ID | var1 | var2 | var3 |
1 | A | B | C |
1 | A | B | C |
1 | A | B | C |
2 | C | ||
2 | A | C | |
3 | C | ||
3 | B | C | |
3 | B | C | |
4 | C | ||
4 | C | ||
4 | C | ||
4 | C |
WANT:
ID | var1 | var2 | var3 |
1 | A | B | C |
1 | A | B | C |
1 | A | B | C |
2 | C | ||
2 | A | C | |
3 | C | ||
3 | B | C | |
3 | B | C |
1 stays, he has A and B in multiple places.
2 stays, he has A in var1 for one of his rows.
3 stays, he has B in at least 1 row, in var 2 this time.
4 is out. He has no A or B at all.
This can't be done from a method of "if it's all C then delete" because 'C' here is just a placeholder for all the things that aren't 'A' or 'B'.
Thanks for any help.
Here's the data step version of my have.
DATA have;
input id var1 $ var2 $ var3 $ ;
CARDS;
1 A B C
1 A B C
1 A B C
2 . . C
2 A C .
3 . . C
3 . B C
3 . B C
4 C . .
4 C . .
4 C . .
4 C . .
;
RUN;
This is an instance where you can take advantage of a self merge. Just merge, bi ID, the following
DATA have;
input id var1 $ var2 $ var3 $ ;
CARDS;
1 A B C
1 A B C
1 A B C
2 . . C
2 A C .
3 . . C
3 . B C
3 . B C
4 C . .
4 C . .
4 C . .
4 C . .
;
RUN;
data want;
merge have (where=(indexw(catx(' ',var1,var2,var3),'A',' ') or indexw(catx(' ',var1,var2,var3),'B',' ')) in=keepthisid)
have;
by id;
if keepthisid;
run;
The MERGE statement has two arguments. The first is the subset of observations that have an "A" or "B", and the second is all observations.
If a given ID never has any "A" or "B" then the KEEPTHISID is always zero for that ID. But if a subject of a given ID has at least one such record, then the behavior of MERGE with BY extends the subset to match every observation in the whole set, so every obs for that ID is kept.
Just be sure that the subset PRECEDES the whole set in the MERGE statement. This will ensure that the values from the dataset on the right-hand will prevail.
This is an instance where you can take advantage of a self merge. Just merge, bi ID, the following
DATA have;
input id var1 $ var2 $ var3 $ ;
CARDS;
1 A B C
1 A B C
1 A B C
2 . . C
2 A C .
3 . . C
3 . B C
3 . B C
4 C . .
4 C . .
4 C . .
4 C . .
;
RUN;
data want;
merge have (where=(indexw(catx(' ',var1,var2,var3),'A',' ') or indexw(catx(' ',var1,var2,var3),'B',' ')) in=keepthisid)
have;
by id;
if keepthisid;
run;
The MERGE statement has two arguments. The first is the subset of observations that have an "A" or "B", and the second is all observations.
If a given ID never has any "A" or "B" then the KEEPTHISID is always zero for that ID. But if a subject of a given ID has at least one such record, then the behavior of MERGE with BY extends the subset to match every observation in the whole set, so every obs for that ID is kept.
Just be sure that the subset PRECEDES the whole set in the MERGE statement. This will ensure that the values from the dataset on the right-hand will prevail.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.