






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







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
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.
and options available on PROC SUMMARY:
PROC SUMMARY DATA=SASdataset options;
Options (partial list):
MISSING
NWAY
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=:
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
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;
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:
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.
GTOTMKT=TOTMKTV~ RETAIN GTOTMKT; DELETE; END; ELSE
(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; - -
(See Output #4 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.
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;
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
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
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
particular product can read most easily. Use that
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;.
SET SUMDS(DROP= _TYPE_fREQ.J: FILE OUTFILE NOTITLES: PUT ••, STRCTYP ..... STNORE ...... TOTMKTVl·· .. • AVGFORCL ,. (^) , ot RUN:
Note that SAS writes out a blank after PUTting out the
SET SUMDS(DROP= _TYPE __FREQj: FILE OUTFILE NOTITLES: PUT ••• STRCTYP +(-1) '",.. STNORE +(-1)
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:
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:
In Summary
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
and flat files which can then be downloaded and imported into any software product.
AZ
2 2 2
1 17 40
3 3 3
1 1 9
OUtput fI2.
OUtput fI2.
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.
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
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.
OUtput IS
Partial Output:
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
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
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"
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"