Hi All,
I have just started using the proc optmodel and I need help on calculation of max of a value for all ids in a data set with constraint values of a & b aggregated over all ids.
For example -
data new;
input id$ a b ;
datalines;
1 5 4
2 6 5
3 5 3
4 6 4
;
Now I want to solve for x that maximizes Z = (a + bx) for all ids in data i.e. for id 1, 2 and etc.
My Code was like below -
proc optmodel;
set <str> DATA;
var X{DATA} >= 0;
num a{DATA}; num b{DATA};
max Z = (a[i] + b[i]*X[i]);
con sum{i in DATA} (a[i]+b[i])=5;
read data new into DATA = [id] a b;
solve with LP /logfreq= 1 ;
print X;
quit;
But I am getting the below error -
ERROR 525-782: The symbol 'i' is unknown.
ERROR 621-782: Subscript 1 must be a string, found a number.
I would really appreciate some help on this.
Thanks
Here's one way:
proc optmodel;
set <str> DATA;
var X >= 0 <= 100;
num a{DATA};
num b{DATA};
str id;
max Z = a[id] + b[id]*X;
read data new into DATA = [id] a b;
num Xsol{DATA};
do id = DATA;
put id=;
solve;
Xsol[id] = X.sol;
end;
print a b Xsol;
quit;
Alternatively, you could replace the DO loop with a COFOR loop to solve these independent problems concurrently:
cofor {i in DATA} do;
id = i;
put id=;
solve;
Xsol[id] = X.sol;
end;
You are very close, but your constraint looks like not right.
data new;
input id$ a b ;
datalines;
1 5 4
2 6 5
3 5 3
4 6 4
;
proc optmodel;
set <str> DATA;
var X{DATA} >= 0;
num a{DATA};
num b{DATA};
max Z =sum{i in DATA} (a[i] + b[i]*X[i]);
read data new into DATA = [id] a b;
solve with LP ;
print X;
quit;
Do you want to solve one problem that involves all ids, or do you want to a separate problem for each id?
Also, without additional constraints, the problem is unbounded in the sense that you can make the objective value as large as you want just by making X[i] large.
Do you have any other constraints? Otherwise, the problem still has a trivial optimal solution: X[i] = 100 for each i.
Here's one way:
proc optmodel;
set <str> DATA;
var X >= 0 <= 100;
num a{DATA};
num b{DATA};
str id;
max Z = a[id] + b[id]*X;
read data new into DATA = [id] a b;
num Xsol{DATA};
do id = DATA;
put id=;
solve;
Xsol[id] = X.sol;
end;
print a b Xsol;
quit;
Alternatively, you could replace the DO loop with a COFOR loop to solve these independent problems concurrently:
cofor {i in DATA} do;
id = i;
put id=;
solve;
Xsol[id] = X.sol;
end;
Thank you so much Rob, this is working perfectly
Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.