## selecting common cases in multiple data sets

Solved
Super Contributor
Posts: 328

# selecting common cases in multiple data sets

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.

Accepted Solutions
Solution
‎07-25-2016 03:59 PM
Posts: 5,519

## Re: selecting common cases in multiple data sets

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

All Replies
Super User
Posts: 6,751

## Re: selecting common cases in multiple data sets

[ Edited ]

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.

Solution
‎07-25-2016 03:59 PM
Posts: 5,519

## Re: selecting common cases in multiple data sets

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
Super Contributor
Posts: 328

## Re: selecting common cases in multiple data sets

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.
☑ This topic is solved.