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

I have two large datasets, where variables are different but shares similar id (or name in this example), how do I find out which obs are common and not?

 

data one;

length name $30;
input name;
datalines;
mary
peter
john
;
run;

 

data two;

length name $30;
input name;
datalines;
mary
peter
luke
;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

This is what I do in such cases:

proc sort data=one;
by name;
run;

proc sort data=two;
by name;
run;

data
  common
  only_one
  only_two
;
merge
  one (in=o)
  two (in=t)
;
by name;
if o and t
then output common;
else if o
then output only_one;
else output only_two;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

This is what I do in such cases:

proc sort data=one;
by name;
run;

proc sort data=two;
by name;
run;

data
  common
  only_one
  only_two
;
merge
  one (in=o)
  two (in=t)
;
by name;
if o and t
then output common;
else if o
then output only_one;
else output only_two;
run;
Kurt_Bremser
Super User

PS to speed up the process, sort to new datasets and keep only the key variable:

proc sort
  data=one (keep=name)
  out=one_s
;
by name;
run;

proc sort
  data=two (keep=name)
  out=two_s
;
by name;
run;

data
  common
  only_one
  only_two
;
merge
  one_s (in=o)
  two_s (in=t)
;
by name;
if o and t
then output common;
else if o
then output only_one;
else output only_two;
run;

 

Ksharp
Super User
 

data one;
length name $30;
input name;
datalines;
mary
peter
john
;
run;

 

data two;
length name $30;
input name;
datalines;
mary
peter
luke
;
run;


proc sql;
create table common_in_both as
select name from one
intersect
select name from two;

create table in_one_but_not_in_two as
select name from one
except
select name from two;

create table in_two_but_not_in_one as
select name from two
except
select name from one;
quit;

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
  • 1711 views
  • 0 likes
  • 3 in conversation