Suppose you need to join multiple tables by a primary key using PROC SQL.
![]() |
PROC SQL : Multiple Tables |
The sample data for three tables are shown below. The primary key in these tables is the variable "ID". We need to join these tables.
Create Sample Data
SAS : PROC SQL Code
Create Sample Data
data temp;
input id x1 x2;
cards;
1 25 37
2 35 47
3 44 97
;
run;
data temp2;
input id var1 var2;
cards;
2 65 37
3 85 47
5 34 97
;
run;
data temp3;
input id xx1 xx2;
cards;
3 55 37
5 25 47
4 64 97
;
run;
SAS : PROC SQL Code
proc sql noprint;
create table test as
select a.ID, b.*, c.* from
temp a left join temp2 b
on a.id = b.id
left join temp3 c
on a.id = c.id;
quit;
![]() |
Output : Joined Table |
Thanks for sharing..Good one..
ReplyDeleteHi, thanks for your tutorial. By the way I got one question: how to join efficiently more than 10 tables using proc sql (should we do a loop for that?)
ReplyDeleteThanks.
Yes, you can use loop. Loop works under PROC SQL.
ReplyDeleteHi Deepanshu, Can i have a example for the same. If you feel like to share.
ReplyDeleteHi Deepanshu, in reference to Mr.Swaroop's questions... can we achieve the output by referring to the same code given above just by adding table and respective column names one after another if i have 10 tables? is it possible if i don't want to use any sort of loops as you have suggested?
ReplyDelete