/* Granger Causality Test For Pooled Models */ %let p=4 ; /* Number of Lags */ %let yvar=YYYY ; /* Y variable */ %let mydatevar=Date ; /* Date Time Variable */ %let mygeo=_geocode ; /* Cross Sectional variable from pooled structure */ %let mydata=MYDATA; data MYDATA; input Date _Geocode $ YYYY Varone Vartwo Varthree Varfour VarFive; cards; 2001.3 AK 50.093 182341271 154279108.3 29745203.78 25106216.28 17928109.9 2001.4 AK 49.564 193858751 63269392.28 40472021.26 38825337.17 28047859.09 2002.1 AK 47.97 252930364 102275665.2 29138457.49 15350931.89 3918773.069 2002.2 AK 48.273 259933236 45980310.35 3791571.875 1982056.495 1737048.583 2002.3 AK 47.988 260391569 142925355.5 9901907.783 4150850.764 4028130.642 2002.4 AK 46.63 269793122 41897796.82 144852.9369 47703.38799 24004.33274 2003.1 AK 49.102 270472058 169327829.4 133093305.6 31616478.87 594283.8549 2003.2 AK 48.15 281454748 67956093.29 38697025.94 25280075.15 9163809.417 2003.3 AK 49.186 285458475 103331649.9 8061116.268 89954.46046 34482.37129 2003.4 AK 50.228 285146477 69083878.35 43465495.84 3610131.886 1051195.654 2004.1 AK 48.372 299360785 156301279.2 20812036.31 9994373.595 3539073.934 2004.2 AK 50.301 299541591 58273181.78 42262129.74 41848081.03 40379011.09 2004.3 AK 50.977 316826521 201298640 130234467 53090250.54 49950385.43 2004.4 AK 52.82 316928998 247149029.2 140360613.4 95255260.98 46291087.89 2005.1 AK 58.721 316592157 281927335 74024776.01 26033826.42 598951.226 2005.2 AK 62.134 314822662 16872151.62 13519967.39 10210535.75 9780010.209 2005.3 AK 65.201 324339135 114154055.1 58112959.74 24058425.78 5565011.202 2005.4 AK 73.037 324666406 35681840.77 27993880.64 26444495.38 14726393.68 2006.1 AK 77.174 325474518 48317359.74 40861537.3 26559994.93 16382503.6 2006.2 AK 79.607 285071632 8086754.762 7678161.549 403749.2049 277987.0301 2006.3 AK 80.502 297513692 214013672.6 92549687.72 74252523.9 43618489.16 2006.4 AK 78.434 298449932 140263641 33268706.27 7805907.93 3820785.891 2007.1 AK 74.705 298956884 41192941.23 11803986.9 1881990.345 13074.47213 2007.2 AK 71.031 300861972 173205553.4 106998053.8 53863922.93 34409808.12 2007.3 AK 68.579 295458933 243430116.8 154741496.8 110494098.3 102107472.6 2007.4 AK 68.676 301811434 228333011 95912466.17 40077570.16 10503478.19 2008.1 AK 69.735 303376375 60110242.16 49002553.39 26879647.28 13870448.42 2008.2 AK 70.88 306169147 212761334.3 189827463.7 100321266.7 89340615.31 2008.3 AK 73.876 312472989 282219706.2 90140727.84 84470014.42 12118019.14 2008.4 AK 65.746 309228467 71221772.43 35552956.58 1615240.699 1540654.772 2009.1 AK 66.979 302283840 3546586.074 1482201.638 77141.08363 37767.28412 2009.2 AK 64.623 305251718 102237199.1 11670093.31 2573736.501 76910.08603 2009.3 AK 64.297 305288501 168684331 75307740.56 23330370.56 10185335.02 2009.4 AK 64.134 310362395 10960638.41 6354020.504 4988031.121 1022311.39 2010.1 AK 65.557 310729720 75283931.7 14232591.32 603761.9986 458595.8909 2010.2 AK 67.308 302203244 245842194 20213075.25 2011033.251 1842088.461 2010.3 AK 68.003 293597978 265548731 247350828.4 81790130.71 77792179.44 2010.4 AK 67.965 309876800 134768482.2 130399102.4 23130018.37 9092129.95 2011.1 AK 68.425 311834720 228970745.4 28657170.89 2063723.896 723729.1886 2011.2 AK 69.114 322549763 182754925.2 18787053.16 8212588.195 7297365.704 2011.3 AK 69.679 311265933 138490270.8 28824422.04 19027939.82 14045498.18 2001.3 FL 211.846 6044748534 3794942826 308907032.4 161156066.4 85910406.62 2001.4 FL 216.035 6157867186 180872137.3 8501055.949 8444662.289 3362283.531 2002.1 FL 214.542 6535800159 266084977.2 125866769.4 26852130.39 10046855.27 2002.2 FL 205.848 6594695841 5111731899 1144298971 367414970.6 98406694.7 2002.3 FL 216.608 6609789164 792313601.8 56349243.64 9626126.859 6330248.679 2002.4 FL 238.966 6660098865 2879250693 1894401159 994534396.3 406785559.8 2003.1 FL 209.807 6579505480 2292698267 310878.6053 234210.5335 223143.0314 2003.2 FL 225.76 6488370106 2987843027 1432689798 389288623.8 157867891.6 2003.3 FL 232.81 6643635113 4955779298 1683574243 1079142925 634992957.8 2003.4 FL 229.516 6726122477 4118503725 2611648558 2334076650 1355777886 2004.1 FL 223.728 6404710173 2775921975 2151964563 1576109827 1257495386 2004.2 FL 223.241 6999945595 5113674117 115305871.7 13049996.77 10434708.11 2004.3 FL 239.827 7132440009 314725910.7 242836386.2 152329835.2 149593880.2 2004.4 FL 233.027 7117070795 4068165031 2173190585 1185663704 333601061.8 2005.1 FL 268.682 7163144969 4324912982 3630456980 1218167404 917982697.9 2005.2 FL 297.184 7312305604 1583888746 1181283505 513316051.2 203506921 2005.3 FL 312.756 8191171331 3898391680 2394032945 519297114.4 256518233.7 2005.4 FL 337.386 7709293855 6644353888 3789023495 1088839592 1061952347 2006.1 FL 364.004 7849382131 2252389210 2155109990 1887935281 1633810132 2006.2 FL 387.552 7540962249 3848860955 839628599.6 62101189.74 8550764.339 2006.3 FL 382.02 7642250857 1960225909 1528976296 881758550.6 309211054 2006.4 FL 378.616 7594688023 3446765139 1182761527 424048686.8 266214682 2007.1 FL 358.117 7827382009 1205592172 533172135.9 9931336.224 56653.63447 2007.2 FL 349.993 7890470437 6392808087 3018472746 891738571.4 857540290.2 2007.3 FL 293.666 7786826124 572304590.4 238522233.8 69466021.21 53782282.61 2007.4 FL 330.533 7865848171 6094864080 3566385484 2430595745 1046148889 2008.1 FL 317.949 7786559727 1718096974 1312180612 472729307.7 378764930.5 2008.2 FL 316.148 7731771411 2041214703 305451366.4 209147825.8 9815033.812 2008.3 FL 307.419 7852707618 4581329678 2782203470 2261101487 1962415782 2008.4 FL 294.745 7742857028 7237584031 7051193533 5523211995 4848846712 2009.1 FL 283.675 7586763428 6981242941 806701522.9 87415056.31 16016219.61 2009.2 FL 278.764 7465795888 5984337092 3564036947 2942446035 138363223.5 2009.3 FL 287.651 7523875420 3179166365 3177701644 2531312103 299919582.4 2009.4 FL 281.621 7383836440 6856047522 1908688907 796015662.2 526432817.5 2010.1 FL 279.402 7281567054 2220844512 572680485 396515219 59247027.27 2010.2 FL 289.421 6980977780 1191317239 310243477.7 170514992.7 78965909.95 2010.3 FL 291.431 6861537408 5214583301 262420030.5 106511969.8 103483752.8 2010.4 FL 298.42 7119724928 5324425299 2077545469 217125422.2 208216405.2 2011.1 FL 297.863 6970379102 167361811.1 144352786.5 103027429.5 75900092.92 2011.2 FL 305.086 7014111921 981899844.7 636781456.4 180641806.5 114698288.6 2011.3 FL 307.527 6885671456 5438069449 2701347354 2368227906 982687703.6 2001.3 GA 108.096 2843914417 2345148926 1196176911 243645435.9 157457052.3 2001.4 GA 111.125 2868374597 240435388.8 113088828.9 35736655.47 5271258.582 2002.1 GA 110.552 3031265955 928394997.8 569860371.7 40213838.01 24102608.08 2002.2 GA 121.194 3030671671 1731060317 237807701.9 100848127.2 55346040.99 2002.3 GA 100.143 2972130522 885777353 577636231.4 12507512.18 7882485.48 2002.4 GA 113.107 3013074198 2816886978 1525800267 589722806.6 83072347.27 2003.1 GA 117.254 3005102075 1346982036 837239852 681861790.3 363604325.1 2003.2 GA 118.47 2968854953 548661076.4 300680083.6 116676893.1 107111845.4 2003.3 GA 104.518 3007957561 1427325135 557077417.6 543209681.6 382649535.6 2003.4 GA 112.678 3032688793 1629173174 1577222726 883021640 193571443.1 2004.1 GA 113.159 2815553704 734823527.9 45190004.07 4053949.971 3279828.437 2004.2 GA 115.956 3089825692 2239145532 1614732998 1167335496 1122537576 2004.3 GA 114.359 3142833204 2807357848 2711995343 2068082362 1694813037 2004.4 GA 117.371 3119860073 746702375.9 579915843.5 561479690.8 36418287.95 2005.1 GA 128.742 3092860978 1582841395 835628900.9 529038705.7 376338451.7 2005.2 GA 128.753 3200081226 810534423.8 702150555.3 521293455.7 58004686.22 2005.3 GA 143.071 3329312056 1744906460 1257811653 1257592091 510178038.1 2005.4 GA 145.964 3370945069 2481049696 1720003195 509919622.3 292966081.6 2006.1 GA 150.275 3411059212 476591899.8 332703869.6 17961293.89 915153.1044 2006.2 GA 147.166 3313125567 1745873599 421472537.6 357732465.5 220529572.2 2006.3 GA 190.281 3339789375 2468902616 2270927807 2174814378 1133540788 2006.4 GA 144.496 3326456444 2930534161 2170367648 1975657412 425145491.8 2007.1 GA 144.542 3385415649 812093590 657089615.6 320929285.5 119363268.6 2007.2 GA 142.267 3463690029 995751949.6 711623713.6 282405327.3 64727159.59 2007.3 GA 131.045 3457987696 2104126833 443722640.4 346775577.4 231123808.6 2007.4 GA 132.224 3508901953 189730036.3 18570589.5 7295128.942 5902533.516 2008.1 GA 144.05 3455826717 1624301123 1527576825 394306382.6 141432525.2 2008.2 GA 137.305 3437074023 1936371068 912046982.4 403101359.8 152880286.7 2008.3 GA 140.523 3546491274 2864195730 569926261 235914211.8 205963917.4 2008.4 GA 146.788 3456713993 1597096588 952579203.8 437965533.2 163996800.8 2009.1 GA 132.23 3381000908 1648789514 216950013.1 142103044.1 45052282.85 2009.2 GA 132.844 3330564602 3295045221 1123544440 103056759.6 28406227.99 2009.3 GA 128.834 3281052091 2855728646 1894408217 431930274.4 188059642.2 2009.4 GA 127.391 3198123916 672986381.3 471483562.5 273980172.2 61602111.52 2010.1 GA 126.707 3129188392 2129748817 1438408356 512654065.6 35801508.73 2010.2 GA 132.934 3073262470 2222302179 1369323173 948078971.1 443111491.1 2010.3 GA 140.423 3014674504 1898700002 1201357400 1051115117 1021995880 2010.4 GA 133.571 3137446187 3028890125 1952630604 617939791.3 421995939.3 2011.1 GA 134.648 3066398279 1048481884 156070742.6 154342404.5 51772221.68 2011.2 GA 139.361 3108527031 1346617496 648556424.6 599081174.5 255240329.8 2011.3 GA 142.148 3066180423 934945266.1 671696185.3 15898505.67 2515769.376 ; run; /* list out all the Xvariables you wish to test as leading indicators */ data TESTVARS; length MYVARS $30.; input MYVARS $; cards; Varone Vartwo Varthree Varfour ; run; ods pdf file='c:\granger.pdf' notoc; %macro granger; OPTIONS NOTES SOURCE SOURCE2 MPRINT MLOGIC MERROR SYMBOLGEN ; data _null_; set testvars end=last; if last then call symput('nfiles',_n_); run; data TEST; set &mydata; run; proc sort ;by &mygeo &mydatevar ;run; data remove (keep=&mydatevar); set test; run; proc sort nodupkey ;by &mydatevar;run; data remove;set remove; if _n_>&p then delete;flag=1;run; %do k=1 %to &nfiles; data _null_; set testvars; if &k=_n_ then call symput('myvalue',trim(left(myvars))); run; data test; set test; %do i=1 %to &p; &yvar&i=lag&i(&yvar); &myvalue&i=lag&i(&myvalue); %end; run; %end; /* removing data due to lag generation */ proc sort data=test;by date;run; data test; merge test(in=a) remove; by &mydatevar; if flag=1 then delete; drop flag; if a; run; proc sort data=test;by &mygeo &mydatevar;run; %do k=1 %to &nfiles; data _null_; set testvars; if &k=_n_ then call symput('myvalue',trim(left(myvars))); run; proc autoreg data=test; model &yvar = &yvar.1-&yvar&p &myvalue.1-&myvalue&p; output out=out1 r=e1; run; proc autoreg data=out1; model &yvar = &yvar.1-&yvar&p; output out=out2 r=e0; run; data mydata; set out2; P=&p; T=_N_; SE0=E0*E0; SE1=E1*E1; Retain SSE0 SSE1; if _N_=1 then SSE0=0; if _N_=1 then SSE1=0; SSE0=SSE0+SE0; SSE1=SSE1+SE1; call symput ('SSE0',SSE0); call symput ('SSE1',SSE1); call symput ('T',T); run; data mydata2; set mydata; 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); Run; data GRANGERRESULTS(keep=P T SSE0 SSE1 TEST1 TEST2 P_VAL1 P_VAL2); set mydata2; label P='Max # Lags' T='Observations' SSE0='Sum of Squared Errors - Restricted' SSE1='Sum of Squared Errors - Unrestricted' TEST1='Granger F-Test Statistic' TEST2='Granger Asymptotic Equivalency Test Statistic' P_VAL1='Granger F-Test Probability' P_VAL2='Granger Asymptotic Equivalency Test Probability' ; if _n_^=&T then delete; run; proc transpose data=GRANGERRESULTS out=GRANGERRESULTS;run; data grangerresults; set grangerresults; rename COL1=%upcase(&myvalue); run; proc print; format &myvalue best12.; run; %if &k=1 %then %do; data Results_Summary; set grangerresults; run; %end; %if &k>1 %then %do; data Results_summary; merge Results_summary grangerresults; run; %end; %end; ods pdf close; %mend granger; %granger;