* USED FOR LOGIT MODEL; * Hscore = hardcode score; * Score = score generated internally by SAS; *First we read in a test data set for you; data mydata; input Y VAR1 VAR2 VAR3; cards; 1 10 33 4 0 20 21 3 1 30 59 2 1 20 76 1 0 30 24 3 0 20 22 2 1 10 28 2 1 10 49 2 0 30 76 2 1 20 59 2 0 30 45 5 0 30 21 4 0 30 49 2 0 10 23 4 1 10 55 2 0 20 29 3 1 10 49 2 0 20 45 5 0 20 24 3 0 10 30 3 0 10 24 5 1 10 59 2 0 30 31 2 0 20 33 4 0 30 22 3 0 30 29 3 0 10 30 3 0 20 28 3 1 30 59 2 0 30 56 2 1 30 77 1 1 20 97 2 0 20 23 4 0 30 25 4 0 30 23 4 0 30 88 1 0 30 49 2 1 30 76 1 1 20 67 1 1 10 55 2 0 20 26 2 1 20 49 2 1 20 68 1 0 30 45 2 1 20 68 1 0 20 32 5 1 30 22 1 1 30 55 2 1 20 66 1 0 20 29 3 ; *Next we estimate a simple logit model; proc logistic data=mydata outest=trythis descending; model y = var1 var2 var3 / selection=stepwise; run; %macro hardcode(myestimates,dev_out,val_out); OPTIONS NOTES SOURCE SOURCE2 MPRINT MLOGIC MERROR SYMBOLGEN; data coeff (keep=myvar coef); length myvar $32; set &myestimates end=last; where _type_='PARMS'; file 'c:\vars_1x3r5.txt'; put '%macro sel_var;'; * ---------------------------------------; * This rearranges the coefficients from the regression in an easier format to output; * Very handy little routine; array myarray{*} _numeric_; length myvar $32; do j = 1 to dim(myarray); if (myarray{j} ^= .) then do; call vname (myarray{j}, myvar); coef= (myarray{j}); if not(myvar in ("_LNLIKE_","_RMSE_")) then do; if myvar ^="INTERCEPT" then do; put myvar;output; end; end; end; end; if last then put '%mend sel_var;'; * ---------------------------------------; data coeff; set coeff end=last; if last then call symput ('nvars',_n_); /* Now we have number of parameters in a macro var */ run; * Write out hardcode in a macro so you can bring it back in and score the dataset; data coeff ; set coeff end=last; file 'c:\hardcode_xc8f4.txt'; if _n_=1 then do; put '%macro myhardcode;'; put 'hscore = '; put ' ' coef ' + '; end; if _n_>1 and _n_ <&nvars then put ' ' myvar ' * ' coef ' + '; if last then do; put ' ' myvar ' * ' coef';'; *Now this turns it into the logistic probability rather than log odds; put "hscore = 1 /(1+exp(-(hscore)));"; put '%mend myhardcode;'; end; run; *Scoring dataset with SAS code for Logit model; data mydata_scored; set mydata; %include 'c:\hardcode_xc8f4.txt'; %myhardcode; Title Dataset is Scored with Logit Model; proc print data=mydata_scored (obs=10); run; %mend hardcode; %hardcode(trythis,mydata,mydata);