BookmarkSubscribeRSS Feed
GreenTree1
Obsidian | Level 7

HI,

 

I have two datasets.

 

data 1                                 data 2

id .  infection                        id .       

1 .    no                                 1

1 .    yes                                2

2 .    no                                 4

3 .     yes

2 .     no

2 .     no 

4.     no

 

the only IDs and observations I want to keep in Data 1 are the ones in data 2. So, in the end, I want Data 1 to look as follows

 

data 1                                 data 2

id .  infection                        id .       

1 .    no                                 1

1 .    yes                                2

2 .    no                                 4

 

2 .     no

2 .     no 

4.     no

 

Since Id 3 is not in data 2, it has been removed. I also want to keep the multiple observations per id in data 1 and not limit data 1 to 1 obs per id. How do I go about this? would appreciate your help.

 

Thanks

5 REPLIES 5
Sathish_jammy
Lapis Lazuli | Level 10

 

proc sort data = data1;
by id;
run;

data want;
merge data1(in=in1)
data2 (in=in2);
by id;
if in1 and in2;
run;
Ksharp
Super User
proc sql;
create table want as
select * from data1
 where id in (select id from data2);
quit;
GreenTree1
Obsidian | Level 7

Thank you Satish and Ksharp,

 

The code worked but it has shrunk my data more than expected. I just want to keep the IDs common to data1 and data2 and get rid of IDs in data 1 that are not in data2.

 

Data 1 has multiple observations tied to one ID and I want to keep those observations as well, whereas DATA 2 has one obs/id.

Patrick
Opal | Level 21

@GreenTree1 

Using the sample data you've posted the proposed solutions will both return the desired result you've posted.

If things are not working with your real data then there must be something different you haven't told us yet.

 

"Data 1 has multiple observations tied to one ID"

If you've got the ID repeated per observations as in your sample data then the proposed solutions will work. If they aren't repeated then you need to post sample data that's representative for your real data.

GreenTree1
Obsidian | Level 7
Thanks Patrick, I will try this on the original data and will update this post shortly.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1748 views
  • 0 likes
  • 4 in conversation