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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RobPratt
SAS Super FREQ

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;

View solution in original post

7 REPLIES 7
Ksharp
Super User

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;
RobPratt
SAS Super FREQ

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.

MS15
Calcite | Level 5
Hi Rob,
I am looking for separate optimisation for each ids. For example -
For id 1, max of z = 5 + 4X
and for id 2, max of z = 6 + 5X and so on.
And the contraints would be X < 100.
Appreciate your help
Thanks
RobPratt
SAS Super FREQ

Do you have any other constraints?  Otherwise, the problem still has a trivial optimal solution: X[i] = 100 for each i.

MS15
Calcite | Level 5
Hi Rob,
I will apply other constraint this is just a test constraint. For now I can go with z < 100 and that will suffice.

Thanks
RobPratt
SAS Super FREQ

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;
MS15
Calcite | Level 5

Thank you so much Rob, this is working perfectly 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7 replies
  • 1188 views
  • 0 likes
  • 3 in conversation