Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 37 ยท Statistics and Reporting

ODS and Output Destinations

Control SAS results with the Output Delivery System: send output to HTML, PDF and Excel, pick tables with ODS SELECT and TRACE, capture results with ODS OUTPUT, and set styles, with a Python stand-in.

Every SAS procedure produces its results as output objects: a table of statistics, a graph, a note. The Output Delivery System (ODS) takes those objects and delivers them to a destination: the results window, an HTML page, a PDF, an Excel workbook, a Word-friendly RTF file, or a SAS data set. ODS separates what was computed from how it is shown, so the same PROC can drive a web page and a spreadsheet.

SAS cannot run on this site, so all sas blocks are read-only. I describe what the statements do without showing generated files. The TryIt is a Python stand-in that models the "one set of results, several destinations" idea.

Opening and closing destinations

You turn a destination on, run procedures, and turn it off to finish the file:

SAS
ods html file="C:\reports\sales.html" style=journal;

proc means data=work.sales mean sum;
  class region;
  var amount;
run;

proc freq data=work.sales;
  tables region * product;
run;

ods html close;

Everything that ran between ods html file= and ods html close; ended up in sales.html. If you forget to close the destination, the file may be empty or locked when you try to open it. Other destinations work the same way:

Destination Statement Output
HTML ods html file="a.html" Web page (with images via path= and gpath=)
PDF ods pdf file="a.pdf" Paginated document
Excel ods excel file="a.xlsx" Workbook, one sheet per sheet_name option
RTF ods rtf file="a.rtf" Word-readable document
PowerPoint ods powerpoint file="a.pptx" Slides
Listing ods listing Plain text output

You can have several destinations open at once, and all of them receive the same results. ods _all_ close; closes everything, which is useful at the top of a batch program. In SAS Studio and SAS Enterprise Guide, an HTML destination is already open, which is where your output appears.

Styles

A style controls fonts, colors and borders. Choose one with style=:

SAS
ods pdf file="report.pdf" style=journal;       /* black and white, print friendly */
ods html file="report.html" style=htmlblue;    /* a built-in color style */

journal, htmlblue, default, minimal and sasweb are typical built-in styles. To see all styles in your installation, run proc template; list styles; run;. Custom styles are defined with proc template, but the built-in ones are enough for most reports.

Excel with several sheets

SAS
ods excel file="C:\reports\summary.xlsx"
  options(sheet_name="Sales by region" embedded_titles="yes");

proc means data=work.sales sum;
  class region;
  var amount;
run;

ods excel options(sheet_name="Products");
proc freq data=work.sales;
  tables product;
run;

ods excel close;

Each sheet_name change starts a new worksheet. sheet_interval="proc" makes a new sheet for each procedure automatically.

Stand-in: one result, many destinations

Python Stand-in: render one table as text and HTML

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The data structure never changed; only the renderer did. ODS has the same split: procedures create the table object, destinations render it.

Choosing and excluding output

A procedure often creates more tables than you want. Every table has a name; find the names with the trace statement:

SAS
ods trace on;
proc ttest data=work.patients;
  class sex;
  var weight;
run;
ods trace off;

The log lists each output object with its Name (for example Statistics, TTests, Equality). Then keep or hide by name:

SAS
ods select TTests;                  /* show only this table */
ods exclude Equality;               /* show everything except this one */
ods select all;                     /* reset */
ods noproctitle;                    /* drop the procedure name heading */

Use ods graphics on; before procedures that produce plots (modern SAS turns this on by default) and ods graphics / width=6in height=4in imagefmt=png; to size images.

Capturing results as data sets

ods output copies a result table into a data set so you can use the numbers in later code:

SAS
ods output TTests=work.ttest_results Statistics=work.group_stats;

proc ttest data=work.patients;
  class sex;
  var weight;
run;

proc print data=work.ttest_results;
run;

Now work.ttest_results holds the t statistic, degrees of freedom and p-value, one row per method, ready to merge into a summary report or to check automatically (if probt < 0.05 then ...). Compare this with the output out= statement of PROC MEANS and PROC SUMMARY: output statements save only a procedure's chosen statistics, while ods output saves any table the procedure prints. For that reason, ods output is the standard way to get at numbers that otherwise appear only in the report.

Add ods output ... ; ... ; ods output close; only if you want to end the capture explicitly; otherwise the capture covers one procedure run.

Titles, footnotes and options

Titles belong to the report, not to ODS, but they work with all destinations:

SAS
title1 "Quarterly sales";
title2 "Region: East";
footnote "Source: finance system, run on &sysdate9";
options nodate nonumber;     /* remove the date and page number line */
title;                       /* clear all titles */

Titles persist until you change or clear them, so a title from an earlier step can leak into a later report. Clear them with title; and footnote; at the end of each report.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What must you do to complete an ODS HTML file?

    The destination writes the file until it is closed; without ods html close the file may be incomplete or locked.
  2. 2. Which statement lists the names of output objects a procedure creates?

    ods trace on writes each output object's name to the log.
  3. 3. How do you save a printed table as a data set?

    ods output table-name=data-set copies that output object into a data set.
  4. 4. What does ods select TTests; do?

    ods select limits the displayed output to the objects you name.
  5. 5. Why clear titles with title; after a report?

    A TITLE stays in effect until replaced or cleared.

Summary

  • ODS separates results from presentation; destinations include HTML, PDF, Excel, RTF and PowerPoint.
  • Open a destination with ods <dest> file=, run procedures, then close it.
  • style= changes the look; ods excel options(sheet_name=) builds multi-sheet workbooks.
  • ods trace on reveals table names, ods select and ods exclude filter them.
  • ods output name=data-set turns any printed table into data you can program with.

Next: PROC REPORT and PROC TABULATE