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.
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;
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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.