Docsity

PROC SUMMARY in SAS: An Overview of Statistical Analysis and Output Customization, Study notes of Statistics

Toggle sidebar
bg1
Beginning Tutorials
PRoe
SUMMARY: A POWERFUL EXPLORATORY DATA ANALYSIS TOOL
Kim L Kolbe Ritzow
Systems Seminar Consultants, Kalamazoo,
MI
Abstract
Starting with Version 6
of
SASe
Software, PROC
SUMMARY and PROC MEANS are virtually identical
except
for
two
differences. One, PROC MEANS prints
a report
by
default, whereas PROC SUMMARY does
not. And two, PROC SUMMARY requires the
OUTPUT statement, whereas MEANS does not.
These procedures allow us
to
explore
our
data not
only in terms
of
counts
and
distributions,
but
also
statistically. Both procedures
now
support the CLASS
statement (this is new
to
PROC MEANS starting with
Version 6). The CLASS statement acts much like the
BY,
only it
does
not require sorting and in
most
cases
is more effiCient than the BY statement.
For
the purpose
of
this paper, the examples will use
PROC SUMMARY. Keep in mind, however, PROC
MEANS could have been used instead and it would
produce
the
exact same results.
Background
The
syntax
of
PROC SUMMARY is quite extensive.
The following is a partial list
of
some
of
the statements
and options available
on
PROC SUMMARY:
PROC SUMMARY DATA=SASdataset options;
Options
(partial list):
MISSING
NWAY
DESCENDING
ORDER=
-Treat missing values as a
valid subgroup value
-Calculate
only
highest level
of
interaction
-Arrange lowest summary
levels first (by default they
are arranged ascending)
-Specify sort order
of
CLASS variables
Optional
statements
(partial
list):
CLASS variable(s);
VAR variable(s);
BY variable(s);
WHERE where condition(s);
OUTPUT
OUT=
SASdataset options=variable(s);
198
Available
options
on
the
OUTPUT
OUT=:
N
NMISS
PRT
VAR
MEAN
RANGE
CSS
MIN
SKEWNESS
STD
USS
CV
SUMWGT
MAX
KURTOSIS
STDERR
SUM
T
Aggregating
Data
with
PROC SUMMARY
The CLASS statement in PROC SUMMARY names
the
character
or
numeric variables in which the data will
be CLASSified upon. The variables listed on
the
CLASS
statement should be categorical variables, that
is, they should have a few number
of
discrete values.
The
VAR
statement on the other hand, names the
numeric variables which will
be
analyzed. The
OUTPUT OUT = statement names
the
output SAS data
set.
It
also defines which statistics will be used with
which variables and what they will be called on the
output SAS data set.
PROC
SUMMARY
DATA=MKTDATA;
CLASS STNORE STRCTYP;
VAR
FRCSLVL
MKTVL;
OUTPUT
OUT=SUMDS
SUM (MKTVL)
=TOTMKTVL
MEAN(FRCSLVL) = AVGFORCL
MIN(MKTVL) = MINMKTVL
MAX(MKTVL) = MAXMKTVL;
RUN;
PROC PRINT
DATA=SUMDS
NOOeS;
TmE
'RESULT
OF
PROC
SUMMARY';
RUN; (See Output
#1
tor results)
As
variables are added
to
the CLASS statement, the
number
of
resulting TYPE values will
grow
exponentially.
--
How
are
the
TYPE Values
Useful?
The TYPE variable
is
a part
of
the
output SAS data
set built (meaning if you did a PROC CONTENTS
you
would see it
on
there). This also means that you can
query against this variable's value
and
use it
to
create
different reports, each
of
which contain different
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download PROC SUMMARY in SAS: An Overview of Statistical Analysis and Output Customization and more Study notes Statistics in PDF only on Docsity!

PRoe SUMMARY: A POWERFUL EXPLORATORY DATA ANALYSIS TOOL

Kim L Kolbe Ritzow

Systems Seminar Consultants, Kalamazoo, MI

Abstract Starting with Version 6 of SASe Software, PROC SUMMARY and PROC MEANS are virtually identical except for two differences. One, PROC MEANS prints a report by default, whereas PROC SUMMARY does not. And two, PROC SUMMARY requires the OUTPUT statement, whereas MEANS does not.

These procedures allow us to explore our data not only in terms of counts and distributions, but also statistically. Both procedures now support the CLASS statement (this is new to PROC MEANS starting with

Version 6). The CLASS statement acts much like the

BY, only it does not require sorting and in most cases is more effiCient than the BY statement.

For the purpose of this paper, the examples will use PROC SUMMARY. Keep in mind, however, PROC MEANS could have been used instead and it would produce the exact same results.

Background The syntax of PROC SUMMARY is quite extensive.

The following is a partial list of some of the statements

and options available on PROC SUMMARY:

PROC SUMMARY DATA=SASdataset options;

Options (partial list):

MISSING

NWAY

DESCENDING
ORDER=
  • Treat missing values as a valid subgroup value
  • Calculate only highest level of interaction
  • Arrange lowest summary levels first (by default they are arranged ascending)
  • Specify sort order of CLASS variables

Optional statements (partial list): CLASS variable(s); VAR variable(s); BY variable(s); WHERE where condition(s); OUTPUT OUT= SASdataset options=variable(s);

Available options on the OUTPUT OUT=:

N
NMISS
PRT
VAR
MEAN
RANGE
CSS
MIN
SKEWNESS
STD
USS
CV
SUMWGT
MAX
KURTOSIS
STDERR
SUM
T

Aggregating Data with PROC SUMMARY The CLASS statement in PROC SUMMARY names the character or numeric variables in which the data will be CLASSified upon. The variables listed on the CLASS statement should be categorical variables, that is, they should have a few number of discrete values. The VAR statement on the other hand, names the numeric variables which will be analyzed. The

OUTPUT OUT = statement names the output SAS data

set. It also defines which statistics will be used with which variables and what they will be called on the output SAS data set.

PROC SUMMARY DATA=MKTDATA; CLASS STNORE STRCTYP; VAR FRCSLVL MKTVL; OUTPUT OUT=SUMDS SUM (MKTVL) =TOTMKTVL MEAN(FRCSLVL) = AVGFORCL MIN(MKTVL) = MINMKTVL MAX(MKTVL) = MAXMKTVL; RUN;

PROC PRINT DATA=SUMDS NOOeS; TmE 'RESULT OF PROC SUMMARY'; RUN;

(See Output #1 tor results)

As variables are added to the CLASS statement, the number of resulting TYPE values will grow exponentially. --

How are the TYPE Values Useful? The TYPE variableis a part of the output SAS data set built (meaning if you did a PROC CONTENTS you would see it on there). This also means that you can query against this variable's value and use it to create different reports, each of which contain different

:information, with different levels of detail and summarization.

Interpreting _TYPE_values:

TYPE =
TYPE =
TYPE =
TYPE =

Represents the entire data set (grand total- company overall) Represents STRCTYP (across all STNOREs) Represents STNORE (across all STRCTYPs) Represents STRCTYPs within STNOREs

After a PROC SUMMARY, a series of PROC PRINTs could be coded to select off different TYPE values and to create several different reportS each with a different level of information:

PROC PRINT DATA=SUMDS(DROP= STNORE STRCTYP fREQJ; WHERE TYPE = 0; TITLE1"CEO'S-REPORT"; TITLE2 "COMPANY OVERALL"; VAR TOTMKTVL AVGFORCL MINMKTVL MAXMKTVL; RUN;

PROC PRINT DATA=SUMDS(DROP= STRCTYP _FREQJ; WHERE TYPE = 2; T1TLE1 "REGIONAL MANAGER'S REPORT"; TITLE2 ·STATE TOTALS"; VAR STNORE TOTMKTVL AVGFORCL MINMKTVL MAXMKTVL; RUN;

PROC PRINT DATA=SUMDS(DROP= _FREQJ; WHERE TYPE = 3; T1TLE1 "DETAILED ANALYSIS REPORT"; VARSTNORESTRCTYPTOTMKTVL AVGFORCL MINMKTVL MAXMKTVL; RUN; (See Output #2 for results)

Using TYPE Values In a DATA Step TYPE- values are not only useful for querying against-in a PROC, but can also be used in a DATA step to help create percentages.

DATA PERCENT;
SET SUMDS(KEEP= TOTMKTVL TYPE
STNORE STRCTYP
WHERE=LTYPE_IN (0,3)));
IF TYPE = 0 THEN
DO; -

GTOTMKT=TOTMKTV~ RETAIN GTOTMKT; DELETE; END; ELSE

PCTMKT=(TOTMKTVL/GTOTMKT) * 100;

DROP TYPE;
RUN; - -
PROC PRINT DATA=PERCENT;
T1TLE1 'RESULT OF USING TYPE VALUES';
T1TLE2 'IN THE DATA STEP;; -
RUN;

(See Output #3 for results)

Figuring Out TYPE Values There are three waysto see your _TYPE_values:

PROC PRINT the resulting output SAS data set (if you have several CLASS variables this may be A LOT of output)

Manipulate the data to print only the first value of each TYPE variable, which may be enough to figure oUt which TYPE values you need (be careful of doing this if you have used the MISSING option on the PROC SUMMARY statement)

Reference the SAS manual for the formula used in calculating the _TYPE_values

DATA FIRSTONE; SETSUMDS; BY TYPE; IF FIRST. TYPE ; RUN; - -

PROC PRINT DATA=FIRSTONE;
T1TLE1 'PRINTING ONLY THE FIRST';
T1TLE2 'OCCURRENCE OF EACH TYPE ';
T1TLE3 'VALUE'; --
RUN;

(See Output #4 for results)

PROC PRINT DATA=SUMDS;
TITLE 'WITHOUT USING THE CLASS';
RUN;
(See Output #8 for results)

Because a VAR statement is being used different statistics c,!n be specified on the OUTPUT SAS data set.

Formatting CLASS Variables User-defined formats can be used when CLASSifying data.

PROC FORMAT;
VALUE $ STNOFMT
'AK','AZ','CA','CO','NM',
'NV','OR','WA' = 'WEST'
'AL','FL','GA','KY','MO',
'OK','SC','TN','VA','WV' ='SOUTH'
'CT','DC','DE','MA','MD',
'NC','NH','NJ','NY','PA',
'RI' ='EAST
'IL', 'IN' ,'KS' ,'MI' ,'MN',
'NE' ,'OH', 'WI' = 'MIDWEST;
RUN;
PROC SUMMARY DATA=MKTDATA NWAY;
CLASS STNORE STRCTYP;
FORMAT STNORE $STNOFMT.;

VAR FRCSLVL MKlVL; OUTPUT OUT=SUMDS SUM(MKTVL) =TOTMKTVL MEAN(FRCSLVL) =AVGFORCL MIN (MKTVL) =MINMKlVL MAX(MKTVL) = MAXMKlVL; RUN;

PROC PRINT DATA=SUMDS; TITLE 'FORMATTING A CLASS VARIABLE'; RUN;

(See Output #9 for results)
You want to be careful of formatting CLASS variables

if you are going to tum around later on and check for the formatted value (i.e. WHERE STNORE='EAST';), because remember that formats DO NOT change the value of the variable, they simply change their appearance. It may be better to create a variable in a DATA step prior to PROC SUMMARY that physically contains the formatted value by using the PUT function (FSTNORE=PUT(STNORE,$STNOFMT.);) and use that on your CLASS statement instead.

Also notice that the NWAY option was added to the PROe SUMMARY statement. The NWAY option eliminates intermediate levels of summarization- in this case we only got the TYPE =3 records.

You can format CLASS variable regardless if the NWAY option is being used or not.

Creating Flat Files Some times after the data has been aggregated you may want to take the result of that summarization and

put it into a flat file so it could be downloaded to

another platform and imported in to another software product. It is very easy to take the results of PROe SUMMARY and create flat file if you know what you are doing:

PROC SUMMARY DATA=MKTDATA NWAY; CLASS STNORE STRCTYP; VAR FRCSLVL MKTVL; OUTPUT OUT= SUMDS SUM(MKlVL) =TOTMKTVL MEAN(FRCSLVL) = AVGFORCL MIN(MKTVL) =MINMKlVL MAX(MKTVL) =MAXMKTVL; RUN;

FILENAME OUTFILE 'path-name';

DATA NULL; SET SUMDS(DROP= _TYPE __FREQj; FILE OUTFILE NOTITLES; PUT @1 STRCTYP $2. @5 STNORE $2. @10TOTMKTVL 8. @20AVGFORCL 10. @35 MINMKTVL 7. @45 MAXMKTVL 10.; (See Output #10 for results)

Always use DATA NULL when creating flat files. This prevents a SAS data set from being built. The only reason to ever build a SAS data set is so that you can pass it on to another DATA or PROe step for further processing. You usually buDd a flat file as a last step within your program, so there is no need to build a SAS data set. Remember, DATA; does not prevent a data set from being built, SAS builds one for

us, but it chooses the name. The ONLY way to

prevent a data set from being built is by saying DATA NULL.

Both SAS-defined and user-defined formats can be used on the PUT statement.

The example above will create a fixed column. blank delimited fde to the location specified on the FILENAME statement (depending on your platform you could also reference the name specified on the FILE statement in JCL. ASSIGN statements. ALLOCates. etc.).

Creating a Flat File to Import Into Lotus Character literals can be specified on the PUT statement. which is useful in creating a delimited file that Lotus can import. This technique can be used to create flat fHes that are delimited by characters other than blanks. Check the reference manual of the

product you wHI be importing Into under "Importing
Files· to determine what type of delimited file the

particular product can read most easily. Use that

delimiter in place of the delimiters used in the

example below.

PROC SUMMARY DATA=MKTDATA NWAY; CLASS STNORE STRCTYP; VAR FRCSlVl MKTVL; OUTPUT OUT= SUMDS SUM(MKTVl) =TOTMKTVl MEAN (FRCSLVL) =AVGFORCl MIN(MKTVL) =MINMKTVl MAX(MKTVL) = MAXMKTVL; RUN;.

FILENAME OUTFILE 'path-name';
DATA NULL:

SET SUMDS(DROP= _TYPE_fREQ.J: FILE OUTFILE NOTITLES: PUT ••, STRCTYP ..... STNORE ...... TOTMKTVl·· .. • AVGFORCL ,. (^) , ot RUN:

MINMKTVL ,. ,'" MAXMKTVL '.';
(See Output #11 for results)

Note that SAS writes out a blank after PUTting out the

value of a variable. We can solve this by "tricking"
SAS into thinking It has a decremental pointer:
DATA NUU;

SET SUMDS(DROP= _TYPE __FREQj: FILE OUTFILE NOTITLES: PUT ••• STRCTYP +(-1) '",.. STNORE +(-1)

"',n' TOTMKTVL +(-1) ''',.' AVGFORCL +(-1)

''',.. MINMKTVL +(-1) '.,.' MAXMKTVL +(-1)
.... ;RUN;
(See Output #12 for results)

Another way of doing it would be to assign constants rather than literals:

DATA NUU: SET SUMDS(DROP= _TYPE __ FREQj: RETAIN DQUOTE'" COMMA ','; FILE OUTFILE NOTITLES: PUT DQUOTE STRCTYP DQUOTE COMMA DQUOTE STNORE DQUOTE COMMA DQUOTE TOTMKTVL DQUOTE COMMA DQUOTE AVGFORCL DQUOTE COMMA OQUOTE MINMKTVL DQUOTE COMMA DQUOTE MAXMKTVL DQUOTE:RUN:

(See Output #13 for results)

SAS PUTs a blank after character constants as well, so the decrernental pointer used In the previous example will need to be used again:

DATA NULL: SET SUMDS(DROP= _TYPE__FREQj: RETAIN DQUOTE '"' COMMA ',': FILE OUTFILE NOTITLES; PUT DQUOTE +(-1) STRCTYP +(-1) DQUOTE +(-1) COMMA +(-1) DQUOTE +(-1) STNORE +(-1) DQUOTE +(-1) COMMA +(-1) DQUOTE +(-1) TOTMKTVL +(-1) DQUOTE +(-1) COMMA +(-1) DQUOTE +(-1) AVGFORCL +(-1) DQUOTE +(-1) COMMA +(-1) DQUOTE +(-1) MINMKTVL +(-1) DQUOTE +(-1) COMMA +(-1) DQUOTE +(-1) MAXMKTVL +(-1) DQUOTE; RUN:

(See Output #14 for results)

In Summary

As we have seen through the course of this paper,

PROC SUMMARY is a very powerful exploratory data analysis tool. It allows us to explore our data both statistically as well as in terms of how our data values are distributed in a single PROC step.

We also have seen how we can post-process the output SAS data set from PROC SUMMARY in DATA

or PROC steps to produce various types of reports

and flat files which can then be downloaded and imported into any software product.

Partial Output:

RESULT OF PROC SUMMARY
STNORE STRCTYP TYPE FREQ_ TOTMKTVL AVGFORCL MINMKTVL MAXMKTVL
NA 1 108 11824700 97274.31 27000 350000
ND 1 2109 268794599 113624.23 15000 3300002
YA 1 62 6089850 89167.13 25500 235000
••••••••••.•••••••••••••••••••••••••••••••• ETC •••••••••••••••••••••••••••••••••••••
AK
AL

AZ

2 2 2

1 17 40

•••••••••.••••••••••••••••••••••••••••••••• ETC •.•.••••••••.••••••••••••••••••••••••
AK
AL
AL
NA
NA
NO

3 3 3

1 1 9

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •. .• ETC
CEO'S REPORT- COMPANY OVERALL
CBS TOTMKTVL AVGFORCL MINMKTVL MAXMKTVL

OUtput fI2.

Partial Output:

REGIONAL MANAGER'S REPORT- STATE TOTALS
OBS STNORE TOTMKTVL AVGFORCL MINMKTVL MAXMKTVL
9 AK 46000 36800.00 46000 46000
10 AL 1081600 56213.00 39000 137500
11 AZ 3905000 89237.50 35000 358000
12 CA 116594652 1714BB.64 25000 3300002
13 CO 4858660 98243.09 30000 319000
•••••••••••••••••••••••••• ETC •••••••••••••••••••••••••••.••••.•

OUtput fI2.

Partial Output:

OBS 1 2 3 4

DETAILED ANALYSIS REPORT OBS STNORE STRCTYP TOTMKTVL AVGFORCL MINMKTVL MAXMKTVL 46 AK NA 46000 36800.00 46000 46000 47 AL NA 42000 35000.00 42000 42000 48 AL NO 548000 56435.67 39000 135000 49 AL YA 52000 42000.00 52000 52000 •••••••••••••••••••••••••• ETC ••••••••••••••••••••••••••••••••••

OSS STNORE

OUtput tIZ

RESULT OF USING TYPE VALUES IN THE DATA STEP STRCTYP TOTMKTVL (^) GTOTMKT PCTMKT

1 AK NA 46000 371473153 0. 2 AL NA 42000 371473153 0. 3 AL ND 548000 371473153 0. 4 AL YA 52000 371473153 0. 5 AL YD 394600 371473153 0.

  • • • • • • • • • • • • • • • • • • • • • •• ETC ••••••••••••••••••••••••••••••• OUtput iI.

PRINTING ONLY THE FIRST OCCURRENCE OF EACH TYPE VALUE STNORE STRCTYP ^ TYPE^ FREQ_^ TOTMKTVL AVGFORCL MINMKTVL

AK AK

NA NA

0 1 2 3

3034 108 1 1

371473153 109400.72 10000 11824700 97274.31 27000 46000 36800.00 46000 46000 36800.00 46000

MAXMKTVL 3300002 350000 46000 46000

Partial Output:

RESULT OF IMPLICIT OUTPUT STATEMENT

OBS STNORE TOTFORCL TOTMKTVL^ AVGFORCL 1 0 3034 331046583 371473153 109400. 2 NA 1 108 10505625 11824700 97274. 3 NO 1 2109 239633502 268794599 113624. 4 YA 1 62 5528362 6089850 89167. •••••••••••••••••••••••••••••••••••••••••••• ETC ••••••••••••••••••••••••••••••••• 9 AK 2 1 36800 46000 36800. 00 10 AL 2 17 955621 1081600 56213. 11 A2 2 40 3569500 3905000 89237.

  • • • • • • • • • • • • • • • • • •• ••• • ••• • • • • • • • • • • • • • • •• •• ETC ••••••••••••••••••••••••••••••••• 46 AK NA 3 1 36800 46000 36800. 47 AL NA 3 1 35000 42000 35000. 48 AL NO 3 9 507921 548000 56435.
  • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •• ETC •••••••••••••••••••••••••••••••••

OUtput IS

Partial Output:

FORMATTING A CLASS VARIABLE
OBS STNORE TOTMKTVL AVGFORCL MINMKTVL MAXMKTVL
1 WEST NA 3 25 3808000 139096.00 44500 350000
2 WEST NO 3 626 110375052 157244.74 26000 3300002
3 WEST YA 3 19 2440250 120378.95 57250 235000
•••••••••••••••.•..••••••••••••••••••••••••••••• ETC ••••••••••••••••••••••••••••••••.•••••••

output fJ

Partial Listing of the Flat File Created:

NA AK 46000 36800 46000 NA AL 42000 35000 42000 NO AL 548000 56436 39000 135000

  • • •. • • • •. •.. • • •. • • • •• ETC •••••••••••••••••••••••••••• outplt #

Partial Listing of the Flat File created:

"NA ","AK ","46000 ","36800 ","46000 ","46000 II "NA ","AL ","42000 ","35000 ","42000 ","42000 " "NO ","AL ","548000 ","56435.666667 ","39000 ","135000 II

  • • • • • •. • •. • • • • • • • •• • • •• ETC •••••••••••••••••••••••••••••

Partial Listing of the Flat File Created:

"NA","AK", "46000","36800", "46000","46000" "NA","AL","42000","35000", "42000","42000" "NO","AL","548000" ,"56435.666667","39000", "135000"

  • • • •. • • • • • • • • • •• .• ETC •••••.•••••••••••••••••••••

outplt #

Partial Listing of the Flat File Created:

" NA " , " AK " , " 46000 " , " 36800 " , " 46000 " , " 46000 " " NA " , " AL II , " 42000 " , " 35000 " , " 42000 " , " 42000 " " ND " , " AL " , " 548000 " , n 56435.666667 " , " 39000 " , " 135000 " •••••••.••••.•••••••••••••••••• ETC •••••••••.•••••••••••••••••••••••••••• outplt #

Partial Listing of the Flat File Created:

"NA" ,"AI(" ,"46000" ,"36800" ,"46000" ,"46000" "NA","AL" , "42000" , "35000", "42000", "42000" "ND" ,"AL", "548000" ,"56435.666667" , "39000", "135000"

  • • • • • • • • • • • • • • • • •• ETC •••••.••••••••••••••••••••• output #