Download presentation
Presentation is loading. Please wait.
Published byHomer Dean Modified over 6 years ago
1
Introduction to SAS Essentials Mastering SAS for Data Analytics
Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward
2
Chapter 19: CREATING CUSTOM REPORTS
SAS ESSENTIALS -- Elliott & Woodward
3
LEARNING OBJECTIVES • To be able to create custom reports using PROC TABULATE • To be able to create custom reports using PROC REPORT • To be able to create a custom report using the DATA step SAS ESSENTIALS -- Elliott & Woodward
4
19.1 USING PROC TABULATE PROC TABULATE is used to make summary data tables and can handle multiple levels in rows and columns and summary statistics. An abbreviated syntax for PROC TABULATE: PROC TABULATE <Options>; CLASS variables </options>; VAR variables </options>; TABLE <page> , <row> , column < / options> ... Other statements; RUN; SAS ESSENTIALS -- Elliott & Woodward
5
Example PROC TABULATE For example, this simple PROC TABULATE statement defines a class variable (GP) and a table. PROC TABULATE DATA=sasdataset; CLASS GP; TABLE GP; RUN; SAS ESSENTIALS -- Elliott & Woodward
6
Table 19.1 Common Options for PROC TABULATE Option Explanation
DATA=dataname Specifies which data set to use. OUT=dataname Specifies an output data set STYLE=styletype Specifies an ODS style format for the table FORMAT= Specify a default overall format for each cell, Best 12.2 is the default. ORDER=option Specifies order that categories are displayed options are UNFORMATTED, DATA, FORMATTED and FREQ. MISSING Specifies that missing values be treated as a category SAS ESSENTIALS -- Elliott & Woodward
7
Table 19.2 Common Statements in PROC TABULATE Statements Explanation
VAR variables; Specifies which variables you will use to calculate statistics. Must be numeric. CLASS variables; Specifies categorical variables. TABLE specification; Identifies the dimensions and other components of the table to create BY, FORMAT, LABEL, WHERE These statements are common to most procedures, and may be used here. SAS ESSENTIALS -- Elliott & Woodward
8
The TABLE statement This statement is the most important component in PROC TABULATE. It is used to define what information will be displayed in the table For example, this simple PROC TABULATE statement defines a class variable (GP) and a table. PROC TABULATE DATA=sasdataset; CLASS GP; TABLE GP; RUN; SAS ESSENTIALS -- Elliott & Woodward
9
The CLASS Statement This statement identifies a categorical variable to be used in a table. You must identify the variables you are going to use before you use them in a TABLE statement The TABLE statement above tells the procedure to create a table using the GP variable. For Example: PROC TABULATE DATA=sasdataset; CLASS VNAMEl VNAME2; TABLE VNAMEl, VNAME2; RUN; Note, no commas between variables in CLASS statement, but commas have meaning in the TABLE statement. SAS ESSENTIALS -- Elliott & Woodward
10
More about the CLASS Statement
Commas separate table dimensions. In the TABLE statement commas separate rows from columns. An asterisk (*) crosses two variables, where categories of the second variable are expanded within the first one. Thus, CLASS VNAMEl VNAME2 VNAME3; TABLE VNAMEl, VNAME2*VNAME3; creates a table where the categories of VNAME1 define rows, VNAME2 defines columns, and VNAME3 defines columns within the categories of VNAME2. SAS ESSENTIALS -- Elliott & Woodward
11
The VAR Statement The VAR statement identifies quantitative (numeric) data. This variable may be used to indicate summary statistics in table cells, such as means, sums, or medians. For example, CLASS VNAMEl VNAME2; VAR OBS; TABLE VNAMEl, VNAME2*MEAN*OBS; The MEAN*OBS addition to this code tells the procedure to place the mean of OBS, broken down by VNAME1 and VNAME2 variables, in each cell. SAS ESSENTIALS -- Elliott & Woodward
12
The Data Set For Upcoming Examples:
SAS ESSENTIALS -- Elliott & Woodward
13
Hands On Example p 434 (ATAB1.SAS)
PROC TABULATE DATA=MYSASLIB.REGION; CLASS TYPE; TABLE TYPE; RUN; Creates a simple table with only one group – TYPE, whose categories are KIOSK and STORE SAS ESSENTIALS -- Elliott & Woodward
14
Modify the code… CLASS TYPE AREA; TABLE TYPE*AREA;
Within TYPE, the data are broken down by AREA, where AREA values are the compass points N, S, E, and W. SAS ESSENTIALS -- Elliott & Woodward
15
Switch Variables to See How That Changes the Output
CLASS TYPE AREA; TABLE AREA*TYPE; Now the table is AREA categories first, then broken down by TYPE categories. SAS ESSENTIALS -- Elliott & Woodward
16
Add a Third Variable to the Table
CLASS TYPE AREA SITE; TABLE SITE*AREA*TYPE; SITE broken down by AREA broken down by TYPE… SAS ESSENTIALS -- Elliott & Woodward
17
Change SITE*AREA to SITE,AREA
CLASS TYPE AREA SITE; TABLE SITE,AREA*TYPE; The comma between SITE and AREA makes SITE into a ROW instead of a COLUMN. SAS ESSENTIALS -- Elliott & Woodward
18
Adding Labels and Formats to Variables
A label can be added to any variable by using an equal sign(=) followed by a quoted string. For example, TABLE SITE="Store Site", AREA="Area"*TYPE="Type Store"; If you also want to format variables, you can create a format definition for N, S, E, and W using PROC FORMAT; VALUE $FMTCOMPASS "N"="l.North" "S"="2.South" "E"="3.East" "W"="4.West"; RUN; Creates a format definition for AREA. SAS ESSENTIALS -- Elliott & Woodward
19
Do Hands On Exercise p 436 (ATAB2.SAS)
PROC FORMAT; VALUE $FMTCOMPASS "N"="1.North" "S"="2.South" "E"="3.East" "W"="4.West"; RUN; PROC TABULATE DATA=MYSASLIB.REGION ORDER=FORMATTED; CLASS TYPE AREA SITE; TABLE SITE="Store Site", AREA="Area"*TYPE="Type Store"; FORMAT AREA $FMTCOMPASS.; Orders by formatted values SAS ESSENTIALS -- Elliott & Woodward
20
Resulting Table Notice how AREA is in order using the Formatted labels defined as $FMTCOMPASS. Take away the ORDER= FORMATTED statement and rerun the code. Note the order in which the AREA categories appear - in alphabetic order according to original values E N S W. SAS ESSENTIALS -- Elliott & Woodward
21
Reporting SUMS and MEANS
To report the total sales, indicate the variable to sum using a VAR statement and cross that value with a category variable in the TABLES statement. For example, VAR SALES_K; TABLE SITE="Store Site", AREA="Area"*TYPE="Type Store" * SALES_ K; SUM is the default statistic calculated for this method, but you can request other statistics. For example to request the mean of SALES_ K, change the statement to AREA="Area"*TYPE="Type Store“ *SALES_K*MEAN; SAS ESSENTIALS -- Elliott & Woodward
22
Available Statistics for TABLE Statement
Table 19.8 Statistics available in PROC Tabulate PCTSUM NMISS P5 COLPCTSUM SUM P10 MAX PAGEPCTSUM P99 ROWPCTN PCTN Q1 | P25 MEAN VAR QRANGE ROWPCTSUM MEDIAN | P50 MIN P1 PROBT STDDEV / STD Q3 | P75 T N P90 STDERR P95 SAS ESSENTIALS -- Elliott & Woodward
23
Do Hands On Exercise p 438 (ATAB3.SAS)
PROC TABULATE DATA=MYSASLIB.REGION ORDER=FORMATTED; CLASS TYPE AREA SITE; VAR SALES_K; TABLE SITE="Store Site", AREA="Area"*TYPE="Type Store“*SALES_K; FORMAT AREA $FMTCOMPASS.; RUN; SAS ESSENTIALS -- Elliott & Woodward
24
Resulting Table (Reporting SUMS)
Notice that the cell contents are SUMS SAS ESSENTIALS -- Elliott & Woodward
25
Add MEAN= and a Label for SALES_K
TABLE SITE="Store Site", AREA="Area"* TYPE="Type Store"* MEAN="Mean Sales $1000"*SALES_K; Notice label here Reported values are now MEANS SAS ESSENTIALS -- Elliott & Woodward
26
Add a Format for SALES_K Means
TABLE SITE="Store Site", AREA="Area"* TYPE="Type Store"* MEAN="Mean Sales $1000" *SALES_K*F=DOLLAR6.2; Mean dollar values are now formatted. SAS ESSENTIALS -- Elliott & Woodward
27
Create a Better Looking PROC TABULATE Table
In the table as created there are six lines of labels before the actual numbers. To get rid of unwanted labels, change the labels in the TABLE statement to two quotation marks ("") as in MEAN= "" and TYPE= "". TABLE SITE="", AREA ="" * TYPE ="" * MEAN="Mean Sales $1000"*SALES_K=""*F=DOLLAR6.2; Notice that "“ takes away unneeded labels in the table. SAS ESSENTIALS -- Elliott & Woodward
28
Resulting in Table that is Easier to Read
With unneeded labels removed. SAS ESSENTIALS -- Elliott & Woodward
29
Do Hands on Example p439 (ATAB4.SAS)
PROC TABULATE DATA=MYSASLIB.REGION ORDER=FORMATTED; CLASS TYPE AREA SITE; VAR SALES_K; TABLE SITE="", (AREA="" ALL)*TYPE="" *MEAN="Mean Sales $1000“ *SALES_K=""*F=DOLLAR6.2; FORMAT AREA $FMTCOMPASS.; RUN; Notice the ALL statement in (AREA=“” ALL) to add another summary column to the table SAS ESSENTIALS -- Elliott & Woodward
30
Notice the added ALL columns
Resulting Table Notice the added ALL columns SAS ESSENTIALS -- Elliott & Woodward
31
Change where (AREA="" ALL) is placed…
TABLE (AREA="" ALL), SITE=""* TYPE=""* MEAN="Sales $1000"*SALES_K*F=DOLLAR6.2; Now AREA and ALL are in the Row position SAS ESSENTIALS -- Elliott & Woodward
32
A Table with Two ALL options
TABLE (AREA="" ALL), (SITE="" ALL)* TYPE=""* MEAN="Sales $1000"*SALES_K=""*F=DOLLAR6.2; ALL here. ALL here SAS ESSENTIALS -- Elliott & Woodward
33
Enhancing Your Table It is fairly easy to add information to your table. For example, suppose you want to report more than one statistic, say N and MEAN. You can combine statistics by placing them in parentheses such as (N MEAN) Or to eliminate the label being put into the table, you could use (N="" MEAN= "") SAS ESSENTIALS -- Elliott & Woodward
34
Do Hands On Example p 442 (ATAB5.SAS)
PROC TABULATE DATA=MYSASLIB.REGION ORDER=FORMATTED; CLASS TYPE AREA SITE; VAR SALES_K; TABLE(AREA="" ALL),(SITE="" ALL) *TYPE="" *MEAN=""*SALES_K=""*F=6.2; FORMAT AREA $FMTCOMPASS.; RUN; SAS ESSENTIALS -- Elliott & Woodward
35
Resulting Table SAS ESSENTIALS -- Elliott & Woodward
36
Change MEAN= "" statement to (N= "" MEAN= "")
Note each column has N and Mean SAS ESSENTIALS -- Elliott & Woodward
37
Add a BOX option to the code
TABLE(AREA="" ALL),(SITE="" ALL)*TYPE="" *(MEAN="" N="")*SALES_K=""*F=6.2 / BOX="Fall Sales"; Label from BOX= Option SAS ESSENTIALS -- Elliott & Woodward
38
Saving Your Table in Excel
Set up PROC output the an ODS location (TAGSETS.EXCEL) ODS TAGSETS.EXCELXP BODY='C:SASDATA\ TABLE.XLS'; ******Your PROC TABULATE code ; RUN ; ODS TAGSETS.EXCELXP CLOSE; After the PROC, close the ODS location SAS ESSENTIALS -- Elliott & Woodward
39
Do Hands On Exercise p 443 ODS TAGSETS.EXCELXP BODY=‘C:\SASDATA\TABLE.XLS'; PROC TABULATE DATA=MYSASLIB.REGION ORDER=FORMATTED; CLASS TYPE AREA SITE; VAR SALES_K; TABLE(AREA="" ALL="By Area"), (SITE="" ALL="By Site")* TYPE=""* (N="" MEAN="")*SALES_K=""*F=6.2 / BOX="Fall Sales"; FORMAT AREA $FMTCOMPASS.; RUN; ODS TAGSETS.EXCELXP CLOSE; SAS ESSENTIALS -- Elliott & Woodward
40
Resulting Table – In Microsoft Excel
SAS ESSENTIALS -- Elliott & Woodward
41
Continue to Chapter 19 – Part 2
SAS ESSENTIALS -- Elliott & Woodward
42
These slides are based on the book:
Introduction to SAS Essentials Mastering SAS for Data Analytics, 2nd Edition By Alan C, Elliott and Wayne A. Woodward Paperback: 512 pages Publisher: Wiley; 2 edition (August 3, 2015) Language: English ISBN-10: X ISBN-13: These slides are provided for you to use to teach SAS using this book. Feel free to modify them for your own needs. Please send comments about errors in the slides (or suggestions for improvements) to Thanks. SAS ESSENTIALS -- Elliott & Woodward
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.