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
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]);
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]);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.