The HAVING
clause is used with at least one summary function and an optional
GROUP BY clause to summarize groups of data in a table. A HAVING clause
is any valid SQL expression that is evaluated as either true or false
for each group in a query. Alternatively, if the query involves remerged
data, then the HAVING expression is evaluated for each row that participates
in each group. The query must include one or more summary functions.
Typically, the GROUP
BY clause is used with the HAVING expression and defines the group
or groups to be evaluated. If you omit the GROUP BY clause, then the
summary function and the HAVING clause treat the table as one group.
The following PROC SQL
step uses the
PROCLIB.PAYROLL
table (shown in
Creating a Table from a Query's Result) and groups the rows by Gender to
determine the oldest employee of each gender. In SAS, dates are stored
as integers. The lower the birthdate as an integer, the greater the
age. The expression
birth=min(birth)
is evaluated
for each row in the table. When the minimum birthdate is found, the
expression becomes true and the row is included in the output.
proc sql;
title 'Oldest Employee of Each Gender';
select *
from proclib.payroll
group by gender
having birth=min(birth);
Note:
This query involves remerged data because the values returned by a
summary function are compared to values of a column that is not in
the GROUP BY clause. See
Remerging Data for more information about summary
functions and remerging data.