Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 37 ยท Procedures: PROC

PROC MEANS and PROC SUMMARY

Summarize numeric variables with PROC MEANS and PROC SUMMARY: statistics keywords, CLASS vs BY, the OUTPUT statement, _TYPE_ and _FREQ_, NWAY and missing handling, with a Python stand-in.

PROC MEANS computes descriptive statistics for numeric variables: count, mean, standard deviation, minimum and maximum, and many more. PROC SUMMARY does the same calculations, but does not print anything unless you ask. Together they are the workhorses of SAS reporting, because they can compute statistics for whole groups and write the results to a data set.

SAS cannot run on this site; sas blocks are read-only. The TryIts are Python stand-ins that reproduce the arithmetic on small lists so you can check the numbers yourself.

The simplest call

SAS
proc means data=work.sales;
run;

With no other statements, SAS summarizes every numeric variable and prints N, Mean, Std Dev, Minimum and Maximum. Normally you narrow it down:

SAS
proc means data=work.sales n mean median min max maxdec=1;
  var amount quantity;
run;
  • Statistic keywords go on the PROC line: n, nmiss, mean, median, std, var, min, max, range, sum, stderr, cv, q1, q3, p90, lclm, uclm, and so on.
  • var lists the analysis variables.
  • maxdec=1 limits the printed decimals.

If you name any statistic, only those are printed. The default five appear only when you name none.

CLASS and BY

To summarize per group, use class or by:

SAS
proc means data=work.sales mean sum;
  class region product;
  var amount;
run;

class does not require sorted data and produces a combined table; by needs a prior PROC SORT and prints one separate report per group. For most reporting tasks use class. class also gives you statistics for combinations, covered next.

SAS
proc means data=work.sales mean sum nway;
  class region product;
  var amount;
run;

Without nway, SAS reports every level of grouping: overall, by region, by product, and by both. With nway it reports only the finest level (every combination of all class variables).

Stand-in: grouped statistics

Python Stand-in: PROC MEANS with CLASS

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A group with one observation has no standard deviation, so SAS prints it as missing, just as the stand-in shows nan.

The OUTPUT statement

The report is useful to read. For programming, save the statistics as a data set:

SAS
proc means data=work.sales noprint nway;
  class region;
  var amount;
  output out=work.region_totals
         sum=total_amount
         mean=avg_amount
         n=orders;
run;

The output statement writes one row per class combination. The stat=name pairs rename the results, otherwise a statistic would overwrite the variable name. When you list several variables, give one name per variable in order (sum=a_sum q_sum) or use the shortcut / autoname, which creates names like amount_Sum.

Two automatic variables are added:

  • _TYPE_ says which class combination the row belongs to. 0 is the grand total; with two class variables the codes are 0 (overall), 1 (second variable alone), 2 (first alone), 3 (both). With nway, only the highest type remains.
  • _FREQ_ is the number of observations in that group (including those with a missing analysis value).

Because both exist, you can separate subtotals from details in a later DATA step with where _type_ = 3;. Use / levels or the ways statement for more readable control.

PROC MEANS vs PROC SUMMARY

PROC MEANS PROC SUMMARY
Prints a report by default Yes No (unless print is given)
Typical use Looking at numbers Creating output data sets

They share the same statements and options. Many programmers write proc means ... noprint; for the second job; either is fine.

Missing values and weights

PROC MEANS ignores missing values in the analysis variable when computing mean, sum, std, and so on, and reports how many it skipped with nmiss. A missing value in a class variable removes that row from the report unless you add missing to the PROC statement, in which case missing becomes its own group. That is another reminder to check data quality first, as you did with PROC FREQ in PROC FREQ and Frequency Tables.

SAS
proc means data=work.sales n nmiss mean missing;
  class region;
  var amount;
  weight units;       /* weighted mean: each row counts units times */
run;

weight gives a weighted mean and weighted standard deviation. freq instead treats the variable as a count of identical rows, which also changes N.

Stand-in: an OUTPUT data set with TYPE and FREQ

Python Stand-in: OUTPUT with _TYPE_ and _FREQ_

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This models how a single output statement hands you the grand total, each margin and every combination at once, and why filtering on _TYPE_ is the standard way to pick the level you want.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main difference between PROC MEANS and PROC SUMMARY?

    They calculate the same statistics; PROC SUMMARY needs the PRINT option to display results.
  2. 2. Which statement makes PROC MEANS compute statistics per group without requiring sorted data?

    CLASS groups the data without prior sorting; BY needs sorted input.
  3. 3. What does the NWAY option do?

    NWAY drops the subtotal and grand-total rows, leaving only the finest level.
  4. 4. In an OUTPUT data set, what does FREQ hold?

    FREQ is the observation count of the group; TYPE is the code for the combination.
  5. 5. A missing value in the analysis variable is:

    Statistics use only nonmissing values; NMISS tells you how many were skipped.

Summary

  • PROC MEANS computes statistics for numeric variables; name the statistics you need on the PROC line and the variables in var.
  • class groups without sorting; by needs sorted data; nway keeps only the finest combination.
  • output out= with stat=name pairs saves results; _TYPE_ and _FREQ_ describe each row.
  • PROC SUMMARY is the quiet twin, perfect for creating data sets.
  • Missing analysis values are skipped; missing class values need the missing option to appear.

Next: PROC FORMAT and Custom Formats