You are on page 1of 5

Lesson 44: ODS

<<Previous|Contents|Next>>
Video: Traffic Lighting
Use ODS to send output to different destinations, e.g.:
ods listing close;
ods html file="c:\stats\manpresults.xls" style=minimal;
proc reg data=s.manp;
model y=x1-x7;
quit;
ods html close;
ods listing;

This ods command creates an external file with the given path and filename.
The "ods html close" command is important here. Any time you want to create a file, whether
html, pdf, or anything else, the file on the disk is not closed until the ods close command is
issued. This has two results, 1. You cannot open it with an outside program, and 2. If more
output is generated, say by running the procedure again, the new output will be added to the file.
SAS will not overwrite these files if they are open in either the results viewer or Excel
(externally). So you must remember to close the document in Excel or in the Results Viewer
before running the program again.
The results viewer opens this like an Excel file because of the xls extension, and the file can be
double-clicked and opened in Excel directly. It is actually an html file, though. Excel knows
how to open html files. It will not become an Excel file unless you go "Save As" and save it in
Excel format.
You can apply different styles to change the appearance of the output. Use the "minimal" style to
keep the formatting simple for later modification in Excel. There are many styles available,
although not all are useful for html. Refer to the lesson on ODS or ODS documentation for more
information about styles.
Use ODS select to choose which tables to print. Use ODS output to send an output table (or
object) to a SAS data set.
ods listing close;
ods html file="c:\stats\manpresults.xls" style=minimal;
proc reg data=s.manp;
model y=x1-x7;
ods select parameterestimates;
ods output parameterestimates=manpresults;
quit;
ods html close;
ods listing;

Now you can use proc print to print it, using only the desired variables, and sending the output to
html or an excel file.

ods listing close;


ods html file="c:\stats\manpresults.xls" style=minimal;
proc print data=s.manpresults;
var variable estimate stderr tvalue probt;
run;
ods html close;
ods listing;

You can format titles, such as:


title font=times color=red bcolor=yellow height=6 "Parameter Estimates
Table";

You can also use proc report instead of proc print.


ods listing close;
ods html file="c:\stats\manpresults.xls" style=minimal;
proc report data=manpresults nowindows split="/";
columns variable estimate stderr tvalue probt;
define variable/display "Variable/Name";
define estimate/display "Parameter/Estimate";
define stderr/display "Standard/Error";
define tvalue/display "Test/Statistic" width=9;
define probt/display "P-/Value";
run;
ods html close;
ods listing;

Proc report allows greater control of the output, and in particular will allow us to do "Traffic
Lighting." In html the column headings won't automatically split like they do in the output
window, so a split character can be added to accomplish that.
Use "traffic lighting" to change colors of different p-values or highlight whole rows. In the
define statement for a variable, you can put a style command like this:
define probt/display "P-/Value" style=column[foreground=red
background=yellow font_weight=bold];

This will change the appearance of a whole column. However we would like the change to apply
only to certain rows. In order to do this, we introduce another feature of proc report, the
compute block. But even before that, we need to define a format, since the p-value is a
continuous variable. We want to define the ranges that our traffic lighting will affect.
proc format;
value pvalcolor
low-<.05=green
.05-<.1=blue
other=red;
run;

Now use the format by putting it in place of the color in the define statement. We also take out
the background color here. You could make another format to assign contrasting colors to the
background.
define probt/display "P-/Value" style=column[foreground=pvalcolor.
font_weight=bold];

Lesson 45: ODS


<<Previous|Contents|Next>>
Video: More traffic lighting and ODS
Now we will see how we can highlight a whole row based on the value of one column.
This is done using a compute block in proc report, as shown below.
ods html file="c:\stats\manpresults.xls" style=minimal;
proc report data=manpresults nowindows split="/";
columns variable estimate stderr tvalue probt;
define variable/display "Variable/Name";
define estimate/display "Parameter/Estimate";
define stderr/display "Standard/Error";
define tvalue/display "Test/Statistic" width=9;
define probt/display "P-/Value";
compute probt;
if probt<.05 then
call define(_row_, "style", "style=[foreground=red
font_weight=bold]");
endcomp;
run;
ods html close;

The compute block begins with the keyword "compute" and ends with "endcomp." Although it
looks like we are saying we will compute the value of probt, this is not what it means. Actually
it is a computation based on the column probt. Some other kinds of compute blocks can actually
compute values for a new column. Here we are using an "if" statement to apply a style whenever
a condition on probt is met. There could be multiple styles applied with more "call define"
statements based on different values of probt. The syntax of the compute block is, unfortunately,
quite complex. You can find it in the documentation for proc report, but be prepared to wade
through several links to different sections before putting it all together.
Survey of documentation about proc report and ODS.
ODS Graphics in proc reg.
<<Previous|Contents|Next>>
Exercise:

Refer to the used cars data set which can be created with this program: usedcars.sas. Use the
ideas of this lesson to demonstrate what you can do with proc report on this data. Be creative,
use your own ideas and imagination to create an attractive report.
Lesson 46: ODS Document Destination
<<Previous|Contents|Next>>
Video: ODS Document
Begin by loading some data and doing a regression example. We can use the manp data set
which was used in earlier examples.
proc reg data=s.manp;
model y=x1-x7;
run;

Observe that in the output window there are four tables created, which are also ods objects.
We are going to now use the "document" destination. The document needs a name. It is a SAS
name similar to data set names. In fact, a two-level name can be used to store the document
permanently. The document destination must be closed when you are finished with what you
want to put in it.
Before running the code, you need to put the command "odsdocuments" in the command bar in
the upper left corner of the SAS window. This opens a document window on the left side with
the Results and Explorer. (It can also be done after running the program.) In this window there
will be a tree which shows the objects in the document.
ods document name=manpresult;
proc reg data=s.manp;
model y=x1-x7;
run;
ods document close;

You have to navigate through the tree down to the "leaves" in the document which are the ods
objects. Here you can right click and perform more tasks. Under "Open As" you can open them
in various output destinations. You can also save them from the document viewer in these other
formats.
These methods don't give us a chance to apply styles, though. There is another way this can be
done. Just issue an ods command in your program that tells what kind of document you want to
produce, such as:
ods pdf file="c:\stat510\results.pdf" style=journal;

Now go to the document tree, right click on an object, and choose "replay." Then go back to the
program window and close the ods destination.
ods pdf close;

This will create your document using the settings in the ods statement.
Now we'll do a few examples using proc document. You can specify the document name which
shows up under the documents folder. One of the statments is "list" which will print some
information about the path and names of objects. This may be needed to refer to the objects.
proc document name=manpresult;
list/levels=all;
run;

You can combine results from different runs, or reorder the results in a document by creating a
new document. (You cannot reorder objects within an existing document, although you can
delete objects.)
Right-click on documents and select "New Document." Give it a new name if you like. Select
work or another directory in the list at the bottom. Now go to the tree for Manpresult and right
click on an object, say "Parameter Estimates." Choose copy. Go to your new document folder
and choose paste. I have found that the new object doesn't show up right away, but if you right
click on the new document folder and select "Expand All" it will show up. You can copy and
paste more objects from any of the documents you have in the document window to your new
document. They will be entered in the order you listed, they are always added to the bottom of
the document. You can then use the method above to replay the document into any destination
you choose.
All of these things can also be done using proc document code. At present, I do not know how to
do this correctly. It is something like the following, but the path is not right:
proc document name=manpresult;
copy Reg#1\MODEL1#1\Fit#1\y#1\FitStatistics#1 to manpresult;
run;

You might also like