BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dsklein12
Calcite | Level 5

Using the below code I can calculate absolute error for each month. However, something isn't working when I try to minimize the sum of a created variable, "sae". Effectively, I'm trying to minimize "mae" (which is the average of the "sae" values, excluding the first observation) to find the optimal value for "a". 

 

 

 

proc optmodel;
set <str,num> MASTER_DATES;
set MASTER = setof {<i,t> in MASTER_DATES}i;
set NROW = setof {<i,t> in MASTER_DATES}t;

num performance {MASTER_DATES};
num month {MASTER_DATES};

read data example_data1 into MASTER_DATES=[master_nbr nrow] performance month;

num forecast{<i,t> in MASTER_DATES};
num sae{<i,t> in MASTER_DATES};

var a >=0 <=1;

for {<i,t> in MASTER_DATES} do;
if <i,t-1> in MASTER_DATES then
do;
forecast[i,t] = (performance[i,t-1]*a)+((1-a)*forecast[i,t-1]);
sae[i,t] = abs(performance[i,t] - forecast[i,t]);
end;
else
do;
forecast[i,t] = performance[i,t];
sae[i,t] = 0;
end;
end;

min mae = (sum {<i,t> in MASTER_DATES} sae[i,t]) / 10;

solve;
print a;

create data optmodel_outdata from [master_nbr nrow] performance month forecast sae;
quit;

 

 

Thanks,

Dan

1 ACCEPTED SOLUTION

Accepted Solutions
RobPratt
SAS Super FREQ

You have declared forecast and sae as numeric constants and explicitly set their values once, so the objective function is a constant that does not depend on the variable a.  Instead, you can use implicit variables to get the behavior you want:

impvar forecast{<i,t> in MASTER_DATES} = 
   if <i,t-1> in MASTER_DATES then performance[i,t-1]*a+(1-a)*forecast[i,t-1]
   else performance[i,t];

impvar sae{<i,t> in MASTER_DATES} = abs(performance[i,t] - forecast[i,t]);

View solution in original post

2 REPLIES 2
RobPratt
SAS Super FREQ

You have declared forecast and sae as numeric constants and explicitly set their values once, so the objective function is a constant that does not depend on the variable a.  Instead, you can use implicit variables to get the behavior you want:

impvar forecast{<i,t> in MASTER_DATES} = 
   if <i,t-1> in MASTER_DATES then performance[i,t-1]*a+(1-a)*forecast[i,t-1]
   else performance[i,t];

impvar sae{<i,t> in MASTER_DATES} = abs(performance[i,t] - forecast[i,t]);
dsklein12
Calcite | Level 5
Thank you Rob for the explanation. This works perfect!

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!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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