24.2 - Opening and Closing ODS Destinations

Printer-friendly versionPrinter-friendly version

If you are perfectly content with your output being sent to the output window in the default listing format, then you don't have 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, HTML say, then you have to open the destination before the SAS code that 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 that 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. 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:

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 to 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 (right-click!) 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:

and the specifically requested HTML output:

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:

along the bottom of your SAS window.

Closing the Listing Destination

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:

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

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.