- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Dear friends,
I wrote a code that calculates eight lags and eight leads for each macro variable in my list and then uses OLS and WLS to calculate R-squared. But my code is not well written since for each lag and lead I had to copy-paste the same procedure and then manually modify it for the corresponding lag or lead. Is there any way I can create a loop for lags and leads, say, for i = 1:8, create lags, or for j =1:8, create leads and then run the following steps blah-blah-blah.
Also, once I created lags and leads for each macro variable, I have a column lag_1 or lead_1 and row that shows the name of the macrov variable. But if later on I need to use, say BBB_lag_1 in my OLS regression, I don't know how to identify that specific cell. Does anyone know what the best way to assign names for each variable in column lag_1 that corresponds to the name of the appropriate row (say, BBB) and then attach the name of the appropriate column (say, lag_1). I am sorry for my English. It is probably confusing.
The code is attached.
libname UB 'blah-blah-blah';
%let macro = ub.macro_lgd_f1f8_LZ_YT;
%let list =
rGDP_grw
rGDP_grw_yoy
nGDP_grw
nGDP_grw_yoy
rDPI_grw
rDPI_grw_yoy
nDPI_grw
nDPI_grw_yoy
ata ub.laglead;
set ¯o;
array var_list(*) &list;
do i = 1 to dim(var_list);
variable = vname(var_list(i));
value = var_list(i);
output;
end;
keep Date AVG_LGD variable value ;
run;
proc sort data=ub.laglead;
by variable date;
run;
* Create 8 lags and 8 leads of each macro variable in the list;
data ub.laglead;
set ub.laglead;
by variable;
lag_1=ifn(first.variable=0,lag(value),.);
lag_2=ifn(lag2(variable)=variable, lag2(value),.);
lag_3=ifn(lag3(variable)=variable, lag3(value),.);
lag_4=ifn(lag4(variable)=variable, lag4(value),.);
lag_5=ifn(lag5(variable)=variable, lag5(value),.);
lag_6=ifn(lag6(variable)=variable, lag6(value),.);
lag_7=ifn(lag7(variable)=variable, lag7(value),.);
lag_8=ifn(lag8(variable)=variable, lag8(value),.);
if eof1=0 then set ub.laglead (firstobs=2 keep=value rename=(value=lead_1)) end=eof1;
if last.variable then lead_1 =.;
if eof2=0 then set ub.laglead (firstobs=3 keep=variable value rename=(variable=variable3 value=lead_2)) end=eof2;
if variable3^= variable then lead_2 =.;
drop variable3;
if eof3=0 then set ub.laglead (firstobs=4 keep=variable value rename=(variable=variable4 value=lead_3)) end=eof3;
if variable4^= variable then lead_3 =.;
drop variable4;
if eof4=0 then set ub.laglead (firstobs=5 keep=variable value rename=(variable=variable5 value=lead_4)) end=eof4;
if variable5^= variable then lead_4 =.;
drop variable5;
if eof5=0 then set ub.laglead (firstobs=6 keep=variable value rename=(variable=variable6 value=lead_5)) end=eof5;
if variable6^= variable then lead_5 =.;
drop variable6;
if eof6=0 then set ub.laglead (firstobs=7 keep=variable value rename=(variable=variable7 value=lead_6)) end=eof6;
if variable7^= variable then lead_6 =.;
drop variable7;
if eof7=0 then set ub.laglead (firstobs=8 keep=variable value rename=(variable=variable8 value=lead_7)) end=eof7;
if variable8^= variable then lead_7 =.;
drop variable8;
if eof8=0 then set ub.laglead (firstobs=9 keep=variable value rename=(variable=variable9 value=lead_8)) end=eof8;
if variable9^= variable then lead_8 =.;
drop variable9;
run;
*Calculate Rsquared using ordinary OLS;
** Run an OLS regression where the dependent variable is LGD and independent varialbe (one at a time) macroeconomic variable;
proc reg data=ub.laglead outest=ub.OLS_est_value tableout;
by variable;
model AVG_LGD=value/selection=rsquare b;
output out=ub.OLS_pred_value residual=OLS_residual_value predicted=OLS_predicted_value;
run;
*ods graphics off;
proc print data=ub.OLS_est_value;
run;
****************************EIGHT LAGS *****************************;
* Lag 1;
*ods graphics on;
proc reg data=ub.laglead outest=ub.OLS_est_lag_1 tableout;
by variable;
model AVG_LGD=lag_1/selection=rsquare b;
output out=ub.OLS_pred_lag1 residual=OLS_residual_lag1 predicted=OLS_predicted_lag1;
run;
*ods graphics off;
proc print data=ub.OLS_est_lag_1;
run;
* Lag 2;
proc reg data=ub.laglead outest=ub.OLS_est_lag_2 tableout;
by variable;
model AVG_LGD=lag_2/selection=rsquare b;
output out=ub.OLS_pred_lag2 residual=OLS_residual_lag2 predicted=OLS_predicted_lag2;
run;
*ods graphics off;
proc print data=ub.OLS_est_lag_2;
run;
* Lag 3;
proc reg data=ub.laglead outest=ub.OLS_est_lag_3 tableout;
by variable;
model AVG_LGD=lag_3/selection=rsquare b;
output out=ub.OLS_pred_lag3 residual=OLS_residual_lag3 predicted=OLS_predicted_lag3;
run;
*ods graphics off;
proc print data=ub.OLS_est_lag_3;
run;
* Lag 4;
proc reg data=ub.laglead outest=ub.OLS_est_lag_4 tableout;
by variable;
model AVG_LGD=lag_4/selection=rsquare b;
output out=ub.OLS_pred_lag4 residual=OLS_residual_lag4 predicted=OLS_predicted_lag4;
run;
*ods graphics off;
proc print data=ub.OLS_est_lag_4;
run;
* Lag 5;
proc reg data=ub.laglead outest=ub.OLS_est_lag_5 tableout;
by variable;
model AVG_LGD=lag_5/selection=rsquare b;
output out=ub.OLS_pred_lag5 residual=OLS_residual_lag5 predicted=OLS_predicted_lag5;
run;
*ods graphics off;
proc print data=ub.OLS_est_lag_5;
run;
* Lag 6;
proc reg data=ub.laglead outest=ub.OLS_est_lag_6 tableout;
by variable;
model AVG_LGD=lag_6/selection=rsquare b;
output out=ub.OLS_pred_lag6 residual=OLS_residual_lag6 predicted=OLS_predicted_lag6;
run;
*ods graphics off;
proc print data=ub.OLS_est_lag_6;
run;
* Lag 7;
proc reg data=ub.laglead outest=ub.OLS_est_lag_7 tableout;
by variable;
model AVG_LGD=lag_7/selection=rsquare b;
output out=ub.OLS_pred_lag7 residual=OLS_residual_lag7 predicted=OLS_predicted_lag7;
run;
*ods graphics off;
proc print data=ub.OLS_est_lag_7;
run;
* Lag 8;
proc reg data=ub.laglead outest=ub.OLS_est_lag_8 tableout;
by variable;
model AVG_LGD=lag_8/selection=rsquare b;
output out=ub.OLS_pred_lag8 residual=OLS_residual_lag8 predicted=OLS_predicted_lag8;
run;
*ods graphics off;
proc print data=ub.OLS_est_lag_8;
run;
****************************EIGHT LEADS *****************************;
* Lead 1;
*ods graphics on;
proc reg data=ub.laglead outest=ub.OLS_est_lead_1 tableout;
by variable;
model AVG_LGD=lead_1/selection=rsquare b;
output out=ub.OLS_pred_lead1 residual=OLS_residual_lead1 predicted=OLS_predicted_lead1;
run;
*ods graphics off;
proc print data=ub.OLS_est_lead_1;
run;
* Lead 2;
proc reg data=ub.laglead outest=ub.OLS_est_lead_2 tableout;
by variable;
model AVG_LGD=lead_2/selection=rsquare b;
output out=ub.OLS_pred_lead2 residual=OLS_residual_lead2 predicted=OLS_predicted_lead2;
run;
*ods graphics off;
proc print data=ub.OLS_est_lead_2;
run;
* Lead 3;
proc reg data=ub.laglead outest=ub.OLS_est_lead_3 tableout;
by variable;
model AVG_LGD=lead_3/selection=rsquare b;
output out=ub.OLS_pred_lead3 residual=OLS_residual_lead3 predicted=OLS_predicted_lead3;
run;
*ods graphics off;
proc print data=ub.OLS_est_lead_3;
run;
* Lead 4;
proc reg data=ub.laglead outest=ub.OLS_est_lead_4 tableout;
by variable;
model AVG_LGD=lead_4/selection=rsquare b;
output out=ub.OLS_pred_lead4 residual=OLS_residual_lead4 predicted=OLS_predicted_lead4;
run;
*ods graphics off;
proc print data=ub.OLS_est_lead_4;
run;
* Lead 5;
proc reg data=ub.laglead outest=ub.OLS_est_lead_5 tableout;
by variable;
model AVG_LGD=lead_5/selection=rsquare b;
output out=ub.OLS_pred_lead5 residual=OLS_residual_lead5 predicted=OLS_predicted_lead5;
run;
*ods graphics off;
proc print data=ub.OLS_est_lead_5;
run;
* Lead 6;
proc reg data=ub.laglead outest=ub.OLS_est_lead_6 tableout;
by variable;
model AVG_LGD=lead_6/selection=rsquare b;
output out=ub.OLS_pred_lead6 residual=OLS_residual_lead6 predicted=OLS_predicted_lead6;
run;
*ods graphics off;
proc print data=ub.OLS_est_lead_6;
run;
* Lead 7;
proc reg data=ub.laglead outest=ub.OLS_est_lead_7 tableout;
by variable;
model AVG_LGD=lead_7/selection=rsquare b;
output out=ub.OLS_pred_lead7 residual=OLS_residual_lead7 predicted=OLS_predicted_lead7;
run;
*ods graphics off;
proc print data=ub.OLS_est_lead_7;
run;
* Lead 8;
proc reg data=ub.laglead outest=ub.OLS_est_lead_8 tableout;
by variable;
model AVG_LGD=lead_8/selection=rsquare b;
output out=ub.OLS_pred_lead8 residual=OLS_residual_lead8 predicted=OLS_predicted_lead8;
run;
*ods graphics off;
proc print data=ub.OLS_est_lead_8;
run;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You are checking Autocorrelation . Check proc autoreg :
proc autoreg data=have;
model AVG_LGD= /dw=5 dwprob; /* Lag 1 2 3 4 5*/
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Do you have a license for SAS/ETS?
Proc setinit;run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Dear Reeza,
Thank you for your response. Yes, I do.
Yelena
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
In addition, to @Ksharp correct solution, which requires SAS/ETS, you can use proc expand to calculate lead/lag variables. See the documentation for examples.
Just as an FYI.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You are checking Autocorrelation . Check proc autoreg :
proc autoreg data=have;
model AVG_LGD= /dw=5 dwprob; /* Lag 1 2 3 4 5*/
run;