This tutorial is designed for beginners who want to get started with PROC SQL Joins. It explains different types of joins and the equivalent data step merge code for these joins. This tutorial includes several examples to help you practice and become proficient in PROC SQL Joins.
- PROC SQL joins do not require sorted tables (data sets), while you need to have two data sets sorted when using Merge Statement
- PROC SQL joins do not require that common variable have the same name in the data sets you are joining, while you need to have common variable name listed in BY option when using MERGE statement.
- PROC SQL joins can use comparison operators other than the equal sign (=).
- PROC SQL can handle many to many relationship well whereas Data Step Merge do not.
1. Cross Join / Cartesian product
The Cartesian product returns a number of rows equal to the product of all rows (observations) in all the tables (data sets) being joined. For example, if the first table has 10 rows and the second table has 10 rows, there will be 100 rows (10 * 10) in the merged table (data set).
Let's create the two sample datasets that will be used in this tutorial to explain how to use JOINS in SAS.
Data A; Input ID Name$ Height; cards; 1 A 1 3 B 2 5 C 2 7 D 2 9 E 2 ; run;
Data B; Input ID Name$ Weight; cards; 2 A 2 4 B 3 5 C 4 7 D 5 ; run;
The following code shows how to apply Cartesian Product using PROC SQL in SAS.
PROC SQL; Create table dummy as Select * from A as x cross join B as y; Quit;
Cartesian or Cross Product |
- Since the first data set has 5 rows and the second data set has 4 rows, there are 20 rows (5 * 4) in the merged data set.
- The 'as' keyword (aka alias) is used to assign a table a temporary name.
- Since the ID values of the first data set is different than the ID values of the second data set, the ID given in the joined data set is misleading.
2. Inner Join
The INNER JOIN returns rows common to both tables (data sets). If we select * keyword in the query, the final merged file would have number of columns equal to (Common columns in both the data sets + uncommon columns from data set A + uncommon columns from data set B).
Venn Diagram : Inner Join |
PROC SQL; Create table dummy as Select * from A as x, B as y where x.ID = y.ID; Quit;
Inner Join |
Since the above case is of type INNER JOIN, it returns values 5 and 7 from the variable ID in the combined table as these two values are common in both the datasets A and B
PROC SQL; Create table dummy as Select * from A as x inner join B as y On x.ID = y.ID; Quit;
Both the codes produce same result.
Data dummy; Merge A (IN = X) B (IN=Y); by ID; If X and Y; run;
3. Left Join
The LEFT JOIN returns all rows from the left table with the matching rows from the right table.
Left Join Venn Diagram |
PROC SQL; Create table dummy as Select * from A as x left join B as y On x.ID = y.ID; Quit;
Left Join |
Since the above case is of type LEFT JOIN, it returns all rows from the table (dataset) A with the matching rows from the dataset B.
Data dummy; Merge A (IN = X) B (IN=Y); by ID; If X ; run;
4. Right Join
The RIGHT JOIN returns all rows from the right table that do not match any row with the left-hand table, and the matched rows from the left-hand table.
Right Join Venn Diagram |
PROC SQL; Create table dummy as Select * from A as x right join B as y On x.ID = y.ID; Quit;
Right Join |
proc sql; create table dummy as select coalesce (x.ID,y.ID) as ID, coalesce (x.name,y.name) as name,height,weight from a as x right join b as y on x.id = y.id; quit;
Right Join with Coalesce |
Since the above case is of type RIGHT JOIN, it returns all rows from the table (dataset) B with the matching rows from the dataset A.
Data dummy; Merge A (IN = X) B (IN=Y); by ID; If Y ; run;
5. Full Join
The FULL JOIN returns all rows from the left table and from the right table.
Full Join |
Key takeaway : The FULL JOIN suffers the same difficulty as the RIGHT JOIN. Namely, the common variable values are lost from the right-hand data set. The COALESCE function can solve this difficulty.
proc sql; create table dummy as select coalesce (x.ID,y.ID) as ID, coalesce (x.name,y.name) as name,height,weight from a as x full join b as y on x.id = y.id; quit;
Full Join with Coalesce |
Since the above case is of type FULL JOIN, it returns all rows from the table (dataset) A and B.
Data dummy; Merge A B; by ID; run;By default, MERGE statement performs full join so IN variables are not required.
Join : One to Many Relationship |
SQL Join will return Cartesian Product if duplicate values are found in primary key (common column). In this example, it returns cartesian product of missing values in the "ID" column. Since dataset A has 3 missing values and dataset B has 1 missing value, there are 3 (3*1) missing values in the merged dataset.
Data Step MERGE statement will return the maximum number of missing values in the primary key in both the tables. In this case, it would return 3 missing values i.e. max(3,1).
PROC SQL : One to Many Relationship |
When duplicates, PROC SQL returns cartesian product i.e. product of both the tables. In dataset A, we have 2 5s and 3 5s in dataset B. So, it returns (2x3 = 6) 5s in the combined table.
In PROC SQL, you can refer to permanent libraries when performing joins by specifying the library and table names - library_name.table_name
. See the example below.
PROC SQL; Create table dummy as Select * from readin.A as x left join readin.B as y On x.ID = y.ID; Quit;
Its Very Useful... Thanks a lot sharing..!
ReplyDeleteThank you for stopping by. Glad you found the article useful :-)
DeleteWell explained! Keep up the good work :-)
ReplyDeleteThank you for your appreciation :-)
DeleteReally useful :-)
ReplyDeleteThanks Deepanshu for sharing this valuable knowledge. Its really a treasure for beginners like me.
ReplyDeleteWell explained..Its really useful especially for beginners. Thank u for sharing.
ReplyDeletereally very helpful ! i have one doubt explain the ten procedures which were used in SQL?
ReplyDeleteVery Helpful and easy to understand
ReplyDeleteThanks!
Extremely helpful, thank you very much !
ReplyDeleteReally very helpful but I found a small typo. In the full join SQL code wont it be 'full join' instead of 'right join'?
ReplyDeleteThanks for pointing it out, typo now corrected!
DeleteLove your site...I'm glad that I found this site...
ReplyDeleteGlad you found it useful. Cheers!
DeleteExcellent! good explanation with codes . . . very useful!
ReplyDeletethank you so much!!
ReplyDeleteLoved your explanation..Very useful for SAS beginners..Keep going
ReplyDeletevery usefull information..
ReplyDeleteHi Deepanshu,
ReplyDeletein all the joins it is mentioned
(Common columns in both the data sets + uncommon columns from data set A + uncommon columns from data set B).
is this true????
If you include "*" keyword, it would select variables from both the tables.
Deletefull join code should be like this
ReplyDeletePROC SQL;
Create table dummy as
Select coalesce (x.ID,y.ID) as ID,
coalesce (x.name,y.name) as name,height,weight
from A as x full join B as y
on x.ID = y.ID;
Quit;
Thanks for pointing it out. I have corrected the code. Cheers!
Deletefull join Data Step code should be
ReplyDeleteData dummy;
Merge A (IN = X) B (IN=Y);
by ID;
if x=1 or y=1;
run;
Please let me know if I am wrong
DeleteBy default, MERGE Statement performs FULL JOIN so "if x=1 or y=1;" is not required. Please let me know incase any query(s) Thanks!
DeleteThe output record will be prepared either from the record of A or B, hence the statement "if x=1 or y=1;" will not be required to mention and it will be by default.
DeleteWell explained about joins. Very use full for the beginners.
ReplyDeleteDid you read the first paragraph 'Advantages of PROC SQL Joins over Data Step Merging'? That's answer of your first question. Second question is also discussed in the article. If you have duplicates in any of the tables, you would get cartesian product of the duplicate records.
ReplyDeleteWas very useful.!! Well explained.
ReplyDeleteWhat about Outer joins? How it will be prepared using PROC SQL?
ReplyDeleteThis is really great website. I'm glad I found it.
ReplyDeleteIt helpful for my understanding, Thank you
ReplyDeleteHow many datasets can be merged in proc sql (sas 9.4)?
ReplyDeleteI love your site to refer any concept in SAS or SQL
ReplyDeleteSuperb description about SAS
ReplyDeleteGreat, thanks for posting this.
ReplyDeleteexcellent explanation
ReplyDeleteHi Deepanshu,
ReplyDeleteI appreciate your work and thanks for creating a decent tool. If you'll mention the rules that would be great. Also add interesting scenarios bit logical concepts.
and Syntax of every function statement.
Thanks
I dont think ur explaination about inner join is accurate, ud better compare that with Proc SQL outter join.
ReplyDeleteWhy missing values are coming in right join but not in left join?
ReplyDeleteGreat Job!
ReplyDeleteHi, Deepanshu! It's been years since you last answered to any comments here, so this is a long shot. What if I want to use PROC SQL to join various tables that are listed in sequence? For instance, BIRTHS12, BIRTHS13... BIRTHS21? Is there a way to join tables in a range? [BIRTHS12-BIRTHS21]?
ReplyDeleteIn PROC SQL, you can use the %do %to loop to generate a series of join statements for each table from BIRTHS12 to BIRTHS21. Better way to do this via merge statement in SAS (as shown below) -
Deletedata want;
merge BIRTHS12-BIRTHS21;
by ID;
run;