@Kevin_L_ wrote:
I am trying to write a do loop to basically do the following (pasted below). Basically what this is doing is making a forecast using prior known information. Since it uses it's own forecast in the forecast, I have to update the lags through the iterations of the loop.
The do loop I wrote seems to just replace the dates (I am relatively new to coding these). Any help is much appreciated.
DO loop that does not work:
data ao.forecast;
set ao.forecast;
if date>='31dec2018'd then do;
yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
end;
run;
Longer version without the DO loop that works:
data ao.forecast;
set ao.forecast;
if date='31dec2018'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='31mar2019'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='30jun2019'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='30sep2019'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='31dec2019'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='31mar2020'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='30jun2020'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='30sep2020'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='31dec2020'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='31mar2021'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='30jun2021'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='30sep2021'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
if date='31dec2021'd then yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
run;
You do not show any DO Loop in your code.
Your second data step doesn't have any OUTPUT statement so you only get the result of the last value. I suspect you wanted something like this:
data ao.forecastnew;
set ao.forecast;
/* keep adding your specific dates as shown*/
do date ='31dec2018'd,'31mar2019'd,'30jun2019'd,'30sep2019'd ;
yhat=mu+beta1*yhat_l1+beta2*yhat_l4;
yhat_l4=lag4(yhat);
yhat_l1=lag1(yhat);
output;
end;
run;
However since none of your calculations actually depend on the date I am not at all clear on what you are actually attempting.
Also LAG may not return the value you expect as the nature of the queue that each LAG or DIF function call maintains often means that values may not be quite as expected. I think that you will end up having to provide some example data and a clearer description of what you are actually trying to accomplish to get a solution.
WARNING: You may have already destroyed your input data set by using
data ao.forecast;
set ao.forecast;
while not syntactically incorrect if you make any logic error you have completely rewritten the source file and the values may no longer be what you expect. Note that this is a very difficult thing to debug after you have done it. Better, especially when learning new code features is to always direct the data to a new data set. If you have already caused a problem you will have to go back to wherever you built the first ao.forecast data set.