Output Data from SAS Procedures

Many SAS procedures make important statistics available as output data sets, either with an OUTPUT statement, or with an option on the PROC statement. With ODS commands (Output Delivery System), every table produced by every procedure can be returned as a data set.

(This same group of ODS commands also makes it very simple to save your output in many document formats and also to create many statistical graphs with little effort.)

Creating Data Sets with ODS

ODS divides procedure output into components called output objects. Look at the following default output from the TTEST procedure:

data scores;
   input Gender $ Score @@;
   datalines;
f 75  f 76  f 80  f 77  f 80  f 77  f 73
m 82  m 80  m 85  m 85  m 78  m 87  m 82
;
proc ttest data=scores;
  class Gender;
  var Score;
  run;
                            The TTEST Procedure
 
                             Variable:  Score

   Gender        Method            N        Mean     Std Dev     Std Err

   f                               7     76.8571      2.5448      0.9619
   m                               7     82.7143      3.1472      1.1895
   Diff (1-2)    Pooled                  -5.8571      2.8619      1.5298
   Diff (1-2)    Satterthwaite           -5.8571                  1.5298

            Gender        Method            Minimum     Maximum

            f                               73.0000     80.0000
            m                               78.0000     87.0000
            Diff (1-2)    Pooled                               
            Diff (1-2)    Satterthwaite                        

 Gender        Method               Mean       95% CL Mean        Std Dev

 f                               76.8571     74.5036  79.2107      2.5448
 m                               82.7143     79.8036  85.6249      3.1472
 Diff (1-2)    Pooled            -5.8571     -9.1902  -2.5241      2.8619
 Diff (1-2)    Satterthwaite     -5.8571     -9.2064  -2.5078            

             Gender        Method             95% CL Std Dev

             f                                1.6399   5.6039
             m                                2.0280   6.9303
             Diff (1-2)    Pooled             2.0522   4.7242
             Diff (1-2)    Satterthwaite                     

        Method           Variances        DF    t Value    Pr > |t|

        Pooled           Equal            12      -3.83      0.0024
        Satterthwaite    Unequal      11.496      -3.83      0.0026

                           Equality of Variances
 
             Method      Num DF    Den DF    F Value    Pr > F

             Folded F         6         6       1.53    0.6189

SAS produces four output tables:

  • “Statistics” (wrapped as multiple lines)
  • “Confidence Limits” (also wrapped)
  • “T-Tests”
  • and “Equality of Variances”

Any table can be saved as a SAS data set once you know the ODS name for the table. But as you can see in the preceding output, these are not always obvious.

Determining the Names of Output Tables

Use the ODS TRACE ON and ODS TRACE OFF statements to determine the names of procedure tables (you can also find them in the SAS Help documentation for each procedure, under “Details”). These give you the table names in the Log:

ods trace on;

proc ttest data=scores;
  class Gender;
  var Score;
  run;

ods trace off;
1                             The SAS System


Output Added:
-------------
Name:       Statistics
Label:      Statistics
Template:   Stat.TTest.Statistics
Path:       Ttest.Score.Statistics
-------------

Output Added:
-------------
Name:       ConfLimits
Label:      Confidence Limits
Template:   Stat.TTest.ConfLimits
Path:       Ttest.Score.ConfLimits
-------------

Output Added:
-------------
Name:       TTests
Label:      T-Tests
Template:   Stat.TTest.TTests
Path:       Ttest.Score.TTests
-------------

Output Added:
-------------
Name:       Equality
Label:      Equality of Variances
Template:   Stat.TTest.Equality
Path:       Ttest.Score.Equality
-------------
NOTE: The PROCEDURE TTEST printed page 1.

What we need are the “Names.”

Redirecting the Output

Once you know the name of the table you want to make a SAS data set from, use an ODS OUTPUT statement to save the results.

The basic specification is

ODS OUTPUT ods-table = dataset;

For example, to save the “T-tests” table as a data set:

ods output ttests=tstats;
proc ttest data=scores;
  class Gender;
  var Score;
  run;

Below is a listing of the six variables and two observations that got written to the TSTATS data set:

proc print data=tstats;
run;
  Obs   Variable   Method          Variances    tValue       DF    Probt

   1     Score     Pooled           Equal        -3.83       12   0.0024
   2     Score     Satterthwaite    Unequal      -3.83   11.496   0.0026

Creating Data Sets with PROC Options

Many SAS statistical PROCs also provide options to save values calculated by the procedure into data sets. This is typically done with an option on the main PROC statement, or by including an OUTPUT statement as part of the PROC.

These are not options with PROC TTEST, but they are both options with PROC GLM.

Additional Stats with a PROC Option

If we fit our t-test as a general linear model, we can save the fit statistics by adding the OUTSTAT=dataset option.

This saves the F-test statistics. (Notice that F is the square of the t value above, and the the probability is the same as the classic “pooled” results.)

proc glm data=scores noprint outstat=glmfit;
  class Gender;
  model score = Gender / ss3;
  run;

proc print data=glmfit;
run;
  Obs   _NAME_   _SOURCE_   _TYPE_   DF      SS        F            PROB

   1    Score     ERROR     ERROR    12    98.286     .       .         
   2    Score     Gender    SS3       1   120.071   14.6599   .002400695

OUTPUT Statement

Adding an OUTPUT statement to PROC GLM provides a method of saving fitted values and diagnostic measures.

proc glm data=scores noprint;
  class Gender;
  model score = Gender / ss3;
  output out=fitted predicted=scorehat residual=residual;
  run;

proc print data=fitted;
run;
              Obs    Gender    Score    scorehat    residual

                1      f         75      76.8571    -1.85714
                2      f         76      76.8571    -0.85714
                3      f         80      76.8571     3.14286
                4      f         77      76.8571     0.14286
                5      f         80      76.8571     3.14286
                6      f         77      76.8571     0.14286
                7      f         73      76.8571    -3.85714
                8      m         82      82.7143    -0.71429
                9      m         80      82.7143    -2.71429
               10      m         85      82.7143     2.28571
               11      m         85      82.7143     2.28571
               12      m         78      82.7143    -4.71429
               13      m         87      82.7143     4.28571
               14      m         82      82.7143    -0.71429

Last Revised: 7/11/2024