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!