Capturing output from any procedure with an ODS OUTPUT statement

1

There's an old song that starts out, "You Can Get Anything You Want at Alice's Restaurant."  Well, maybe you are too young to know that song, but if you’re a SAS users, you’ll be glad to know that you can capture anything produced by any SAS procedure (even if the procedure does not have an OUTPUT statement or does not output the values you need), with an ODS OUTPUT statement.

I used this technique in my latest book, Cody's Data Cleaning Techniques, third edition, to compute trimmed statistics using PROC UNIVARIATE.  If you are not familiar with the term “trimmed statistics,” it means to compute statistics such as means and standard deviations after trimming off values from the top and bottom of a distribution.  PROC UNIVARIATE has an OUTPUT statement, but it does not output a trimmed mean or trimmed standard deviation.

To determine the name of the PROC UNIVARIATE output object that contains trimmed statistics, start out by running the following:

ods trace on/listing;
 
proc univariate data=sashelp.cars(keep=Invoice) trim=.1;
   *The TRIM option used here will trim 10% from the top
    and bottom of the distribution;
   var Invoice;
run;
 
ods trace off;

 

This places the names of the output objects directly in the output.  Below is part of the output produced by this program:

Right before the reporting of trimmed statistics, you see TrimmedMeans, the name of the output object you want.  The next step is to use the ODS OUTPUT statement to place the trimmed statistics in a SAS data set, like this:

ods output TrimmedMeans = Trimmed;
 
proc univariate data=sashelp.Cars(keep=Invoice) trim=.1;
   *The TRIM option used here will trim 10% from the top
    and bottom of the distribution;
   var Invoice;
run;
 
ods output close;

 

Don't forget to close the output (just as you would close HTML or PDF). What's in data set Trimmed? Let's use PROC PRINT to find out.

title "Listing of Data Set Trimmed";
proc print data=Trimmed noobs;
run;

 

Here is the output:

You now have your trimmed statistics in a data set.  The three variables I needed from this data set were Mean (trimmed mean), StdMean (the standard error), and DF (degrees of freedom).

In summary, the ODS OUTPUT statement is a powerful tool that you can use to capture any values from any procedure and place these values in a SAS data set for further use.

Feels good to get what you want, don't you think?

Share

About Author

Ron Cody

Private Consultant

Dr. Ron Cody was a Professor of Biostatistics at the Rutgers Robert Wood Johnson Medical School in New Jersey for 26 years. During his tenure at the medical school, he taught biostatistics to medical students as well as students in the Rutgers School of Public Health. While on the faculty, he authored or co-authored over a hundred papers in scientific journals. His first book, Applied Statistics and the SAS Programming Language, was first published by Prentice Hall in 1985 and is now in its fifth edition. Since then, he has published over a dozen books on SAS programming and statistical analysis using SAS. His latest book, A Gentle Introduction to Statistics Using SAS Studio was published this year. Ron has presented numerous papers at SAS Global forums, regional conferences, as well as local user groups. He is presently a contract instructor for SAS Institute and continues to write books on SAS and statistical topics.

Related Posts

Back to Top