You are on page 1of 2

SAS Table Joins Tutorial & Demonstrations – Tom Tuduc

Inner Join, Right Join, Left Join, and Full Join.


This tutorial demonstrates various table joins in SAS with SQL codes and results.

Sample Data
Table Measurement
PersonId Weight Height
1 133 5.7
3 123 5.4
4 98 5.5
5 155 5.9
6 158 6.1

Table Patient
PersonId Name Gender City
1 Mike J. m Portland
2 Ann K. f Salem
3 Trish L. f Eugene
7 Tom K m Portland
9 Lance T. m Portland

INNER JOIN
proc sql;
select Patient.PersonID,
Height,
Name,
Gender
from Measurement, Patient
where Measurement.PersonId = Patient.PersonId;
quit;

Results:
PersonId Height Name Gender
1 5.7 Mike J. m
3 5.4 Trish L. f

RIGHT JOIN, LEFT JOIN, and FULL JOIN

proc sql;

title " Performing Right Join";


select p.PersonId as Patient_PersonId ,
m.PersonId as Measurement_PersonId,
Height
from learn.Patient as p right join
learn.Measurement as m
on p.PersonId eq m.PersonId;

title " Performing Left Join";


select p.PersonId as Patient_PersonId ,
m.PersonId as Measurement_PersonId,
Height
from learn.Patient as p left join
learn.Measurement as m
on p.PersonId eq m.PersonId;

title " Performing Full Join";


select p.PersonId as Patient_PersonId ,
m.PersonId as Measurement_PersonId,
Height
from learn.Patient as p full join
learn.Measurement as m
on p.PersonId eq m.PersonId;

quit;

Results:

Performing Right Join


Patient_PersonId Measurement_PersonId Height
1 1 5.7
3 3 5.4
4 5.5
5 5.9
6 6.1

Performing Left Join


Patient_PersonId Measurement_PersonId Height
1 1 5.7
2
3 3 5.4
7
9

Performing Full Join


Patient_PersonId Measurement_PersonId Height
1 5.7
2
3 5.4
4 5.5
5 5.9
6 6.1
7
9

You might also like