Obsidian | Level 7

## Remove every row for an ID if on every row for that ID all 3 variables lack a set of values.

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;

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

## Re: Remove every row for an ID if on every row for that ID all 3 variables lack a set of values.

This is an instance where you can take advantage of a self merge.  Just merge, bi ID, the following

1. Any instance where an 'A' or 'B' is found.
2. All observations in 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;
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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
2 REPLIES 2
PROC Star

## Re: Remove every row for an ID if on every row for that ID all 3 variables lack a set of values.

This is an instance where you can take advantage of a self merge.  Just merge, bi ID, the following

1. Any instance where an 'A' or 'B' is found.
2. All observations in 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;
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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Obsidian | Level 7

## Re: Remove every row for an ID if on every row for that ID all 3 variables lack a set of values.

Amazing. Thanks a lot!
Discussion stats
• 2 replies
• 464 views
• 3 likes
• 2 in conversation