BookmarkSubscribeRSS Feed
bochen
Fluorite | Level 6

Hi all,

 

I would like to use left join to join multiple tables and I am wondering whether the order of the join matters. Say

proc sql;
create table t1 as
select * from A 
natural left join B 
natural left join C;
quit;

does this code generate the same result as 

proc sql;
create table t1 as
select * from A 
natural left join 
(select * from B natural left join C);
quit;

?

 

Or as long as the tables A, B, and C is linked by left join in this order, the result would not change?

 

I tried the above in a small trial dataset, and I got the same table. However, when I used similar multiple natural left join with the data I worked on, I got different result. 

 

More generally, is the following correct: A left join B left join c =  (A left join B) left join c =  A left join (B left join c) =  A left join D where D = B left join c?

 

Thanks

1 REPLY 1
FreelanceReinh
Jade | Level 19

Hi @bochen,

 

Here is a simple example which shows that the first two steps can generate different results:

data a;
id=1; x=2;
run;

data b;
id=1; y=1;
run;

data c;
id=1; x=1;
run;

The first PROC SQL step joins A and B on A.id=B.id to the intermediate result "one obs. with id=1, x=2, y=1" (call this T) and this with C on T.id=C.id & T.x=C.x, which results in T.

 

The second PROC SQL step first joins B and C on B.id=C.id to the intermediate result "one obs. with id=1, x=1, y=1" (call this V) and then joins A with V on A.id=V.id & A.x=V.x, which results in a single observation with id=1, x=2, y=. (missing), because the keys do not match.

 

Other examples show that even if both steps create the same observations, the results can differ in the order of observations and in the order of variables.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1 reply
  • 1629 views
  • 0 likes
  • 2 in conversation