Controlling SAS output using ODS
See
www.stattutorials.com/SASDATA
for
files mentioned in this tutorial
These SAS statistics tutorials briefly explain the use and
interpretation of standard statistical analysis techniques for Medical,
Pharmaceutical, Clinical Trials, Marketing or Scientific Research. The examples
include how-to instructions for SAS Software.
ODS, or Output Delivery System, is a method within SAS of
controlling the output from SAS Procedures. ODS began with version 8 and is now
in production in version 9.1 (but still under development)
ODS can be used to output results using several types of
format including:
- Basic SAS output (Listing)
- Output in HTML format (html)
- Output to Acrobat (pdf)
- Output as Rich Text Format (rtf) (Can be read by
Microsoft Word)
- Output to Postscript
ODS can also be used to
- Output data sets
- Output Graphs associated with procedures
Not every option available in ODS is covered in this
introduction to ODS. However, most SAS users should find that the information
discussed will cover most output needs.
This example shows how you can utilize ODS quickly and
simply. Following this example, we will look at options for using ODS that will
give you more control over the output.
Suppose you want to create output to be saved in Microsoft
Word.
Using the following code (ODS1.SAS)
proc
univariate
data=sasdata2.somedata;
histogram
time1 /cfill=SKYblue;
run;
you get the
standard SAS output (listing) shown (partially) below. The graph is in a
separate window, and colors available for the graph are limited.
The UNIVARIATE Procedure
Variable: ID (ID Number)
Moments
N 50 Sum Weights 50
Mean 374.22 Sum Observations 18711
Std
Deviation 167.498314 Variance 28055.6853
Skewness -0.2716195 Kurtosis -1.3081517
Uncorrected SS 8376759 Corrected SS 1374728.58
Coeff
Variation 44.7593165 Std Error Mean 23.6878388
Basic Statistical Measures
Location Variability
Mean 374.2200 Std Deviation 167.49831
Median 403.5000 Variance 28056
Mode . Range 503.00000
Interquartile Range 310.00000
... etc (output
truncated)
The graph output is:

By adding a simple ODS command to the code, as shown here: (ODS2.SAS)
ODS
RTF;
proc
univariate
data=sasdata2.somedata;
histogram
time1 /cfill=skyblue;
run;
ODS RTF CLOSE;
The
ODS RTF; command
turns on the ODS output in “Rich Text Format” which is compatible with Word.
The ODS RTF CLOSE;
command turns off the ODS output. The output is opened in a Microsoft Word
document as show here (partial output)
| Moments |
| N |
50 |
Sum Weights |
50 |
| Mean |
374.22 |
Sum Observations |
18711 |
| Std Deviation |
167.498314 |
Variance |
28055.6853 |
| Skewness |
-0.2716195 |
Kurtosis |
-1.3081517 |
| Uncorrected SS |
8376759 |
Corrected SS |
1374728.58 |
| Coeff Variation |
44.7593165 |
Std Error Mean |
23.6878388 |
This output is in
standard Word tables for text output, and also includes the previous graph in
the same Word file.
Note that SAS
initially saves the output in to an “.RTF” file. You can resave the output as a
“.DOC” file to make it into a standard Word document by selecting (in Word)
File/Save As… and choosing the “Files of Type” as “.doc”.)
The syntax of the
ODS command is
ods output-format
<options>;
The default SAS
output location is the normal “listing” which appears in the Output window. This
output can be turned on using the command:
ODS LISTING;
and turned off
using
ODS LISTING CLOSE;
To turn on any
other output type simply use the command
ODS
OUTPUT-FORMAT;
For example
ODS
HTML;
The turn the output off use the CLOSE option:
ODS
HTML CLOSE;
Usually, when you are using one of the other output types you may want to turn
off the default listing first, output to your selected type, then turn the
listing option back on. For example:
ODS
LISTING
CLOSE;
ODS
RTF;
* SOME PROCEDURES…;
ODS
RTF
CLOSE;
ODS
LISTING;
The RTF listing appears in the SAS Results Viewer and in the case of RTF output,
SAS also prompts you for a filename which allows you to store the information
into a specific RTF file
One of the most
useful <OPTIONS> is the ability to direct the output directly into a specific
file. The following examples show how you can “open” the output into a specific
type of file.
ods html body='HTML-file-pathname.html';
ods pdf file='PDF-file-pathname.pdf';
ods rtf file='RTF-file-pathname.rtf';
ods ps file='PS-file-pathname.ps';
Notice that the
HTML output uses BODY= and the others use FILE=. HTML output can also be output
to frames, but that is not discussed here. (Technically, the FILE= option also
works for HTML, but if you decide on using other HTML options (such as frames)
it is a good idea to stick with the BODY=option.)
For example, to
send data to an HTML file named C:\RESEARCH\OUTPUT.HTM you could use the syntax:
ODS HTML BODY=
‘C:\RESEARCH\OUTPUT.HTM’;
after running
whatever SAS PROCS whose output you want to capture in the file, you turn off
thIS ODS output using the command
ODS HTML CLOSE;
In a program you
would typically first turn off the default listing, open the ODS output, run
procedures, close ODS and reopen the standard listing:
ODS
LISTING
CLOSE;
ODS
HTML
FILE=
'C:\RESEARCH\OUTPUT.HTM';
PROC
PRINT;
PROC
MEANS;
*AND SO ON…;
ODS
HTML
CLOSE;
ODS
LISTING;
If you leave off
the destination as in
ODS
PDF;
*. . .some procedures;
ODS
CLOSE
PDF;
or
ODS
RTF;
*. . .some procedures;
ODS CLOSE RTF;
You will prompted
to enter an output filename (or the results will go into the SAS Results
viewer).
To control the
style of the SAS output you can specify the option
STYLE=STYLE-TYPE;
For example (to use
a style called ‘Barretsblue’)
ODS
RTF
STYLE=BARRETTSBLUE;
* ...some procedures;
ODS
RTF
CLOSE;
So, for
example, using the same program as above with a STYLE option (MODIFIED ODS2.SAS)
ODS
RTF
STYLE=BarrettsBlue;
PROC
UNIVARIATE
DATA=SASDATA2.SOMEDATA
;
HISTOGRAM
TIME1 /CFILL=SKYBLUE;
RUN;
ODS
RTF
CLOSE;
Produces
tables that are formatted with the “Barrettblue” style:
|
Moments |
|
N |
50 |
Sum Weights |
50 |
|
Mean |
374.22 |
Sum Observations |
18711 |
|
Std Deviation |
167.498314 |
Variance |
28055.6853 |
|
Skewness |
-0.2716195 |
Kurtosis |
-1.3081517 |
|
Uncorrected SS |
8376759 |
Corrected SS |
1374728.58 |
|
Coeff Variation |
44.7593165 |
Std Error Mean |
23.6878388 |
To produce a list
of the current SAS styles available, use the code:
PROC
TEMPLATE;
LIST STYLES;
RUN;
Here is a (partial)
listing of the results:
Obs Path Type
ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
1 Styles Dir
2 Styles.Analysis Style
3 Styles.Astronomy Style
4 Styles.Banker Style
5 Styles.BarrettsBlue Style
6 Styles.Beige Style
7 Styles.Brick Style
8 Styles.Brown Style
9 Styles.Curve Style
10 Styles.D3d Style
11 Styles.Default Style
12 Styles.EGDefault Style
13 Styles.Education Style
14 Styles.Electronics Style
15 Styles.Festival Style
16 Styles.FestivalPrinter Style
17 Styles.Gears Style
18 Styles.Journal Style
19 Styles.Magnify Style
20 Styles.Meadow Style
21 Styles.MeadowPrinter Style
22 Styles.Minimal Style
23 Styles.Money Style
24 Styles.NoFontDefault Style
25 Styles.Normal Style
26 Styles.NormalPrinter Style
27 Styles.Printer Style
28 Styles.Rsvp Style
29 Styles.Rtf Style
30 Styles.Sasweb Style
31 Styles.Sasweb2 Style
And so on...
Here is a simple
example, first using RTF output: (ODS3.SAS)
* DEFINE WHERE LISTING WILL GO;
ODS
RTF
FILE='C:\RESEARCH\ODS.RTF'
STYLE=SCIENCE;
* CLOSE THE NORMAL OUTPUT LISTING;
ODS
LISTING
CLOSE;
PROC
MEANS
DATA=SASDATA2.SOMEDATA;
TITLE
'ODS HTML Example';
RUN;
PROC
BOXPLOTS;
PLOT AGE*GP;
RUN;
* CLOSE THE RTF OUTPUT;
ODS
RTF
CLOSE;
* REOPEN THE NORMAL LISTING;
ODS
LISTING;
RUN;
This code produces
the following output – all in the same RTF/Word format file:
|
Variable |
Label |
N |
Mean |
Std Dev |
Minimum |
Maximum |
|
ID
AGE
TIME1
TIME2
TIME3
TIME4
STATUS
SEX |
ID Number
Age on Jan 1, 2000
Baseline
6 Months
12 Months
24 Months
Socioeconomic Status
|
50
50
50
50
50
50
50
50 |
374.2200000
10.4600000
21.2680000
27.4400000
30.4920000
30.8380000
3.9400000
0.4000000 |
167.4983143
2.4261332
1.7169551
2.6590623
3.0255942
3.5307333
1.3311036
0.4948717 |
101.0000000
4.0000000
17.0000000
21.3000000
22.7000000
21.2000000
1.0000000
0 |
604.0000000
15.0000000
24.2000000
32.3000000
35.9000000
36.1000000
5.0000000
1.0000000 |
(Note: We’ll see a
better ODS version of box plots later…)
Using the HTML
ODS destination requires a little extra information. In the listing below
(ODS3a.SAS) notice that instead of FILE= you use BODY= to specify the file
destination. Also, if there are graphs involved you may need to split the
filename between a GPATH= statement that specifies the destination folder for
graphs and a BODY= statement that specifies the destination filename.
(ODS3a.SAS)
* DEFINE WHERE LISTING WILL GO;
ODS
HTML
BODY='ODS.HTML'
GPath="C:\RESEARCH"
STYLE=SCIENCE;
* CLOSE THE NORMAL OUTPUT LISTING;
ODS
LISTING
CLOSE;
PROC
MEANS
DATA=SASDATA2.SOMEDATA;
TITLE
'ODS HTML Example';
RUN;
PROC
BOXPLOTS;
PLOT AGE*GP;
RUN;
* CLOSE THE HTML OUTPUT;
ODS
HTML
CLOSE;
* REOPEN THE NORMAL LISTING;
ODS
LISTING;
RUN;
A final example is
used to produce output in the PDF output (for Adobe Reader). This output
option allows you to create output that can be viewed across computer platforms
as long as the have the (free) Adobe reader. (ODS3b.SAS)
* DEFINE WHERE LISTING WILL GO;
ODS
PDF
FILE='C:\RESEARCH\ODS.PDF'
STYLE=SCIENCE;
* CLOSE THE NORMAL OUTPUT LISTING;
ODS
LISTING
CLOSE;
PROC
MEANS
DATA=SASDATA2.SOMEDATA;
TITLE
'ODS PDF Example';
RUN;
PROC
BOXPLOTS;
PLOT AGE*GP;
RUN;
* CLOSE THE PDF OUTPUT;
ODS
PDF
CLOSE;
* REOPEN THE NORMAL LISTING;
ODS
LISTING;
RUN;
Which produces the
following output (in Adobe)

Some standard SAS
listings output are difficult to read. Outputting in PDF or HTML often improves
the readability as well as the “looks” of the output.
(ODS4.SAS)
DATA
TTEST;
INPUT
GROUP OBS @@;
CARDS;
1 23.00 1 23.00 1 32.00 1 24.00 1 25.00
2 25.00 2 46.00 2 56.00 2 45.00 2 56.00
2 55.00
;
PROC
TTEST;
CLASS
GROUP;
VAR
OBS;
Title
'T-Test Example';
RUN;
Produces the following somewhat confusing listing:
Statistics
Lower CL Upper CL Lower CL
Upper CL
Variable
GROUP N Mean Mean Mean Std Dev Std Dev Std Dev Std
Err
OBS 5 20.705 25.4 30.095 2.2656 3.7815
10.866 1.6912
1
OBS 6 34.619 47.167 59.715 7.4636 11.957
29.326 4.8814
2
OBS Diff
(1-2) -34.45 -21.77 -9.08 6.3706 9.2618 16.908 5.6083
T-Tests
Variable Method Variances DF t Value Pr > |t|
OBS Pooled Equal 9 -3.88 0.0037
OBS Satterthwaite Unequal 6.16 -4.21 0.0053
Equality of Variances
Variable Method Num DF Den DF F Value Pr > F
OBS Folded
F 5 4 10.00 0.0445
Add ODS to the code:
ODS RTF;
PROC
TTEST;
CLASS
GROUP;
VAR
OBS;
Title
'T-Test Example';
RUN;
ODS
RTF
CLOSE;

| T-Tests |
| Variable |
Method |
Variances |
DF |
t Value |
Pr > |t| |
| OBS |
Pooled |
Equal |
9 |
-3.88 |
0.0037 |
| OBS |
Satterthwaite |
Unequal |
6.16 |
-4.21 |
0.0053 |
| Equality
of Variances |
| Variable |
Method |
Num DF |
Den DF |
F Value |
Pr > F |
| OBS |
Folded F |
5 |
4 |
10.00 |
0.0445 |
This output is at
least in a table that makes finding each value a little easier.
Creating an interactive web-page “Drill Down Chart”
using SAS ODS
Start with a regular GCHART (or GPLOT) Graph: (ODS5.SAS)