Getting Started Example for PROC AUTOREG

/*--------------------------------------------------------------

                    SAS Sample Library

        Name: autgs.sas
 Description: Example program from SAS/ETS User's Guide,
              The AUTOREG Procedure
       Title: Getting Started Example for PROC AUTOREG
     Product: SAS/ETS Software
        Keys: autoregression
        PROC: AUTOREG
       Notes:

--------------------------------------------------------------*/

/* Regression with Autocorrelated Errors */
data a;
   ul = 0; ull = 0;
   do time = -10 to 36;
      u = + 1.3 * ul - .5 * ull + 2*rannor(12346);
      y = 10 + .5 * time + u;
      if time > 0 then output;
      ull = ul; ul = u;
   end;
run;

title 'Autocorrelated Time Series';
proc sgplot data=a noautolegend;
   series x=time y=y / markers;
   reg x=time y=y/  lineattrs=(color=black);
run;

proc autoreg data=a;
   model y = time;
run;

ods graphics on;
proc autoreg data=a;
   model y = time / nlag=2 method=ml;
run;

proc autoreg data=a;
   model y = time / nlag=2 method=ml;
   output out=p p=yhat pm=trendhat;
run;

title 'Predictions for Autocorrelation Model';
proc sgplot data=p;
   scatter x=time y=y / markerattrs=(color=blue);
   series x=time y=yhat / lineattrs=(color=blue);
   series x=time y=trendhat / lineattrs=(color=black);
run;

data b;
   y = .;
   do time = 37 to 46; output; end;
run;

data b;
   merge a b;
   by time;
run;

proc autoreg data=b;
   model y = time / nlag=2 method=ml;
   output out=p p=yhat pm=ytrend
                lcl=lcl ucl=ucl;
run;

title 'Forecasting Autocorrelated Time Series';
proc sgplot data=p;
   band x=time upper=ucl lower=lcl;
   scatter x=time y=y;
   series x=time y=yhat;
   series x=time y=ytrend / lineattrs=(color=black);
run;

/*-- Durbin-Watson test for autocorrelation --*/
proc autoreg data=a;
   model y = time / dw=4 dwprob;
run;

data b;
   set a;
   ylag = lag1( y );
run;

proc autoreg data=b;
   model y = ylag / lagdep=ylag;
run;

/*-- stepwise autoregression --*/
proc autoreg data=a;
   model y = time / method=ml nlag=5 backstep;
run;

data a;
   do time = -10 to 120;
      s = 1 + (time >= 60 & time < 90);
      u = s*rannor(12346);
      y = 10 + .5 * time + u;
      if time > 0 then output;
      end;
run;

title 'Heteroscedastic Time Series';
proc sgplot data=a noautolegend;
   series x=time y=y / markers;
   reg x=time y=y / lineattrs=(color=black);
run;

/*-- test for heteroscedastic OLS residuals --*/
proc autoreg data=a;
   model y = time / archtest;
   output out=r r=yresid;
run;

/*-- data with outliers at observation 10 --*/
data b;
   do time = -10 to 120;
      s = 1 + (time >= 60 & time < 90);
      u = s*rannor(12346);
      y = 10 + .5 * time + u;
      if time = 10 then
         do; y = 200; end;
      if time > 0 then output;
   end;
run;
/*-- test for heteroscedastic OLS residuals --*/
proc autoreg data=b;
   model y = time / archtest=(qlm) ;
   model y = time / archtest=(lk,wl) ;
run;

data c;
   ul=0; ull=0;
   do time = -10 to 120;
      s = 1 + (time >= 60 & time < 90);
      u = + 1.3 * ul - .5 * ull + s*rannor(12346);
      y = 10 + .5 * time + u;
      if time > 0 then output;
      ull = ul; ul = u;
      end;
run;
title 'AR(2)-GARCH(1,1) model for the Y series regressed on TIME';
proc autoreg data=c;
   model y = time / nlag=2 garch=(q=1,p=1) maxit=50;
   output out=out cev=vhat;
run;

data out;
   set out;
   shat = sqrt( vhat );
run;

title 'Predicted and Actual Standard Deviations';
proc sgplot data=out noautolegend;
   scatter x=time y=s;
   series x=time y=shat/ lineattrs=(color=black);
run;

/*-- AR(2)-EGARCH(1,1) model --*/
proc autoreg data=a;
   model y = time / nlag=2 garch=(p=1,q=1,type=exp);
run;

/*-- test for stationarity of regression residuals --*/
proc autoreg data=a;
   model y=  / stationarity = (KPSS);
run;

/*-- test for stationarity using quadratic
     spectral kernel and automatic bandwidth selection --*/
proc autoreg data=a;
   model y= /
   stationarity = (KPSS=(KERNEL=QS AUTO));
run;