8.2 Descriptive Statistics with PROC MEANS & PROC SUMMARY
Key Takeaways
- PROC MEANS prints summary statistics to the output window by default, whereas PROC SUMMARY produces no printed output unless the PRINT option is specified.
- When no statistic keywords are specified, PROC MEANS calculates N, MEAN, STD (Standard Deviation), MIN, and MAX for all numeric variables.
- The CLASS statement performs subgroup analysis without requiring pre-sorted data, while the BY statement requires data sorted by BY variables.
- The OUTPUT OUT= statement creates a SAS dataset containing summary statistics, automatically creating _TYPE_ and _FREQ_ variables when CLASS is used, and its AUTONAME option names the result variables by combining the analysis variable with the statistic keyword (e.g., Sales_Mean).
- WAYS n restricts OUTPUT OUT= to n-way CLASS combinations and NWAY keeps only the fully crossed level, replacing manual _TYPE_ filtering.
8.2 Descriptive Statistics with PROC MEANS & PROC SUMMARY
Descriptive statistical analysis is a core data reporting task. In SAS, PROC MEANS and PROC SUMMARY are sister procedures designed to compute summary statistics for numeric variables across an entire dataset or within specific categorical subgroups. Both procedures share virtually identical syntax and analytical engines, but differ in their default output behavior.
1. PROC MEANS vs. PROC SUMMARY: The Key Difference
Understanding the fundamental distinction between PROC MEANS and PROC SUMMARY is a frequent SAS certification topic:
- PROC MEANS: Prints descriptive statistics to the active output destination (Listing, HTML, PDF) by default. To suppress printed output, you must explicitly add the
NOPRINToption. - PROC SUMMARY: Computes identical statistics but produces NO printed output by default. To print output from PROC SUMMARY, you must explicitly include the
PRINToption on the procedure statement.
/* PROC MEANS prints output automatically */
proc means data=sashelp.shoes;
var Sales;
run;
/* PROC SUMMARY computes statistics silently (typically paired with OUTPUT OUT=) */
proc summary data=sashelp.shoes;
var Sales;
output out=work.sales_summary mean=AvgSales;
run;
/* PROC SUMMARY with PRINT option behaves like PROC MEANS */
proc summary data=sashelp.shoes print;
var Sales;
run;
2. Default Statistics vs. Explicit Keywords
When PROC MEANS is executed without specifying statistic keywords, SAS computes a standard set of five default statistics for every numeric variable in the VAR statement (or all numeric variables in the dataset if VAR is omitted):
N: Number of non-missing values.MEAN: Arithmetic average.STD: Standard deviation.MIN: Minimum value.MAX: Maximum value.
Specifying Explicit Statistics Keywords
If you list any statistic keyword on the PROC MEANS statement line, SAS overrides the five defaults and calculates ONLY the statistics you explicitly requested!
/* Requests ONLY N, NMISS, MEAN, and MEDIAN */
proc means data=sashelp.shoes n nmiss mean median maxdec=2;
var Sales Returns;
run;
Frequently Tested Statistic Keywords:
| Keyword | Statistical Description |
|---|---|
N | Count of non-missing observations |
NMISS | Count of missing numeric observations |
MEAN | Arithmetic mean |
STD | Sample standard deviation |
MIN / MAX | Minimum / Maximum values |
SUM | Sum total of values |
MEDIAN | 50th percentile (middle value) |
P1, P5, P10, P25, P50, P75, P90, P95, P99 | Specific percentile values (e.g., P25 = 25th percentile / lower quartile) |
QRANGE | Interquartile range (P75 minus P25) |
CLM | 95% two-sided confidence limit for the mean |
STDERR | Standard error of the mean |
VAR | Sample variance |
3. Options on the Procedure Statement
MAXDEC=n: Specifies the maximum number of decimal places (nfrom 0 to 8) to display in printed statistics.NONOBS: Suppresses the display of the total observation count column in printed output.NOPRINT: Suppresses all printed output (used primarily when creating output datasets viaOUTPUT OUT=).DATA=dataset: Identifies the input dataset.
4. Subgroup Analysis: CLASS vs. BY Statements
Both CLASS and BY statements allow you to analyze data within categorical subgroups, but their underlying execution mechanics differ fundamentally.
| Feature | CLASS Statement | BY Statement |
|---|---|---|
| Sorting Requirement | NO pre-sorting required! SAS processes unsorted data using hash tables. | MANDATORY pre-sorting! Input dataset MUST be sorted by BY variables beforehand. |
| Output Display | Consolidates all subgroups into a single readable summary table. | Produces separate, individual summary tables with distinct headers for each BY group. |
| Output Dataset Structure | Creates _TYPE_ and _FREQ_ automatic variables representing hierarchical combinations. | Does NOT create _TYPE_ variable combinations; outputs distinct rows per BY group. |
| Performance | Highly efficient for high-cardinality categorical variables. | Best when data is already sorted or when separate physical output sections are required. |
/* CLASS Example - Unsorted Data OK */
proc means data=sashelp.shoes maxdec=2;
class Region Product;
var Sales;
run;
/* BY Example - Pre-sorting Mandatory */
proc sort data=sashelp.shoes out=work.shoes_sorted;
by Region;
run;
proc means data=work.shoes_sorted maxdec=2;
by Region;
var Sales;
run;
5. Output Datasets: The OUTPUT OUT= Statement
The OUTPUT OUT= statement directs SAS to write computed statistics into a new SAS dataset.
proc means data=sashelp.shoes noprint;
class Region;
var Sales Returns;
output out=work.shoes_summary
mean=AvgSales AvgReturns
sum=TotalSales TotalReturns;
run;
Syntax Rules for OUTPUT OUT=:
OUT=dataset-name: Names the destination output dataset.statistic=new-variable-list: Maps statistics to output variable names. The order of variables innew-variable-listmatches the order of variables in theVARstatement.- The
AUTONAMEOption: When added to theOUTPUTstatement, SAS automatically creates output variable names by combining the original variable name with the statistic keyword name, eliminating the need to type manual variable lists.
proc means data=sashelp.shoes noprint;
class Region;
var Sales Returns;
output out=work.shoes_autonamed
mean= sum= / autoname;
run;
/* Creates variables: Sales_Mean, Returns_Mean, Sales_Sum, Returns_Sum */
Automatic Variables in Output Datasets: _TYPE_ and _FREQ_ when using CLASS
When a CLASS statement is used with OUTPUT OUT=, SAS creates two automatic variables in the output dataset:
_FREQ_: Contains the number of observations included in that specific subgroup calculation._TYPE_: A numeric variable indicating the combination ofCLASSvariables used to aggregate the statistics:_TYPE_is a binary code: withCLASS A B;, the leftmost CLASS variable owns the high-order bit._TYPE_ = 0: Grand total across all observations (no CLASS variable used for grouping)._TYPE_ = 1: Statistics grouped by B only (the last CLASS variable listed)._TYPE_ = 2: Statistics grouped by A only (the first CLASS variable listed)._TYPE_ = 3: Statistics grouped by A and B together (every CLASS variable).
Restricting the Aggregation Levels with WAYS and NWAY
By default OUTPUT OUT= writes every _TYPE_ combination, which is 2^n rows-worth of levels for n CLASS variables. Two mechanisms narrow that down:
WAYS n;— a statement that requests only the n-way combinations.ways 1;keeps single-variable summaries;ways 0 2;keeps the grand total plus every two-variable combination.NWAY— an option on thePROC MEANSstatement that keeps only the highest-order combination (all CLASS variables crossed), equivalent to the maximum_TYPE_.
/* Only the one-way summaries: Region alone, then Product alone */
proc means data=sashelp.shoes noprint;
class Region Product;
ways 1;
var Sales;
output out=work.oneway_stats sum=TotalSales;
run;
/* Only the fully crossed Region * Product cells */
proc means data=sashelp.shoes nway noprint;
class Region Product;
var Sales;
output out=work.crossed_stats sum=TotalSales;
run;
/* Example of filtering specific aggregation level in DATA step */
data work.region_totals_only;
set work.shoes_summary;
if _type_ = 1; /* Keeps only the level-1 CLASS summaries */
run;
What default statistics are printed by PROC MEANS when NO statistics keywords are specified on the PROC MEANS statement?
A programmer runs PROC MEANS with a BY statement, but the step fails. What step must be performed prior to running PROC MEANS with a BY statement?
How does PROC SUMMARY differ fundamentally from PROC MEANS in default output generation?
A programmer uses the following OUTPUT statement in PROC MEANS: output out=work.summary_stats mean= sum= / autoname; If the VAR statement lists Sales and Profit, what variable names will be created in work.summary_stats for the mean values?