BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cosmid
Lapis Lazuli | Level 10

If I have the following two datasets:

data set ONE:

ID X

1   a

2   .

3   c

6   a

 

data set TWO:

ID Y

1   a

2   b

5   d

6   e

 

Here's the output I am looking for:

ID X  Y

2   .   b

3   c   .

5   .   d

6   a  e

 

Is there a way to do this in PROC SQL? 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

So if X & Y are the same you delete the record? In that case this would work, assuming your input data is sorted by ID.

data want;
merge one two;
by ID;
if x = y then delete;
run;

 

proc sql;

create table want as

select coalesce(t1.ID, t2.ID) as ID, X, Y

from ONE as t1

full join TWO as t2

on t1.id = t2.id

where x ne y;

quit;

View solution in original post

4 REPLIES 4
Reeza
Super User

So if X & Y are the same you delete the record? In that case this would work, assuming your input data is sorted by ID.

data want;
merge one two;
by ID;
if x = y then delete;
run;

 

proc sql;

create table want as

select coalesce(t1.ID, t2.ID) as ID, X, Y

from ONE as t1

full join TWO as t2

on t1.id = t2.id

where x ne y;

quit;

cosmid
Lapis Lazuli | Level 10
Wow, that datastep is so clean compared to a Proc SQL. I needed Proc SQL for my case because with Proc SQL it will not print anything to the output window if the dataset is empty.

Thank you for the help!

And I really need to read more about coalesce and full joins!
Ksharp
Super User
data ONE;
input ID X $;
cards;
1   a
2   .
3   c
6   a
;
 

data TWO;
input ID Y $;
cards;
1   a
2   b
5   d
6   e
;


proc sql;
create table want as
select coalesce(a.id,b.id) as id,x,y
 from 
 (select * from one except select * from two) as a
 full join
 (select * from two except select * from one) as b
 on a.id=b.id;
quit;
cosmid
Lapis Lazuli | Level 10
Thank you Ksharp! both your code and Reeza's worked. I would have accepted both as a solution but it only allowed me to pick one so I just picked one that replied earlier hopefully you are okay with it. I really appreciate your help on this! I learned from both of your codes! Thanks again!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 709 views
  • 2 likes
  • 3 in conversation