Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 08-08-2019 05:22 AM
(1109 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

7 REPLIES 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

I will apply other constraint this is just a test constraint. For now I can go with z < 100 and that will suffice.

Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you so much Rob, this is working perfectly

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 16. Read more here about **why** you should contribute and **what is in it** for you!

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.