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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1889 views
  • 3 likes
  • 3 in conversation