SAS Learning Post

Capturing output from any procedure with an ODS OUTPUT statement

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:

SAS Certifications Tutorials, SAS Certifications Guide, SAS Certificatons

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:

SAS Certifications Tutorials, SAS Certifications Guide, SAS Certificatons

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).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s