Table of Contents
An outer join in SAS is a data manipulation technique used to combine two or more tables in a way that preserves all data from both tables, even if there is no matching data in one of the tables. This is achieved by using the MERGE statement in SAS, which allows for the joining of tables based on a common variable or set of variables. The result of an outer join will include all rows from both tables, with missing values inserted for any unmatched variables. This is a useful tool for analyzing and comparing data from multiple sources, as it ensures that no data is lost in the merging process.
Perform an Outer Join in SAS (With Example)
You can use the following basic syntax to perform an outer join with two datasets in SAS:
proc sql;
create table final_table asselect coalesce(x.team, y.team) as team, x.team, x.points, y.team, y.assists
from data1 as x full join data2 as y
on x.team = y.team;
quit;This particular example performs an outer join using the full join statement and returns all rows from the datasets called data1 and data2.
The following example shows how to use this syntax in practice.
Example: Perform an Outer Join in SAS
Suppose we have the following two datasets in SAS that contain information about various basketball teams:
/*create datasets*/
data data1;
input team $ points;
datalines;
A 18
B 22
C 19
D 14
E 14
F 11
G 20
H 28
;
run;
data data2;
input team $ assists;
datalines;
A 4
B 9
C 14
D 13
L 10
M 8
;
run;
/*view datasets*/
proc printdata=data1;
proc printdata=data2;
We can use the following syntax to perform an outer join and create a new dataset that contains each row from both datasets:
/*perform outer join*/
proc sql;
create table final_table asselect coalesce(x.team, y.team) as team, x.team, x.points, y.team, y.assists
from data1 as x full join data2 as y
on x.team = y.team;
quit;
/*view results of outer join*/
proc printdata=final_table;

The resulting dataset contains every row from each individual dataset.
Note that we had to use the function to ensure that the team names from both datasets were returned in the resulting dataset.
If we didn’t use this function, then only the values from the team column in the first dataset would be displayed in the resulting dataset.
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How do you perform an outer join in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-perform-an-outer-join-in-sas/
stats writer. "How do you perform an outer join in SAS?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-you-perform-an-outer-join-in-sas/.
stats writer. "How do you perform an outer join in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-perform-an-outer-join-in-sas/.
stats writer (2024) 'How do you perform an outer join in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-perform-an-outer-join-in-sas/.
[1] stats writer, "How do you perform an outer join in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How do you perform an outer join in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
