## SAS Macro Performing Breusch–Godfrey Test for Serial Correlation

%macro bgtest(data = , r = , x = , order = 1); ********************************************************************; * SAS MACRO PERFORMING BREUSCH-GODFREY TEST FOR SERIAL CORRELATION *; * BY FOLLOWING THE LOGIC OF BGTEST() IN R LMTEST PACKAGE *; * ================================================================ *; * INPUT PAREMETERS: *; * DATA : INPUT SAS DATA TABLE *; * R : RESIDUALS TO TEST SERIAL CORRELATION *; * X : INDEPENDENT VARIABLES IN THE ORIGINAL REGRESSION MODEL *; * ORDER : THE ORDER OF SERIAL CORRELATION *; * ================================================================ *; * AUTHOR: WENSUI.LIU@53.COM *; ********************************************************************; data _1 (drop = _i); set &data (keep = &r &x); %do i = 1 %to ℴ _lag&i._&r = lag&i.(&r); %end; _i + 1; _index = _i - ℴ if _index > 0 then output; run; ods listing close; proc reg data = _last_; model &r = &x _lag:; output out = _2 p = yhat; run; ods listing; proc sql noprint; create table _result as select (select count(*) from _2) * sum(yhat ** 2) / sum(&r ** 2) as _chisq, 1 - probchi(calculated _chisq, &order.) as _p_chisq, &order as _df from _2; quit; title; proc report data = _last_ spacing = 1 headline nowindows split = "*"; column(" * BREUSCH-GODFREY TEST FOR SERIAL CORRELATION * H0: THERE IS NO SERIAL CORRELATION OF ANY ORDER UP TO &order * " _chisq _df _p_chisq); define _chisq / "CHI-SQUARE" width = 20 format = 15.10; define _df / "DF" width = 10; define _p_chisq / "P-VALUE" width = 20 format = 15.10; run; %mend bgtest;

Advertisements