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

Hi,

I have two datasets I am trying to join. One of them has ID, and the other one has ID1 and ID2. ID2 is only populated if someone's ID1 changes (the old value goes in ID2, while the new value goes in ID1). What I want to do is join the datasets such that as many people join as possible. That means I want to join ID to ID1, but then, for those where there is no ID1 match, I'd like to join ID to ID2. I know how to use a proc sql statement to join on two variables, but I don't know of a way to add conditions to the join.

Any help is much appreciated.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

proc sql;

     create table WANT as

     select     A.*,

                    B.*

     from        TABLEA A

     full join   TABLEB B

     on          A.ID=COALESCE(B.ID2,B.ID1);

quit;

/* I.e. if ID2 exists then use that for compare, else use ID1 */

You could also do the join a couple of times:

proc sql;       

     ...

     on          A.ID=B.ID1

     full join TABLEB

     on           A.ID=B.ID2;

quit;

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

proc sql;

     create table WANT as

     select     A.*,

                    B.*

     from        TABLEA A

     full join   TABLEB B

     on          A.ID=COALESCE(B.ID2,B.ID1);

quit;

/* I.e. if ID2 exists then use that for compare, else use ID1 */

You could also do the join a couple of times:

proc sql;       

     ...

     on          A.ID=B.ID1

     full join TABLEB

     on           A.ID=B.ID2;

quit;

Loko
Barite | Level 11

Hello,

/*prepare some data*/

data a;
set sashelp.class;
if mod(_N_,2)=1 then do name1=name;age=0;end;
else do;
name1="ABC";
name2=name;
age=15;
end;

if _n_=19 then delete;
run;

proc sql;
create table want as
select b.name, a.age
from a,sashelp.class b
where
b.name=a.name1 or b.name=a.name2;
quit;

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!

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
  • 2 replies
  • 2118 views
  • 3 likes
  • 3 in conversation