Home » How to Perform an Inner Join in SAS (With Example)

How to Perform an Inner Join in SAS (With Example)

by Tutor Aspire

You can use the following basic syntax to perform an inner join with two datasets in SAS:

proc sql;
    create table final_table as
    select * from data1 as x join data2 as y
    on x.ID = y.ID;
quit;

The following example shows how to use this syntax in practice.

Related: How to Perform a Left Join in SAS

Example: Inner Join in SAS

Suppose we have the following two datasets in SAS:

/*create datasets*/
data data1;
    input team $ points;
    datalines;
Mavs 99
Spurs 93
Rockets 88
Thunder 91
Warriors 104
Cavs 93
Nets 90
Hawks 91
;
run;

data data2;
    input team $ rebounds;
    datalines;
Mavs 21
Spurs 18
Warriors 27
Hawks 29
Knicks 40
Raptors 30
;
run;

/*view datasets*/
proc print data=data1;
proc print data=data2;

Notice that the two datasets share one variable in common: team.

We will use the following syntax to perform an inner join and create a new dataset that contains only the rows in which the team variable shows up in both datasets:

/*perform inner join*/
proc sql;
	create table final_table as
	select * from data1 as x join data2 as y
	on x.team = y.team;
quit;

/*view results of inner join*/
proc print data=final_table;

The resulting dataset contains only the rows in which the team variable appeared in both datasets.

If you refer to the two datasets from earlier, you’ll notice that there are only four teams that appear in both datasets: Mavs, Spurs, Warriors, and Hawks.

Since we chose to join the two datasets on the team variable, these are the four teams that also appear in the final dataset.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

How to Normalize Data in SAS
How to Replace Characters in a String in SAS
How to Replace Missing Values with Zero in SAS
How to Remove Duplicates in SAS

You may also like