24.2 - Opening and Closing ODS Destinations

If you are perfectly content with your output being sent to the output window in the default listing format, then you don't have to tell SAS anything at all. That's because the Listing destination is open by default. On the other hand, if you want to tell SAS to send your output to another ODS destination, like HTML, then you have to open the destination before the SAS code generates your output.

To open a destination, you simply submit the following ODS statement:

ODS open-destination;

where open-destination is a keyword (as well as any required options for the destination) that tells SAS where you want to send your output. In this lesson, we'll focus only on the most commonly used keyword destinations: Listing, HTML, RTF, and PDF.

After the SAS code generates your output, you have to tell SAS to close the destination so that you can access your output. To close a destination, you simply submit the following ODS statement:

ODS close-destination CLOSE;

where close-destination is the same keyword as the open-destination.

In theory, you can submit ODS statements in any order, depending on whether you need to open or close an ODS destination. In practice, however, most ODS destinations are closed by default, so that you open them at the beginning of your program and close them at the end. The exception is the Listing destination, which is open by default. Let's take a look at an example.

Example 24.1 Section

You might recall that the SAS data set called penngolf contains information, such as the total yardage and par, of eleven golf courses in Pennsylvania. The following program opens the HTML destination so that a subset of the penngolf data set can be printed in HTML format as well as the default Listing format:

OPTIONS PS = 58 LS = 72 NODATE NONUMBER;
LIBNAME stat481 'C:\yourdrivename\Stat481WC\12ods\sasndata';
 
ODS HTML file = 'C:\yourdrivename\Stat481WC\12ods\output\golf.html';
 
PROC PRINT data = stat481.penngolf NOOBS;
   title 'Some of the penngolf data set variables';
   ID name;
   var year type par yards;
RUN;
 
ODS HTML CLOSE;

Some of the penngolf dataset variables
NameYearTypeParYards
Toftrees1968Resort727018
Penn State Blue1921Public726525
Centre Hills1921Private716392
Lewistown CC.Private726779
State College Elks1973SemiPri716369
Park Hills CC1966SemiPri706004
Sinking Valley CC1697SemiPri726755
Williamsport CC1909Private716489
Standing Stone GC1973SemiPri706593
Bucknell GC1960SemiPri706253
Mount Airy Lodge1972Resort727123

This code illustrates the standard ODS practice mentioned earlier... open your destinations at the top of your program, and close them at the bottom. Here, the first ODS statement tells SAS to open the HTML destination and to save the HTML output generated by the PRINT procedure that follows the specified file name. The second ODS statement tells SAS to close the HTML destination so that we can access the created HTML file.

Download and save the penngolf (click to save!) data set to a convenient location on your computer. Then, launch the SAS code, and edit the LIBNAME statement so that it reflects the location in which you saved the data set. Also, edit the first ODS statement's FILE= option so that it reflects the location and name of the file where you want the resulting HTML output to be sent. (Make sure that you give your filename the standard .html extension.) Finally, run  the SAS program. In doing so, you should note that SAS generates two pieces of output. The default listing output is displayed, as always, in the output window:

Some of the penngolf dataset variables
NameYearTypeParYards
Toftrees1968Resort727018
Penn State Blue1921Public726525
Centre Hills1921Private716392
Lewistown CC.Private726779
State College Elks1973SemiPri716369
Park Hills CC1966SemiPri706004
Sinking Valley CC1697SemiPri726755
Williamsport CC1909Private716489
Standing Stone GC1973SemiPri706593
Bucknell GC1960SemiPri706253
Mount Airy Lodge1972Resport727123

and the specifically requested HTML output:

Some of the penngolf dataset variables
NameYearTypeParYards
Toftrees1968Resort727018
Penn State Blue1921Public726525
Centre Hills1921Private716392
Lewistown CC.Private726779
State College Elks1973SemiPri716369
Park Hills CC1966SemiPri706004
Sinking Valley CC1697SemiPri726755
Williamsport CC1909Private716489
Standing Stone GC1973SemiPri706593
Bucknell GC1960SemiPri706253
Mount Airy Lodge1972Resort727123

is displayed in the Results Viewer. If the HTML output doesn't pop up on its own, you can see it by clicking on the Results Viewer button:

SAS window tabs showing results viewer tab open

along the bottom of your SAS window.

 

NOTE: If you encounter “ERROR: No body file. HTML output will not be created.”, look at the SAS Support Knowledge Base for a solution.

 

Closing the Listing Destination Section

Even though we didn't specifically request the listing output in the above program, we got it anyway because, as mentioned earlier, the listing output is open by default. We have to specifically tell SAS to close the Listing destination if we don't want SAS to generate listing output:

ODS LISTING CLOSE;

In general, because open destinations use system resources, it's a good idea to close the Listing destination at the beginning of your program if you don't want to produce listing output. Upon submitting the above ODS statement, the Listing destination remains closed until we end our current SAS session or until we re-open the destination. It's good programming practice to re-set ODS to the default Listing destination at the end of your programs:

ODS LISTING;

Let's take a look at an example.

Example 24.2

The following program is identical to the previous program, except here the Listing destination is closed at the beginning of the program, and re-opened again at the end:

Some of the penngolf dataset variables
NameYearTypeParYards
Toftrees1968Resort727018
Penn State Blue1921Public726525
Centre Hills1921Private716392
Lewistown CC.Private726779
State College Elks1973SemiPri716369
Park Hills CC1966SemiPri706004
Sinking Valley CC1697SemiPri726755
Williamsport CC1909Private716489
Standing Stone GC1973SemiPri706593
Bucknell GC1960SemiPri706253
Mount Airy Lodge1972Resport727123
ODS LISTING CLOSE;
ODS HTML file = 'C:\yourdrivename\Stat481WC\12ods\output\golf.html';

PROC PRINT data = stat481.penngolf NOOBS;
   title 'Some of the penngolf data set variables';
   ID name;
   var year type par yards;
RUN;

ODS HTML CLOSE;
ODS LISTING;

Some of the penngolf dataset variables
NameYearTypeParYards
Toftrees1968Resort727018
Penn State Blue1921Public726525
Centre Hills1921Private716392
Lewistown CC.Private726779
State College Elks1973SemiPri716369
Park Hills CC1966SemiPri706004
Sinking Valley CC1697SemiPri726755
Williamsport CC1909Private716489
Standing Stone GC1973SemiPri706593
Bucknell GC1960SemiPri706253
Mount Airy Lodge1972Resort727123

The first ODS statement tells SAS to close the Listing destination. The second ODS statement tells SAS to open the HTML destination and to save the HTML output generated by the PRINT procedure to the specified file name. The third ODS statement tells SAS to close the HTML destination so that we can access the created HTML file. And, the last ODS statement tells SAS to re-open the Listing destination.

Launch the SAS program, and edit the second ODS statement's FILE= option so that it reflects the location and name of the file where you want the resulting HTML output to be sent. (Again, make sure that you give your filename the standard .html extension.) Before you execute the program, you might want to clear your SAS output window of all previously generated listing output by activating the output window by clicking on it, selecting the Edit menu, and selecting Clear All. Now, run the SAS program to convince yourself that this time SAS generates just HTML output. That is, note, in particular, that the output window contains no listing output.

Closing Multiple ODS Destinations Concurrently Section

As we saw above in Example 24.1, one of the primary features of ODS is that you can simultaneously produce output in multiple formats by having more than one ODS destination open at a time. When you have more than one open ODS destination you can use the keyword shortcut _ALL_ to close all of the destinations concurrently. That is, the following statement:

ODS _ALL_ CLOSE;

closes all currently open destinations at once.