Output Statistics Using
PROC MEANS
We are here to help you... use this series of class-tested tutorials to learn about SAS.
See www.stattutorials.com/SASDATA for files mentioned in this tutorial © TexaSoft, 2007-13
Using PROC MEANS to output statistics
Suppose you have a data set and you want to add a column containing a z-statistic based on the mean and standard deviation of a variable. Here is one way to do that. The following data set contains weights of 12 children. You want to add a column of the difference of the scores from the mean based on a the information in the WEIGHT variable. For good measure also calculate the z-score. (PROCMEANS5.SAS)
DATA WT; INPUT WEIGHT; DATALINES; 64 71 53 67 55 58 77 57 56 51 76 68 ; PROC MEANS NOPRINT DATA=WT;VAR WEIGHT;OUTPUT OUT=WTMEANS MEAN=WTMEAN STDDEV=WTSD; RUN; DATA WTDIFF;SET WT; IF _N_=1 THEN SET WTMEANS; DIFF=WEIGHT-WTMEAN; Z=DIFF/WTSD; * CREATES STANDARDIZED SCORE (Z-SCORE); RUN; ODS RTF; PROC PRINT DATA= WTDIFF;VAR WEIGHT DIFF Z; RUN; ODS RTF CLOSE;
The statement
OUTPUT OUT=WTMEANS MEAN=WTMEAN STDDEV=WTSD;
Creates a SAS data file containing a single record with variables WTMEAN and WTSD (and some other system variables.) You can then use that information to calculate the desired values, as is done in the code:
DATA WTDIFF;SET WT; IF _N_=1 THEN SET WTMEANS; DIFF=WEIGHT-WTMEAN; Z=DIFF/WTSD; * CREATES STANDARDIZED SCORE (Z-SCORE); RUN;
The first SET statement (SET WT) reads in the entire WT data set. The statement
IF _N_=1 THEN SET WTMEANS;
Reads in the first (and only) record from the WTMEANS data set and merges the WTDIFF and WTSD (and a couple of other system variables) into the new WTDIFF data set, allowing you to do the calculations to come up with the DIFF and Z values.
The resulting data set contains the following information
Obs |
WEIGHT |
DIFF |
Z |
1 |
64 |
1.25 |
0.13910 |
2 |
71 |
8.25 |
0.91808 |
3 |
53 |
-9.75 |
-1.08501 |
4 |
67 |
4.25 |
0.47295 |
5 |
55 |
-7.75 |
-0.86244 |
6 |
58 |
-4.75 |
-0.52859 |
7 |
77 |
14.25 |
1.58578 |
8 |
57 |
-5.75 |
-0.63988 |
9 |
56 |
-6.75 |
-0.75116 |
10 |
51 |
-11.75 |
-1.30757 |
11 |
76 |
13.25 |
1.47450 |
12 |
68 |
5.25 |
0.58424 |
NOTE: You could also get standardized values using PROC STANDARD.
PROC STANDARD DATA=WT MEAN=0 STD=1 OUT=ZSCORES; VAR WEIGHT; RUN; PROC PRINT DATA=ZSCORES; RUN;
NOTE:
It is also possible to capture statistics from PROC MEANS output tables using ODS. See ODS tutorials for more info.
End of Tutorial