![]() | ![]() | ![]() |
There are situations which require you to run a procedure many times. For the special case where you need to run a procedure on separate blocks of observations in a data set, you can simply add a BY statement to your procedure call. But suppose you want to fit a series of regression models with either a different response variable, or different predictors, or both. You would obviously like to avoid having to write a separate procedure step for each model.
Your initial thought might be to use a macro, and that is certainly one approach you could take. Another approach which is quite flexible is to use CALL EXECUTE in a DATA step to generate the multiple procedure steps and run them. With this approach you can run the procedure once for each observation in a data set, using the information that you place in the observations.
For example, suppose you want to fit a series of single-predictor logistic regression models using the following data set. Each model will use one of the predictors to model the response variable, REMISS. Suppose that what you want in the end is a single data set that contains the intercept and slope for each of the models, along with tests and confidence intervals for each of the parameters. These statements create the data set.
data remiss;
input remiss cell smear infil li blast temp;
datalines;
1 .8 .83 .66 1.9 1.1 .996
1 .9 .36 .32 1.4 .74 .992
0 .8 .88 .7 .8 .176 .982
0 1 .87 .87 .7 1.053 .986
1 .9 .75 .68 1.3 .519 .98
0 1 .65 .65 .6 .519 .982
1 .95 .97 .92 1 1.23 .992
0 .95 .87 .83 1.9 1.354 1.02
0 1 .45 .45 .8 .322 .999
0 .95 .36 .34 .5 0 1.038
0 .85 .39 .33 .7 .279 .988
0 .7 .76 .53 1.2 .146 .982
0 .8 .46 .37 .4 .38 1.006
0 .2 .39 .08 .8 .114 .99
0 1 .9 .9 1.1 1.037 .99
1 1 .84 .84 1.9 2.064 1.02
0 .65 .42 .27 .5 .114 1.014
0 1 .75 .75 1 1.322 1.004
0 .5 .44 .22 .6 .114 .99
1 1 .63 .63 1.1 1.072 .986
0 1 .33 .33 .4 .176 1.01
0 .9 .93 .84 .6 1.591 1.02
1 1 .58 .58 1 .531 1.002
0 .95 .32 .3 1.6 .886 .988
1 1 .6 .6 1.7 .964 .99
1 1 .69 .69 .9 .398 .986
0 1 .73 .73 .7 .398 .986
;
Since PROC LOGISTIC will be run once for each of the predictor variables, we need a data set that contains a variable holding the names of the predictors. This could be done by specifying all of the names as data in a DATA step.
data names;
input var $ @@;
datalines;
cell smear infil li blast temp
;
The above approach might not be very practical for an existing data set with a large number of variables. An alternative is the following DATA step which only requires that you specify the first and last variables in the form of a variable list. The ARRAY statement, DO loop, and VNAME function pull the names of the variables and store them in a variable (VAR).
data names;
set remiss;
length var $ 32;
keep var;
array vnames (*) cell--temp;
do i=1 to dim(vnames);
var=vname(vnames(i));
output;
end;
stop;
run;
Regardless of which method is used to create a data set variable containing the predictor names, the following step is used to generate the statements for each PROC LOGISTIC step and then executes them. The ODS OUTPUT statement with the PERSIST option accumulates the Parameter Estimates tables in data set PE across all of the PROC LOGISTIC runs. Since this DATA step only produces code and runs it rather than creating a data set, DATA _NULL_ is specified. The SET statement assures that this DATA step will iterate for each observation in the NAMES data set. That is, it will iterate for each predictor name. The CALL EXECUTE statement runs the code in parentheses. Note that the code is a simple concatenation of three text strings: the PROC LOGISTIC statement and the MODEL statement up to the equal sign, the predictor name in the current observation of the NAMES data set, and the end of the MODEL statement and the RUN statement. The double vertical bars ( || ) are the string concatenation operator. For each iteration of this DATA step, the variable VAR in the NAMES data set contains the next predictor name and it is inserted into the MODEL statement. The text string is then run by CALL EXECUTE resulting in PROC LOGISTIC fitting a model with that predictor. The resulting Parameter Estimates table is added to the PE data set by the ODS OUTPUT statement.
ods output parameterestimates(persist)=pe;
data _null_;
set names;
call execute("proc logistic data=remiss;
model remiss =" || var || ";
run;"
);
run;
ods output close;
The SAS Log shows the execution of each PROC LOGISTIC step (notes produced by each executed step are also displayed but are omitted here for clarity):
1 + proc logistic data=data.remiss; model remiss =cell ;
run;
2 + proc logistic data=data.remiss; model remiss =smear ;
run;
3 + proc logistic data=data.remiss; model remiss =infil ;
run;
4 + proc logistic data=data.remiss; model remiss =li ;
run;
5 + proc logistic data=data.remiss; model remiss =blast ;
run;
6 + proc logistic data=data.remiss; model remiss =temp ;
run;
|
At completion, the accumulated parameter estimates tables are in data set PE.
proc print data=pe noobs;
run;
Each pair of observations contains the intercept and slope for each of the six models fit by the preceding DATA step.
|
In this simple example, only a single substitution (the predictor name) is made to the code of the procedure at each run. But you could use this method for much more complex situations requiring more substitutions. You would simply need more variables in the input data set to specify all the information needed for each run.
| Product Family | Product | System | SAS Release | |
| Reported | Fixed* | |||
| SAS System | SAS/STAT | z/OS | ||
| OpenVMS VAX | ||||
| Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
| Microsoft Windows XP 64-bit Edition | ||||
| Microsoft® Windows® for x64 | ||||
| OS/2 | ||||
| Microsoft Windows 8 | ||||
| Microsoft Windows 95/98 | ||||
| Microsoft Windows 2000 Advanced Server | ||||
| Microsoft Windows 2000 Datacenter Server | ||||
| Microsoft Windows 2000 Server | ||||
| Microsoft Windows 2000 Professional | ||||
| Microsoft Windows 2012 | ||||
| Microsoft Windows NT Workstation | ||||
| Microsoft Windows Server 2003 Datacenter Edition | ||||
| Microsoft Windows Server 2003 Enterprise Edition | ||||
| Microsoft Windows Server 2003 Standard Edition | ||||
| Microsoft Windows Server 2003 for x64 | ||||
| Microsoft Windows Server 2008 | ||||
| Microsoft Windows Server 2008 for x64 | ||||
| Microsoft Windows XP Professional | ||||
| Windows 7 Enterprise 32 bit | ||||
| Windows 7 Enterprise x64 | ||||
| Windows 7 Home Premium 32 bit | ||||
| Windows 7 Home Premium x64 | ||||
| Windows 7 Professional 32 bit | ||||
| Windows 7 Professional x64 | ||||
| Windows 7 Ultimate 32 bit | ||||
| Windows 7 Ultimate x64 | ||||
| Windows Millennium Edition (Me) | ||||
| Windows Vista | ||||
| Windows Vista for x64 | ||||
| 64-bit Enabled AIX | ||||
| 64-bit Enabled HP-UX | ||||
| 64-bit Enabled Solaris | ||||
| ABI+ for Intel Architecture | ||||
| AIX | ||||
| HP-UX | ||||
| HP-UX IPF | ||||
| IRIX | ||||
| Linux | ||||
| Linux for x64 | ||||
| Linux on Itanium | ||||
| OpenVMS Alpha | ||||
| OpenVMS on HP Integrity | ||||
| Solaris | ||||
| Solaris for x64 | ||||
| Tru64 UNIX | ||||
| SAS System | SAS/ETS | z/OS | ||
| OpenVMS VAX | ||||
| Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||
| Microsoft Windows XP 64-bit Edition | ||||
| Microsoft® Windows® for x64 | ||||
| OS/2 | ||||
| Microsoft Windows 8 | ||||
| Microsoft Windows 95/98 | ||||
| Microsoft Windows 2000 Advanced Server | ||||
| Microsoft Windows 2000 Datacenter Server | ||||
| Microsoft Windows 2000 Server | ||||
| Microsoft Windows 2000 Professional | ||||
| Microsoft Windows 2012 | ||||
| Microsoft Windows NT Workstation | ||||
| Microsoft Windows Server 2003 Datacenter Edition | ||||
| Microsoft Windows Server 2003 Enterprise Edition | ||||
| Microsoft Windows Server 2003 Standard Edition | ||||
| Microsoft Windows Server 2003 for x64 | ||||
| Microsoft Windows Server 2008 | ||||
| Microsoft Windows Server 2008 for x64 | ||||
| Microsoft Windows XP Professional | ||||
| Windows 7 Enterprise 32 bit | ||||
| Windows 7 Enterprise x64 | ||||
| Windows 7 Home Premium 32 bit | ||||
| Windows 7 Home Premium x64 | ||||
| Windows 7 Professional 32 bit | ||||
| Windows 7 Professional x64 | ||||
| Windows 7 Ultimate 32 bit | ||||
| Windows 7 Ultimate x64 | ||||
| Windows Millennium Edition (Me) | ||||
| Windows Vista | ||||
| Windows Vista for x64 | ||||
| 64-bit Enabled AIX | ||||
| 64-bit Enabled HP-UX | ||||
| 64-bit Enabled Solaris | ||||
| ABI+ for Intel Architecture | ||||
| AIX | ||||
| HP-UX | ||||
| HP-UX IPF | ||||
| IRIX | ||||
| Linux | ||||
| Linux for x64 | ||||
| Linux on Itanium | ||||
| OpenVMS Alpha | ||||
| OpenVMS on HP Integrity | ||||
| Solaris | ||||
| Solaris for x64 | ||||
| Tru64 UNIX | ||||
| Type: | Usage Note |
| Priority: | |
| Topic: | Analytics ==> analytics |
| Date Modified: | 2012-07-03 15:35:48 |
| Date Created: | 2012-06-19 17:35:36 |




