PROC SQL Joins on Multiple Tables

Deepanshu Bhalla 5 Comments , ,

Suppose you need to join multiple tables by a primary key using PROC SQL.

PROC SQL : Join Multiple Tables
PROC SQL : Multiple Tables
Create Sample Data

Sample data for three tables are shown below. The primary key in these tables is the variable "ID". We need to join these tables.

data temp;
input id x1 x2;
1 25 37
2 35 47
3 44 97

data temp2;
input id var1 var2;
2 65 37
3 85 47
5 34 97

data temp3;
input id xx1 xx2;
3 55 37
5 25 47
4 64 97
SAS : PROC SQL Code to Joins Multiple Tables

The following code is creating a new table named "test" by joining data from three different tables ("temp", "temp2", and "temp3") based on the common "ID" column. The result will include all columns from the "temp" table and all columns from both "temp2" and "temp3" tables where the "ID" values match.

proc sql noprint;
create table test as
select a.ID, b.*, c.* from
temp a left join temp2 b
on =
left join temp3 c
on =;
Output of PROC SQL Joins on Multiple Tables
Output : Joined Table

select a.ID, b.*, c.*: This SELECT statement selects columns from three different tables: "temp", "temp2" and "temp3".

a.ID refers to the "ID" column from the "temp" table.
b.* selects all columns from the "temp2" table.
c.* selects all columns from the "temp3" table.

Related Posts
Spread the Word!
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 5 Responses to "PROC SQL Joins on Multiple Tables"
  1. Thanks for sharing..Good one..

  2. Hi, 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?)

  3. Yes, you can use loop. Loop works under PROC SQL.

  4. Hi Deepanshu, Can i have a example for the same. If you feel like to share.

  5. Hi 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?

Next → ← Prev