*Tobit Regression and Implementation code; *Answers checked and verified with SHAZAM econometrics software; data mydata; input Y X; cards; 0. 1 0. 2 0. 3 0. 4 3.134800 5 3.508000 6 0.8312000 7 8.006400 8 0. 9 0. 10 2.935200 11 3.904800 12 6.514400 13 5.977200 14 3.726000 15 10.41240 16 16.90640 17 9.296800 18 7.891600 19 14.21640 20 ; data mydata_2; /* Development data set*/ set mydata; IF Y = 0 THEN LOWER = .; ELSE LOWER = Y; run; PROC LIFEREG data=mydata_2 outest=mycoefficients ; /* coeffs written to mycoefficients */ MODEL (LOWER, Y) = X / D=NORMAL ; OUTPUT OUT=OUT; RUN; ****************************************************************************; *Now writing out Implementation Routine; data mycoefficients (keep=myvar coef); length myvar $32; set mycoefficients end=last; where _type_="PARMS"; file 'c:\sel_var_1234.txt'; put '%macro sel_var;'; array parametr{*} _numeric_; length myvar $32; do i = 1 to dim(parametr); if (parametr{i} ne .) then do; call vname (parametr{i}, myvar); coef= (parametr{i} ); if not(myvar in ("_LNLIKE_","DIST", "STATUS", "LOWER", "SHAPE1","_RMSE_")) then do; if upcase(myvar)="INTERCEPT" then call symput('const', trim(left(coef))); if myvar ^="INTERCEPT" then do; put myvar; output; end; end; end; end; if last then put '%mend sel_var;'; * ---------------------------------------; data mycoefficients; set mycoefficients end=last; if last then call symput ('nvars',_n_); /* Now we have number of parameters in a macro var */ run; * Write out hardcode for TOBIT Model. Note - SAS does not automatically compute; * predicted values for TOBIT. Therefore, you must use hardcode to score dev.; * and validation datasets.; data mycoefficients ; set mycoefficients end=last; file 'c:\hardcode_tobit_1234.txt'; if _n_=1 then do; put '%macro myhardcode_t;'; put '************************ TOBIT HARDCODE PROGRAM CODE ******************************;'; put '* Hardcode matches SHAZAM output which has internally generated predicted values;'; put '* Sigma = SCALE used for normalization;'; put '* Index = Begins as unnormalized coefficients and then divided by sigma;'; put '* Prob = Probability that the predicted value will be above ZERO;'; put '* Density = Cumulative Normal Density Function ;'; put '* Yhat_C = Predicted (conditional) value of Tobit Model if observ known to be above ZERO;'; put '* Yhat = Predicted value of Tobit Model;'; put '***************************************************************************************;'; put ' '; put 'index = '; put ' ' coef ' + '; end; if _n_>1 and _n_ <&nvars-1 then put ' ' myvar ' * ' coef ' + '; if _n_=&nvars-1 then put ' ' myvar ' * ' coef';'; if last then do; if myvar^='_SCALE_' then put ' ' myvar ' * ' coef';'; if myvar='_SCALE_' then put 'sigma=' coef ';'; put 'index=index/sigma;'; put 'prob=probnorm(index);'; put 'density=exp(-.5*index**2)/sqrt(2*3.1416);'; put 'yhat=(sigma*index*prob)+(sigma*density);'; put 'yhat_c=(sigma*index)+((sigma*density)/(prob));'; put '%mend myhardcode_t;'; end; run; data mydata_2; set mydata_2; %include 'c:\hardcode_tobit_1234.txt'; %myhardcode_t; run; title Tobit Model Results; proc print data=mydata_2;run;