Previous Page | Next Page

Introduction to the SQL Procedure

Comparing PROC SQL with the SAS DATA Step

PROC SQL can perform some of the operations that are provided by the DATA step and the PRINT, SORT, and SUMMARY procedures. The following query displays the total population of all the large countries (countries with population greater than 1 million) on each continent.

proc sql;
   title 'Population of Large Countries Grouped by Continent';
   select Continent, sum(Population) as TotPop format=comma15.
      from sql.countries
      where Population gt 1000000
      group by Continent
      order by TotPop;
quit;

Sample SQL Output

                 Population of Large Countries Grouped by Continent

                  Continent                                TotPop
                  -----------------------------------------------
                  Oceania                               3,422,548
                  Australia                            18,255,944
                  Central America and Caribbean        65,283,910
                  South America                       316,303,397
                  North America                       384,801,818
                  Africa                              706,611,183
                  Europe                              811,680,062
                  Asia                              3,379,469,458

Here is a SAS program that produces the same result.

title 'Large Countries Grouped by Continent';
proc summary data=sql.countries;
   where Population > 1000000;
   class Continent;
   var Population;
   output out=sumPop sum=TotPop;
run;

proc sort data=SumPop;
   by totPop;
run;

proc print data=SumPop noobs;
   var Continent TotPop;
   format TotPop comma15.;
   where _type_=1;
run;

Sample DATA Step Output

                        Large Countries Grouped by Continent

                 Continent                                  TotPop

                 Oceania                                 3,422,548
                 Australia                              18,255,944
                 Central America and Caribbean          65,283,910
                 South America                         316,303,397
                 North America                         384,801,818
                 Africa                                706,611,183
                 Europe                                811,680,062
                 Asia                                3,379,469,458

This example shows that PROC SQL can achieve the same results as Base SAS software but often with fewer and shorter statements. The SELECT statement that is shown in this example performs summation, grouping, sorting, and row selection. It also displays the query's results without the PRINT procedure.

PROC SQL executes without using the RUN statement. After you invoke PROC SQL you can submit additional SQL procedure statements without submitting the PROC statement again. Use the QUIT statement to terminate the procedure.

Previous Page | Next Page | Top of Page