BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Manhort
Obsidian | Level 7

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:

IDvar1var2var3
1ABC
1ABC
1ABC
2  C
2AC 
3  C
3 BC
3 BC
4C  
4C  
4C  
4C  

 

WANT:

IDvar1var2var3
1ABC
1ABC
1ABC
2  C
2AC 
3  C
3 BC
3 BC

 

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
mkeintz
PROC Star

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

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

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

--------------------------

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 464 views
  • 3 likes
  • 2 in conversation