SAS/ETS Examples
Bivariate Granger Causality Test
Contents |
Back to Example
/*-----------------------------------------------------------------
Example: Bivariate Granger Causality Test
Requires: SAS/ETS, SAS/IML, SAS/GRAPH
Version: 9.0
------------------------------------------------------------------*/
title 'Bivariate Granger Causality Test';
data gdp;
set sashelp.citiqtr;
keep date gdpq;
run;
data gp;
set sashelp.citimon;
keep date eegp;
run;
proc expand data=gp out=temp from=month to=qtr;
convert eegp / observed=average;
id date;
run;
data combined;
merge gdp temp;
by date;
run;
data causal;
set work.combined;
gdpq_1 = lag(gdpq);
gdpq_2 = lag2(gdpq);
eegp_1 = lag(eegp);
eegp_2 = lag2(eegp);
run;
/** Granger Form **/
* unrestricted model;
proc autoreg data=causal;
model gdpq = gdpq_1 gdpq_2 eegp_1 eegp_2;
output out=out1 r=e1; /* output residuals */
run;
* restricted model;
proc autoreg data=out1;
model gdpq = gdpq_1 gdpq_2;
output out=out2 r=e0; /* output residuals */
run;
ods select Iml._LIT1010
Iml.TEST1_P_VAL1
Iml.TEST2_P_VAL2;
ods html body='exgran01.htm';
* compute test;
proc iml;
start main;
use out1;
read all into e1 var{e1};
close out1;
use out2;
read all into e0 var{e0};
close out2;
p = 2; /* # of lags */
T = nrow(e1); /* # of observations */
sse1 = ssq(e1);
sse0 = ssq(e0);
* F test;
test1 = ((sse0 - sse1)/p)/(sse1/(T - 2*p - 1));
p_val1 = 1 - probf(test1,p,T - 2*p - 1);
* asymtotically equivalent test;
test2 = (T * (sse0 - sse1))/sse1;
p_val2 = 1 - probchi(test2,p);
print "IML Result",, test1 p_val1,,
test2 p_val2;
finish;
run;
quit;
ods html close;
* Plot of the two series;
data trans;
set combined;
keep date gdpq eegp;
obs = _n_;
gdpq = (gdpq - 3600)/1400;
eegp = (eegp - 80)/70;
run;
title1 ' GDP and Gasoline Price';
axis1 label=(angle=90 'Gross Domestic Product')
order=(0 to 1 by .142) minor=none major=none offset=(0,0)
value=('3600' '3800' '4000' '4200' '4400' '4600' '4800' '5000');
axis2 label=(angle=90 'Gasoline Price')
order=(0 to 1 by .142) minor=none major=none offset=(0,0)
value=('80' '90' '100' '110' '120' '130' '140' '150');
axis3 label=('Date');
symbol1 color=red interpol=join value=none;
symbol2 color=blue interpol=join value=none;
proc gplot data=trans;
format date year4.;
plot gdpq*date / cframe=ligr
vaxis=axis1 haxis=axis3;
plot2 eegp*date / cframe=ligr
vaxis=axis2;
run;
quit;