The following example uses
the MAX function to find the largest population in the SQL.COUNTRIES
table and displays it in a column called MaxPopulation. Aggregate
functions, such as the MAX function, can cause the same calculation
to repeat for every row. This occurs whenever PROC SQL remerges data.
Remerging occurs whenever any of the following conditions exist:
-
The SELECT clause references a
column that contains an aggregate function that is not listed in a
GROUP BY clause.
-
The SELECT clause references a
column that contains an aggregate function and other column or columns
that are not listed in the GROUP BY clause.
-
One or more columns or column expressions
that are listed in a HAVING clause are not included in a subquery
or a GROUP BY clause.
In this example, PROC
SQL writes the population of China, which is the largest population
in the table:
libname sql 'SAS-library';
proc sql outobs=12;
title 'Largest Country Populations';
select Name, Population format=comma20.,
max(Population) as MaxPopulation format=comma20.
from sql.countries
order by Population desc;
Using Aggregate Functions
In some cases, you might
need to use an aggregate function so that you can use its results
in another calculation. To do this, you need only to construct one
query for PROC SQL to automatically perform both calculations. This
type of operation also causes PROC SQL to remerge the data.
For example, if you
want to find the percentage of the total world population that resides
in each country, then you construct a single query that performs the
following tasks:
-
obtains the total world population
by using the SUM function
-
divides each country's population
by the total world population
PROC SQL runs an internal query to find the sum and
then runs another internal query to divide each country's population
by the sum.
libname sql 'SAS-library';
proc sql outobs=12;
title 'Percentage of World Population in Countries';
select Name, Population format=comma14.,
(Population / sum(Population) * 100) as Percentage
format=comma8.2
from sql.countries
order by Percentage desc;
Note: When a query remerges data,
PROC SQL displays a note in the log to indicate that data remerging
has occurred.
Remerging Summary Statistics