BookmarkSubscribeRSS Feed
Kevin_L_
Calcite | Level 5

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;

2 REPLIES 2
ballardw
Super User

@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.

Kevin_L_
Calcite | Level 5

Thanks for the response.

 

The second set of SAS code I provided does do what I want. Basically, for each date I want to calculate the same thing - only once. However, because the lag is used in the calculation for subsequent dates, I have to update the lags (the lags are used in the forecast).

 

The first set of data is the output for a PROC REG. Then I use the output model to create the forecast, I want to create a simple DO statement does  what the second set of code produces (it works as intended, I just want to simplify the code).

 

There is a DO statement in the first (if date>='31dec2018'd then DO), it seems to do the same thing as the code you provided, It seems to loop through many times. Not just executes once for each date and updates the lags and replace the missing data with the calculated values.

 

I attached the ao.forecast file, that may help make sense of what I want to do.

 

 

Thanks.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 949 views
  • 0 likes
  • 2 in conversation