Chapter Contents

Previous

Next
The SQL Procedure

Example 7: Performing an Outer Join


Procedure features:
joined-table component
left outer join
SELECT clause
COALESCE function
WHERE clause
CONTAINS condition
Tables: PROCLIB.PAYROLL, PROCLIB.PAYROLL2

This example illustrates a left outer join of the PROCLIB.PAYROLL and PROCLIB.PAYROLL2 tables.



Input Tables
PROCLIB.PAYROLL (Partial Listing) [HTML Output]
 [Listing Output]
PROCLIB.PAYROLL2 [HTML Output]
 [Listing Output]


Program

libname proclib 'SAS-data-library';
options nodate pageno=1 linesize=80 pagesize=60;
 Note about code
proc sql outobs=10;
   title 'Most Current Jobcode and Salary Information';
   select p.IdNumber, p.Jobcode, p.Salary,
          p2.jobcode label='New Jobcode',
          p2.salary label='New Salary' format=dollar8.
 Note about code
      from proclib.payroll as p left join proclib.payroll2 as p2




 Note about code
      on p.IdNumber=p2.idnum;


Output
As the output shows, all rows from the left table, PROCLIB.PAYROLL, are returned. PROC SQL assigns missing values for rows in the left table, PAYROLL, that have no matching values for IdNum in PAYROLL2. [HTML Output]
 [Listing Output]
 Note about code
   title 'Most Current Jobcode and Salary Information';
   select p.idnumber, coalesce(p2.jobcode,p.jobcode) 
        label='Current Jobcode',






 Note about code
          coalesce(p2.salary,p.salary) label='Current Salary'
                  format=dollar8.





 Note about code
      from proclib.payroll p left join proclib.payroll2 p2
      on p.IdNumber=p2.idnum;



Output
[HTML Output]  [Listing Output]
 Note about code
   title 'Most Current Information for Ticket Agents';
   select p.IdNumber,
          coalesce(p2.jobcode,p.jobcode) label='Current Jobcode',
          coalesce(p2.salary,p.salary) label='Current Salary'
      from proclib.payroll p left join proclib.payroll2 p2
      on p.IdNumber=p2.idnum
      where p2.jobcode contains 'TA';


Output
[HTML Output]  [Listing Output]


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.