Reading ODS Tables |
In Chapter 6, "Adding Curves to Plots," you generated output data sets by using SAS procedures and learned how to read variables from the server into the DataObject and into IML matrices. This chapter shows you how to read information from tables into an IMLPlus program.
The program statements in this chapter are distributed with Stat Studio. To open the program containing the statements:
The key to this chapter is knowing how to create SAS data sets directly from output tables by using the Output Delivery System (ODS). When you run a SAS procedure, the procedure typically creates one or more tables. By default, ODS sends the tables to the SAS listing, which in Stat Studio is called the output windows. But you can also tell ODS to send a table to a SAS data set. By subsequently reading the data set, you can read statistics from tables into your IMLPlus programs.
Suppose you perform a linear regression analysis with the REG procedure, and you want to read certain statistics into IML matrices. You might want to use the statistics in future computations, or you might want to use them in a title or legend for a plot.
To be specific, suppose you want to read the following statistics into your IMLPlus program: the number of observations used in the regression, the R-square value of the fitted model, and the slope and intercept of the least squares line.
Your first task is to determine the name of the ODS tables that contain the information you need. The "Details" section of the documentation for PROC REG includes a section on the names of ODS tables. You can also have ODS display the names by including the ODS TRACE ON statement before running your PROC REG statement.
This approach is illustrated in the following program.
Type or copy the following statements into the program window, and select
Program Run from the main menu.
declare DataObject dobj; dobj = DataObject.CreateFromFile("Hurricanes"); dobj.WriteVarsToServerDataSet( {"wind_kts" "min_pressure"}, "work", "Hurr", true ); submit; ods trace on / listing; proc reg data=Hurr; model wind_kts = min_pressure; run; ods trace off; endsubmit;
Figure 7.1: Printing ODS Tables Names
The output is shown in Figure 7.1.
The LISTING option in the ODS TRACE ON statement tells ODS to send the
names of tables to the output window rather than to the SAS log. By
scrolling through the output, you can see that the number of
observations used in the regression is part of the NObs table. The R
square value of the fitted model is contained in the FitStatistics
table. The slope and intercept of the least squares line are contained
in the ParameterEstimates table. The following example uses the ODS
OUTPUT statement to write these tables to data sets in work.
Replace the SUBMIT/ENDSUBMIT block of statements in the
program window with the following revised statements. Then select
Program Run from the main menu.
submit; proc reg data=Hurr; model wind_kts = min_pressure; ods output NObs = RegNObs FitStatistics = RegFitStat ParameterEstimates = RegParamEst; run; endsubmit;
These statements cause the REG procedure to create three new data sets in the work library. You can examine each data set by opening it from the server into a data table as follows:
Figure 7.2: Opening a Server Data Set
You might notice that some data sets look different from the
corresponding tables that are displayed in the output window. Some
tables have nonprinting columns
that are not visible
in the output window but are written to the data set.
By opening each data set in turn, you can determine the name of the variable containing the information that you want to read into IML. The number of observations used in the regression is contained in the N variable in the REGNOBS data set. The R-square value of the fitted model is contained in the NVALUE2 variable in the REGFITSTAT data set. The slope and intercept of the least squares line are contained in the Estimate variable of the REGPARAMEST data set.
The SAS/IML language provides statements to read and write server data to and from IML matrices. The USE statement opens a SAS data set, and the READ statement reads server data into IML matrices. Add the following statements at the bottom of the program window, and select Program Run from the main menu.
use RegNObs; read all var {N}; close RegNObs; use RegFitStat; read all var {Label2 NValue2}; /* name of statistic and value */ close RegFitStat; use RegParamEst; read all var {Variable Estimate}; /* var name and coefficient */ close RegParamEst; print "--------------------------------------"; print N[ rowname={"Num Read","Num Used","Num Missing"} ], NValue2[ rowname=Label2 ], Estimate[ rowname=Variable ];
The output is shown in Figure 7.3. Statistics from the REG tables are now contained in IML matrices. You can use these matrices in computations or, as the next chapter shows, in a title or legend for a plot.
Note: You can often read in the names of statistics from the ODS data
set, as the Label2 and Variable matrices in the preceding
example demonstrate.
Figure 7.3: Creating Matrices from ODS Tables