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

I have a data set "all_data", and based on 5 creterias I subsetted to 5 data sets, data1, data2, data3, data4, data5.

 

Now I want to select all the common cases in the 5 data sets, i.e. selecting those cases present in all of the 5 data sets, or those cases satisfying all 5 creterias.

 

In my mind I have two way to do it:

 

proc sql;
create table cases_with_all_5_features as
select a.*
from all_data as a
where a.dupi in (select DUPI from data1) and
      a.dupi in (select DUPI from data2) and
      a.dupi in (select DUPI from data3) and
      a.dupi in (select DUPI from data4) and
      a.dupi in (select DUPI from data5);
quit;

or using data step:

data cases_with_all_5_feature ;
merge all_data (In=A) data1 (In=B)  data2 (in=C) data_3 (in=D) data_4 (In=E) data5 (in=F);
by DUPI;
if A and B and C and D and E and F;
run;

I wonder if there is a simpler way to do this? I do not feel great about these two methods.

 

Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

If your tables have the same variables and no duplicates, you can use intersect

 

proc sql;
create table cases_with_all_5_features as
select * from data1
intersect
select * from data2
intersect
select * from data3
intersect
select * from data4
intersect
select * from data5;
quit;
PG

View solution in original post

3 REPLIES 3
Astounding
PROC Star

None that I know of ... but here's something to think about.  If you're going to need the same set of matches in multiple programs, you might merge the five data sets (data1 through data5) and save the result.  That would make the matching simpler and give you more choices later as to how to look for matches.

PGStats
Opal | Level 21

If your tables have the same variables and no duplicates, you can use intersect

 

proc sql;
create table cases_with_all_5_features as
select * from data1
intersect
select * from data2
intersect
select * from data3
intersect
select * from data4
intersect
select * from data5;
quit;
PG
fengyuwuzu
Pyrite | Level 9
Thanks. They all have the same data structure and no duplicates. This is easier because I do not need to read the all_data set.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1554 views
  • 0 likes
  • 3 in conversation