/************************************************************************************************** Program name: MI_SLOGIT_STEPWISE.sas Programmers: Qixuan Chen, Ph.D. (qc2138@columbia.edu) Department of Biostatistics Mailman School of Public Health Columbia University Xiaohui Jiang, M.S. (ljcindy@umich.edu) Department of Environmental Health Sciences University of Michigan School of Public Health Date: Sep. 3rd, 2008 Purpose: STEPWISE selections for multiply-imputed complex survey data with binary outcome Notes: (1) Make sure there is a variable called '_imputation_' in the data set before using this macro. (2) This macro need to call another macro, "%MI_SLOGIT". (3) The user need to specify the following macro variables: "dset" -- source data "outcome" -- outcome of the model "force" -- force in variables "varlist" -- the list of potential variables for selection, excluding the variables in "force" "catvarlist"-- the string of categorical variables in "varlist" "strata" -- strata in a survey sample "cluster" -- cluster in a survey sample "weight" -- sample weight "alpha1" -- the significance level for entry into the model, for example, 0.05 "alpha2" -- the significance level for staying in the model, for example, 0.05 "time" -- the maximum length of running time (in hours) "output" -- the output of the selected model using forward stepwise selection **************************************************************************************************/ %macro MI_SLOGIT_STEPWISE(dset, outcome, force, varlist, catVarlist, strata, cluster, weight, alpha1, alpha2, time, output); /* add timer*/ data _null_; start=time(); call symput("startTime",start); run; %put &startTime; %if "&force" ne " " %then %do; %let nforce=0; %do %while(%scan(&force, &nforce+1, %str( )) NE); %let nforce=%eval(&nforce+1); %let force&nforce=%scan(&force, &nforce); %end; %end; %else %do; %let force= ; %end; %let pvalue=0; %let newEnterVar=; %let enterVar=; %do %while (&pvalue <= &alpha1); %let nvar=0; %do %while(%scan(&varlist, &nvar+1, %str( )) NE); %let nvar=%eval(&nvar+1); %let var&nvar=%scan(&varlist, &nvar); %put &&var&nvar; %let var=&force &enterVar &&var&nvar; %put &var; %MI_SLOGIT(&dset, &outcome, &var, &catVarlist, &strata, &cluster, &weight, result, test); data MIestimate&nvar; set test; where var ="&&var&nvar"; run; %end; data MIestimate; set %do i=1 %to &nvar; MIestimate&i %end; ; run; proc datasets library=work; delete test %do i=1 %to &nvar; MIestimate&i %end;; quit; proc sort data=MIestimate; by Prob; run; data varlist; set MIestimate; if _N_=1 then do; call symput('pvalue',round(Prob,0.001)); if round(Prob,0.001)<=&alpha1 then do; call symput('newEnterVar', var); delete; end; else %let newEnterVar=; end; run; proc sql noprint; select var into :varlist separated by ' ' from varlist; quit; * add backward selection; %let enterVar=&enterVar &newEnterVar; %put pvalue of newly entered variable=&pvalue; %put newEnterVar=&newEnterVar; %put enterVar=&enterVar; %put varlist=&varlist; /* Start of the Backward selection*/ %let nvar_BW=0; %do %while(%scan(&enterVar, &nvar_BW+1, %str( )) NE); %let nvar_BW=%eval(&nvar_BW+1); %end; %let removeVar=; %let newRemoveVar=; %let varInModel=&force &enterVar; %let p=1; %do %while (&nvar_BW >= 0 and &p>&alpha2); %MI_SLOGIT(&dset, &outcome, &varInModel, &catVarlist, &strata, &cluster, &weight, result, test); %if &nforce>0 %then %do; data test; set test; if var in ("Intercept",%do i=1 %to %eval(&nforce-1); %unquote(%str(%'&&force&i%')), %end; %unquote(%str(%'&&force&nforce%'))) then delete; keep var Prob; run; %end; proc sort data=test; by descending Prob; run; data varlist; set test; if _N_=1 then do; call symput('p',round(Prob,0.001)); if round(Prob,0.001)>&alpha2 then do; call symput('newRemoveVar',var); delete; end; else %let newRemoveVar=; end; drop Prob; run; %let removeVar = &removeVar &newRemoveVar; proc sql noprint; select var into :varInModel separated by ' ' from varlist; quit; %let nvar_BW = %eval(&nvar_BW-1); %put newRemoveVar=&newRemoveVar; %put removeVar=&removeVar; %end; /*End of the Backward selection*/ %let varlist=&varlist &removeVar; %let enterVar=&varInModel; /* check timer */ data _null_; current=time(); call symput("currentTime",current); run; %put ¤tTime; %if ¤tTime >= %sysevalf(&startTime + &time*3600 ) %then %do; /* sas use second as the unit of time */ %abort; %end; /* end of time checking*/ %end; %let varInModel=&force &enterVar; %MI_SLOGIT(&dset, &outcome, &varInModel, &catVarlist, &strata, &cluster, &weight, result, test); %mend;