Hello,
I'm trying to get a count of the distinct patients existing in two datasets. If I use proc sql and the patient numbers are the same in both datasets, they overwrite each other. Is there a proc sql query that scans 2 datasets and produces a number (in this case 3)? Below are sample data including an example of output I'm looking for.
Current data 1
MRN
1
2
3
10
Current data 2
MRN
1
2
3
4
5
MRN_both
1
2
3
Best
Laura
@lmyers2 wrote:(emphasis mine)
Hello,
I'm trying to get a count of the distinct patients existing in two datasets. If I use proc sql and the patient numbers are the same in both datasets, they overwrite each other. Is there a proc sql query that scans 2 datasets and produces a number (in this case 3)? Below are sample data including an example of output I'm looking for.
Current data 1
MRN
1
2
3
10
Current data 2
MRN
1
2
3
4
5
MRN_both
1
2
3
Best
Laura
Why is the fact that they "overwrite each other" a problem. This means that you will get 1 observation per common MRN, which in turns means you will have only 3 observations - the number you want
proc sql noprint;
create table _null_ as select a.mrn from
data1 as a
join
data2 as b
on a.mrn=b.mrn;
quit;
%put &=sqlobs;
This creates table _NULL_ which is not an actual physical data set, but it does cause SQL to behave as if it were. So there will be a count of qualifying matches in macrovar SQLOBS. If you can an actual table, change _NULL_ to a dataset or table name.
data data1;
input MRN;
cards;
1
2
3
10
;
data data2;
input MRN;
cards;
1
2
3
4
5
;
proc sql;
create table want as
select mrn from data1
intersect
select mrn from data2;
quit;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.