/* The macro program below adds the guidelines to the output */
   /* of PROC REG and displays the observations that exceed the */
   /* cutoffs. The default cutoff values used are               */
   /*  o  twice the average hat diagonal element                */
   /*  o  an alpha level of .05 for the RSTUDENT outlier check  */
   /*  o  the 50th percentile of an F-distribution for CookÕs   */
   /* distance measure.                                         */
   /* As has been noted, users can change the cutoff values and */
   /* specify additional PROC REG and MODEL statement options.  */
   /* and MODEL statement options. Finally, users can output    */
   /* the results to a permanent or temporary data set.         */

options ls=80 nodate nonumber;
   %macro diagnose (
   libstat=,     indata=,       regopt=,
   depvar=,      indvars=,      modopt=,
   outopt=,      influ=influ,   results=results,
   hat_mult=2,   alpha_t=0.05,  alpha_f=0.5);
   &libstat;

proc reg data=&indata ®opt;
   model &depvar=&indvars / &modopt;
   output out=&influ press=pe
   h=hatdiag rstudent=rstudent
   cookd=cookd &outopt;

proc univariate data=&influ
                (where=(cookd^=. or hatdiag=1)) noprint;
   var &depvar pe hatdiag;
   output out=sspe css=ssty uss=dum1
          press sum=dum2 dum3 param;
   data &results;
      retain div 0;
      set &influ end=finish;
      if _n_=1 then set sspe;
         obs=_n_;
         if cookd^=. or hatdiag=1 then div=div+1;
            rsqpred=1-(press/ssty);
            r59=repeat("*",59);

      file print notitles;
      if finish;
         hatbig=(&hat_mult*param) / div;
         tval=1-((&alpha_t/div)/2);
         dof_t=div-param-1;
         chart1=tinv(tval,dof_t);
         dof_f=div-param;
         chart2=finv(&alpha_f,param,dof_f);
      
      if finish then put @15 r59 /
         @27 "GUIDELINES FOR INTERPRETATION:   "//
         @15  '  Cutoff value for hat matrix diagonal
              element is:' +3 hatbig 7.3 //
         @15  '     Cutoff |value| for the Rstudent
              statistic is:' +3 chart1 7.3 //
         @15  '       Cutoff value for Cooks distance
              measure is:' +3 chart2 7.3 //
         @15  '         The value of R-square predicted is:'
              +3 rsqpred 7.3 // @15 r59;

data what;
   set &influ end=finish;
   if _n_=1 then set &results (keep=chart2);
      retain ind 0;
      obs=_n_;
      file print notitles header=h;
      if cookd>chart2 then do;
      put @2 obs @13 hatdiag @35 rstudent 8.3
          @55 cookd 8.3 @70 '*****';
      ind=ind+1;
   end;
   if finish=1 and ind>0 then
      put // @2 "'*****' indicates that the Cook's Distance
          exceeds the appropriate cutoff.";
   if finish=1 and ind=0 then   
          put // @2 " The Cook's Distance does not
          exceed the cutoff for any observation.";
   return;
   h:
   put   @30 "Influence Diagnostics  "//
         @15 "X-Leverage" @37 "Rstudent"
         @55 "Cook's Distance" /
         @1  "Obs" @15 "(hat(ii))"
         @37 "Residual" @57 "Measure" /;
   return;
%mend;


   /* Note: the macro argument names are self evident. For      */
   /* example, to add, say, the PROC REG model options of       */
   /* Durbin Watson (dw), and 95% confidence intervals for      */
   /* the true mean of predicted Y (clm), simply enter:         */
   /*                                                           */
   /*    modopt=dw clm,                                         */
   /*                                                           */
   /* To change the Y outlier alpha level to .01, simply        */
   /* enter:                                                    */
   /*                                                           */
   /*    alpha_t=.01,                                           */
   /*                                                           */