Control Output Destination in SAS

In the blog post  Control Your Output with ODS Select and Exclude, I demonstrate how to use the trace and ODS select/exclude statements to control what procedure results to output. In this post, I demonstrate how to control where SAS writes it to.

ODS Destinations

First off, if you are not very familiar with the SAS Output Delivery System, you may not pay much attention to where your results go. I sure did not when I first started using SAS. When I first started learning SAS, I merely accepted that the procedure results was written to the Results Window in SAS 9.3 and forward. However, with just a few lines of code, you can easily control the destination of your SAS output.

The default output destination in SAS 9.3 and forward is the HTML destination (The Results Window). Before that, the LISTING destination was the default. When we work with output destinations in SAS, a very typical way to work is this: Open some destination for output, Run a procedure that creates an output, close the destination again. A simple example is

ods html;                        /* Open HTML destination for output          */
proc sgplot data=sashelp.class;  /* Run some procedure that creates an output */
   scatter x=height y=weight;
run;
ods html close;                  /* Close the destination again               */

The method described above is called the ODS Sandwich and is very good to keep in mind. It is good programming practice to keep track of your output destinations. When you open a destination it is open until you close it again. Therefore, you may forget to close some destination and write a lot of output to the destination, which should not go there.

Needless to say, the HTML and LISTING destinations are not the only output destinations available. You can output to virtually any destination outside SAS that you can think of. Frequently used ODS destinations are HTML, LISTING, PDF, RTF, PRINTER and POWERPOINT.

Multiple Destinations

Next, Let us look at an example of two procedures, which both create output. Lets us assume that we want both output outputted to the HTML destination but only one of them to the PDF destination. In an example like this it is very important to strictly keep track of your output destinations as follows

ods html;                        /* Open HTML destination for output           */
ods pdf;                         /* Open PDF destination for output            */
 
proc print data=sashelp.class;   /* This is output to HTML and PDF             */
run;
 
ods pdf close;                   /* Close PDF destination again                */
 
proc sgplot data=sashelp.class;  /* This is only output to HTML destination    */
run;  
 
ods html close;                  /* Close the HTML destination again           */

Needless to say, you can imagine situations much more complicated than this where it is even more important to keep strict control over what ODS destinations are open for output and what are not.

Write Only Latest Procedure Output to Destination

When you write to an output destination like PDF, you may want to have only the output from the latest procedure run in the destination. This can be done with the following lines of code using the Output Delivery System PREFERENCE option.

ods pdf preferences;
ods pdf close;
ods pdf newfile=proc;
 
proc sgplot data=sashelp.class;
   scatter x=height y=weight;
run;

Finally, this technique is demonstrated in the blog post Clear Log and Results Window.

Save Your SAS Output to a File

A frequent use of output destinations is to save procedure output to a specified directory in a specific file format. A Simple example is goes as follows

ods pdf file="MyPath.pdf";       /* Open PDF destination for output and specify FILE= option */
proc sgplot data=sashelp.class;  /* Run some procedure that creates an output                */
   scatter x=height y=weight;
run;
ods pdf close;                   /* Close PDF destination again                              */

This code creates a simple scatter plot and saves it in a PDF file in the MyPath directory. Needless to say, this technique also works for other file formats.

Summary

The Output Delivery System lets you control where the procedure output goes, and how. As you have seen in this post, it is important to keep track of what destinations are open and what are not. Especially if you are working with many different destinations.

For a great introduction to the SAS Output Delivery System, I recommend the book ODS Techniques: Tips for Enhanching Your SAS Output, which I review in the blog post What SAS Books Are on my Shelf?. Furthermore, check out the SAS documentation page Understanding ODS Destinations.

Finally, you can download the entire code from this post here.