Chapter 8 SAS ESSENTIALS

advertisement
Introduction to SAS Essentials
Mastering SAS for Data Analytics
Alan Elliott and Wayne Woodward
1
SAS ESSENTIALS -- Elliott & Woodward
Chapter 8: Controlling Output Using
ODS
2
SAS ESSENTIALS -- Elliott & Woodward
LEARNING OBJECTIVES
To be able to specify ODS output format and
destination
To be able to specify ODS style
To be able to select specific tables for output
To be able to capture information from ODS tables
To be able to use ODS graphics from SAS®
procedures
To be able to use Traffic Lighting to highlight
selected values






3
SAS ESSENTIALS -- Elliott & Woodward
What is ODS?
ODS, or Output Delivery System, is a method for
controlling the output from SAS® procedures. ODS
began with version 8 and continues with added
enhancements in more recent versions.
Prior to SAS 9.3, SAS output appeared in the Output
Window. This output listing is like a monospaced
typewriter font (with no graphics) and there are few
options that allow you to control the “look” of the
listing.
Beginning with 9.3 default output is HTML type
output.



4
SAS ESSENTIALS -- Elliott & Woodward
8.1 SPECIFYING THE ODS OUTPUT FORMAT
AND DESTINATION
The SAS ODS is set up so that you "turn on" or
initiate output into a designated output format. Once
the output format has been initiated, SAS
procedures send information to that output format.
You can send output from one or more procedures to
the output stream.
Used to tell SAS to

start outputting results
to a specified output
type.
ODS OUTDESTINATION <OPTIONS>;
To end the ODS output, use the CLOSE statement:

ODS OUTDESTINATION CLOSE;
5
Tells SAS that you are
finished outputting results.
SAS ESSENTIALS -- Elliott & Woodward
For example
RTF means “Rich text format” – for
word processing output.
ODS RTF;
SAS code that creates output;
ODS RTF CLOSE;
6
SAS ESSENTIALS -- Elliott & Woodward
Common ODS Output Locations
Here are several ODS output formats:

ODS
ODS
ODS
ODS
ODS
ODS
LISTING <FILE='file-specification'>;
HTML <BODY='HTML-FILE-PATHNAME.HTML>';
PDF <FILE='PDF-FILE-PATHNAME.PDF'>;
RTF <FILE='RTF-FILE-PATHNAME.RTF'>;
PS <FILE='PS-FILE-PATHNAME.PS'>;
PLC <FILE='PCL-FILE-PATHNAME.PCL'>;
Note for ODS the file location is listed as BODY=.
You could also use FILE=. But BODY= is needed in
certain circumstances.

7
SAS ESSENTIALS -- Elliott & Woodward
Descriptions of ODS Output Types
HyperText Markup Language (HTML): HTML is the
current standard (default) output format used in
Windows SAS versions. This HTML format is the
same used for displaying Web pages on the Internet
and is also recognized by most word processors
such as Microsoft Word. To reset SAS to this default
(if it becomes turned off, use the statement ODS
PREFERENCES;)
LISTING: This "old-style" SAS output format was the
default output location used in SAS Windows
versions prior to 9.1. It is sometimes called
"typewriter output" and is based on a monospaced
font.


8
SAS ESSENTIALS -- Elliott & Woodward
Descriptions of ODS Output Types (Continued)
Portable Document Format (PDF): PDF is widely used
to distribute documents by e-mail or via Internet
downloads. Use of a PDF document requires that the
(free) Adobe Reader or a similar program be installed on
your computer. One of the benefits of the PDF format is
that the contents of your document will appear the same
when viewed under many types of computer operating
systems, such as Windows, Mac OS X, and UNIX and its
Linux derivations.
Rich Text Format (RTF): RTF is a common language for
word processors. When you capture information in this
format, it can be opened and edited by most word
processors such as Microsoft Word or even Wordpad.


9
SAS ESSENTIALS -- Elliott & Woodward
Descriptions of ODS Output Types (Continued)





Postscript (PS): PS is a printer language, and the output
captured in this format is usually designed to be printed
on a PS printer.
Print Command Language (PCL): PCL is a printer
language, and the output captured in this format is
usually designed to be printed on a PCL printer.
Extensible Markup Language (XML): XML is a plain
text data interchangeable format that can be read using a
normal text editor but is in a standard machine readable
format.
Tagsets.ExcelXP: The Tagsets.ExcelXP outputs XML
tables that can be read by Microsoft version 2002 or later.
Other specialty output formats are DOCUMENT,
MARKUP, and WML (Wireless Markup Language).
10
SAS ESSENTIALS -- Elliott & Woodward
Example of Specifying an Output Destination

For example, to send data to a PDF file, you could
use the following syntax:
ODS PDF file='WINDOWS-PATH\Filename.PDF';
or, as a specific example:
ODS PDF FILE='C:\SASDATA\MYOUTPUT.PDF';

Example of not specifying an output file name:
ODS PDF;
This sends the output to your
*... SAS code here;
default PDF viewer. (i.e. Adobe
Acrobat Reader.)
ODS CLOSE PDF;

11
SAS ESSENTIALS -- Elliott & Woodward
Another Output Example – to Word



For example, if you use ODS RTF; to open the RTF
(Word) output, then you must at some point close
with the statement ODS RTF CLOSE;.
RTF (Rich Text Format) is the generic Word
Processing language.
Thus, the statements would be
This code sends your
output to Microsoft Word
(if you have it installed)
ODS RTF;
SAS code that produces output ... ;
ODS RTF CLOSE;
12
SAS ESSENTIALS -- Elliott & Woodward
Important Default Settings Related to ODS

Beginning with SAS 9.2 the default output location
became HTML. As of SAS Version 9.4, the output
defaults are as follows:
• Output is HTML, style is HTMLBlue
• ODS Graphics is ON
Note: HTML is the default ODS
• Listing is CLOSED
output type.

Important: If you close HTML with (ODS HTML
CLOSE;), SAS may not produce any output. You’ll
get an error message in the log that there is no
output destination open. If this happens, reset the
default options with:
ODS PREFERENCES;
13
SAS ESSENTIALS -- Elliott & Woodward
What do you do if your output is not showing up
in the Results Viewer?
What do you do if your output is not showing up
in the Results Viewer?
ODS PREFERENCES;
(Or end SAS and restart)
ODS Style


When you output information to a SAS ODS format,
the tables, graphs, and text are defined with default
colors and fonts. You can select from several built-in
ODS styles.
Each style is based on some theme or a format
appropriate for a specific purpose. For example, the
JOURNAL style formats your output in black and
white that is appropriate for inclusion in a journal
article. The option to specify a style is
STYLE=styletype;
16
SAS ESSENTIALS -- Elliott & Woodward
ODS Style Types

This option appears in the ODS statement. For example,
ODS RTF STYLE=JOURNAL;
... some SAS procedures;
ODS RTF CLOSE;

To see a listing of the available SAS styles, use the code:
PROC TEMPLATE; LIST STYLES; RUN;

The default style is Styles. Default. The SAS format styles for
version 9.4 as reported by PROC TEMPLATE are given in
Table 8.1 (with the prefix "Styles." removed to make it easier to
read the table).
17
SAS ESSENTIALS -- Elliott & Woodward
Table 8.1 – ODS Styles
18
SAS ESSENTIALS -- Elliott & Woodward
Hands On Example page 196 (ODS1.SAS)
DATA TEST; SET "C:\SASDATA\SOMEDATA";
* DEFINE WHERE HTML LISTING WILL GO;
TITLE 'ODS HTML Example';
ODS HTML BODY='C:\SASDATA\ODS.HTML';
PROC MEANS MAXDEC=2; VAR AGE TIME1-TIME4;
RUN;
* CLOSE THE HTML OUTPUT;
ODS HTML CLOSE;
RUN;
ODS PREFERNCES;

Where does the output from this program go?
19
SAS ESSENTIALS -- Elliott & Woodward
Illustrating the Destination Problem…


Run just the line ODS HTML CLOSE;
Now run just the code.
PROC MEANS MAXDEC=2; VAR AGE TIME1-TIME4;
RUN;
Look at the log…notice…
WARNING: No output destinations active.
 Correct the problem by running

ODS PREFERNCES;
20
SAS ESSENTIALS -- Elliott & Woodward
Add a STYLE= option to the code
ODS HTML BODY='C:\SASDATA\ODS.HTML‘
STYLE=JOURNAL;
Rerun and observe the difference.

HTMLBlue
JOURNAL

Try another style.
21
SAS ESSENTIALS -- Elliott & Woodward
Hands On Example (ODS2.SAS)
DATA TEST; SET "C:\SASDATA\SOMEDATA";
* DEFINE WHERE PDF LISTING WILL GO;
TITLE 'ODS PDF Example';
ODS PDF FILE='C:\SASDATA\ODS.PDF'
STYLE=STATISTICAL;
PROC MEANS MAXDEC=2; VAR AGE TIME1-TIME4;
RUN;
* CLOSE THE PDF OUTPUT;
ODS PDF CLOSE;
RUN;
 Observe the PDF file on your hard drive.
 Redo the example using RTF.
22
SAS ESSENTIALS -- Elliott & Woodward
8.3 USING ODS TO SELECT SPECIFIC
OUTPUT TABLES FOR SAS PROCEDURES



SAS procedures often output a lot of information you
don't want or need. In ODS output, each part of the
output is contained in a table.
Using ODS options, you can customize which
tables you want SAS to output to the ODS
To include or exclude a table from the output, you
first need to know the table's name. You can
discover this information by using the ODS TRACE
command in the following way:
ODS TRACE ON;
PROC whatever;
ODS TRACE OFF;
23
Putting the TRACE code around
a PROC produces information in
the LOG that tells you the names
of the ODS output tables.
SAS ESSENTIALS -- Elliott & Woodward
Specifying which tables to display

Once you know the names of the tables you want to
display (using TRACE), use the following code to
make that request:
This ODS statement tells SAS to
output only the specified table
from PROC whatever
ODS SELECT name-of-tables-to-include;
PROC whatever;
Etc…
24
SAS ESSENTIALS -- Elliott & Woodward
HANDS ON EXAMPLE ON P 199

(Using ODS3.SAS)
STEP 1: Use TRACE to
discover names of
output tables.
25
RESULTS OF RUNNING THIS CODE:

Open the file ODS3.SAS
Note names of
tables in the SAS
Log file
26
USE THAT INFORMATION:

STEP 2: Use SELECT
tablenames to produce output
that ONLY contains the tables of
interest:
ODS SELECT CROSSTABFREQS CHISQ;
PROC FREQ;WEIGHT COUNT;
TABLES A*B /CHISQ;
RUN;

(You can also use ODS
EXCLUDE to exclude certain
tables from output.)
27
8.4 GOING DEEPER: CAPTURING
INFORMATION FROM ODS TABLES

Once you know the name of an output table, you can
use ODS to save the table contents into a SAS data
file using:

ODS OUTPUT NAMEOFTABLE=OUTPUTDATASET;
You learn the name of the
table using the TRACE
statement
28
When you output a table
using ODS OUTPUT, you
are creating a SAS data
set.
SAS ESSENTIALS -- Elliott & Woodward
CAPTURING OUTPUT USING ODS OUTPUT
(Using ODS6.SAS)
DATA WT;
INPUT WEIGHT @@;
DATALINES;
64 71 53 67 55 58
77 57 56 51 76 68
;
ODS SUMMARY=STATS;
PROC MEANS DATA=WT;
RUN;

29
This ODS OUTPUT
statement captures the
output from PROC MEANS
into a SAS data file named
STATS
Standard PROC MEANS output:
This information is
also stored in a
SAS data set
named STATS
SAS ESSENTIALS -- Elliott & Woodward
THE OUTPUT DATA SET


The output data set (STATS) created from the PROC
MEANS Summary table is shown below.
Note the names of the mean, and standard
deviation.
MEAN of
WEIGHT
30
Standard Deviation of
WEIGHT
COMBINE THE OUTPUT DATA SET WITH THE
ORIGINAL DATA SET

Combine the output data set (STATS) information with
the current SAS data set (WT) using the following code:
Note two SET
statements – combines
the data sets.
DATA WTDIFF; SET WT;
IF _N_=1 THEN SET STATS;

IF _N_=1 specifies to combine only the first record of
WT with STATS to create WTDIFF.
31
SAS ESSENTIALS -- Elliott & Woodward
HANDS ON EXAMPLE P 203

(Code from ODS7.SAS)
As the first record of WT is
read, the STATS data is added
to the data.
DIFF and Z are calculated
using variables in the
combined data set
WTDIFF.
32
RESULTS FROM COMBINED DATA SET
Data from original
data set WT
33
Data from the PROC MEANS output table
saved as STATS
Calculated
variables
RESULTS FROM COMBINED DATA SET
Data from original
data set WT
Data from the PROC MEANS output table
saved as STATS
Note how the first record (_N_=1)
from the data set STATS is
repeated for every original record
of WEIGHT from the WT data set
which allows you to calculate the
DIFF and Z values.
34
Calculated
variables
Using ODS Graphics from SAS Procedures
ODS graphics refers to graphs that are created from SAS
procedures. Although ODS graphics have been around
for several SAS versions, the graphics only recently
began appearing automatically when you run a SAS
PROC (at least in the Windows edition of SAS).
 Sometimes, the automatic creation of ODS graphics
takes a while, and if you are doing a lot of analyses and
only want the tabled output, you might be interested in
shutting off ODS graphics for a while. You can do this
(temporarily) using the command
ODS GRAPHICS CLOSE;
 To turn ODS graphics back on use
ODS GRAPHICS ON;

35
SAS ESSENTIALS -- Elliott & Woodward
ODS Graphics

You can also turn off ODS graphics by selecting (in
SAS) the menu options Tools -> Options->
Preferences, and select the Results Tab. The dialog
box shown in Figure 8.3 is displayed. Note the option
Use ODS Graphics. If you want to turn off graphics,
uncheck this box.
36
SAS ESSENTIALS -- Elliott & Woodward
“Free” ODS Graphs

Open the file ATTEST2.SAS and run it…note that
not plots are requested…
PROC TTEST;
CLASS BRAND; VAR HEIGHT;
Title 'Independent Group t-Test
Example';
RUN;
37
SAS ESSENTIALS -- Elliott & Woodward
TTEST (ODS) Plots automatically generated….
38
SAS ESSENTIALS -- Elliott & Woodward
Hands On Example p 205 - ODS GRAPHS


For some ODS graphs, you need to specifically
request plots
Open ODS8.SAS, p 205
PROC FREQ DATA="C:\SASDATA\SOMEDATA";
TABLES STATUS;
TITLE 'Simple Example of PROC FREQ';
RUN;
 Run this code – no graph is produced.
39
SAS ESSENTIALS -- Elliott & Woodward
Request a graph in PROC FREQ

Change the TABLES statement to
TABLES STATUS/PLOTS=FREQPLOT;
40
SAS ESSENTIALS -- Elliott & Woodward
8.5 GOING DEEPER: USING TRAFFIC
LIGHTING TO HIGHLIGHT SELECTED
VALUES



There may be an instance when you are creating a
report using PROC PRINT that you want to highlight
certain values according to some criteria.
For example, if a value is importantly high or low,
you might want to highlight the value with a color.
Traffic Lighting is a way to do that.
41
SAS ESSENTIALS -- Elliott & Woodward
Traffic Lighting - Step 1 – Create a Format

Create a format that indicates colors for certain
values. For example, suppose you want to highlight
values of ISS, the Injury Severity Score according to
severity. A format to do that could be created using
this code:
PROC FORMAT;
VALUE FMTSEVERE
LOW-<0="BLACK"
0-<10="GREEN"
10-<20="BLUE"
20-HIGH="RED“;
42
Specify colors you want
to use in your report.
SAS ESSENTIALS -- Elliott & Woodward
Traffic Lighting – Step 2 – Apply the Format

In PROC PRINT , tell SAS which variable to assign
this formatting for using the VAR statement. For
example,
VAR ISS/STYLE={FOREGROUND=FMTSEVERE.};
This tells SAS to apply the previously
created FORMAT named FMTSEVERE
to the FOREGROUND style in your
output for the variable ISS.
43
SAS ESSENTIALS -- Elliott & Woodward
Results from Traffic Lighting

Do Hands On Example p 207 (ODS9.SAS)
44
SAS ESSENTIALS -- Elliott & Woodward
Other Traffic Lighting Format Options

There are a number of other format options you may
control using the Traffic Lighting Technique. This
code illustrates how they work in the FORGROUND
STYLE statement:
VAR ISS/STYLE={FOREGROUND=FMTSEVERE.
BACKGROUND=LIGHTYELLOW
This code is used in the
Hands On Example p
FONT_WEIGHT=BOLD
207
FONT_FACE=SCRIPT
FLYOVER='Severity Rating'};
45
SAS ESSENTIALS -- Elliott & Woodward
8.6 EXTENDED ODS FEATURES





SAS ODS features not covered in this chapter:
Every ODS table output by a SAS procedure uses a
template to control the way it is created. Using PROC
TEMPLATE, you can modify these templates to
customize the content and look of each output table.
You can store the components of any ODS report in order
to modify and replay them using PROC DOCUMENT.
These tasks are beyond the scope of this book, and we
refer you to the SAS documentation if you want to learn
more about these topics.
See Table A.2 p 470 for more options.
46
SAS ESSENTIALS -- Elliott & Woodward
8.7 SUMMARY





The SAS ODS provides a number of ways to manipulate
output from SAS procedures.
Many examples in the book have used ODS to output
results from procedures into HTML and other formats.
This chapter introduced other ways to use ODS,
including additional options for outputting results from
SAS procedures, outputting SAS data sets, and using
ODS to enhance graphic output.
This concludes PART 1 of SAS Essentials
Continue to PART 2 – STATISTICAL ANALYSIS USING
SAS® PROCEDURES and Chapter 9: EVALUATING
QUANTITATIVE DATA
47
SAS ESSENTIALS -- Elliott & Woodward
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: 111904216X
ISBN-13: 978-1119042167
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 acelliott@smu.edu.
Thanks.
48
SAS ESSENTIALS -- Elliott & Woodward
Download