Hi All,
I am pretty new to using SAS OR module, I have a very simple problem which can easily be solved using excel solver. However I want to do this exercise using Proc OPTMODEL provided in SAS OR. In my case, I have three decision variables whose values needs to be changed by maximizing the objective function based on some constraints. I have listed down constraints, objective function & decision variables below:-
Constraint 1: All the three decision variables(Var1,Var2 & Var3) have to be binary in nature.
Constraint 2: SUM of all the three decision variables (Total column) should be equal to 1 for each row. (e.g any one of the decision variable can take value as 1 rest decision vars have to be 0)
Maximize Objective function: SUMPRODUCT(Total, Price)/SUM(Total) <= 100
I have pasted below the sample table.
S.No. | Var1 | Var2 | Var3 | Total | Price |
1 | 0 | 1 | 0 | 1 | 30 |
2 | 1 | 0 | 0 | 1 | 77 |
3 | 1 | 0 | 0 | 1 | 99 |
4 | 0 | 0 | 1 | 1 | 26 |
5 | 0 | 1 | 0 | 1 | 49 |
Can anybody help me with this?
Thanks!
Kishore
Yes, that is clearer. You do have 15 binary variables. I think the following does what you want, although I have changed 100 to 10 because otherwise the first constraint would be redundant and the optimal solution would be trivial.
data indata;
input S Var4 Var5 Var6 Price;
datalines;
1 0.2000 0.2500 0.9900 30
2 0.1000 0.2000 0.9000 77
3 0.1500 0.2500 0.9500 99
4 0.2500 0.3500 0.9500 26
5 0.3500 0.4500 0.9900 49
;
proc optmodel;
set SSET;
set JSET = 1..3;
num a {SSET, JSET};
num price {SSET};
read data indata into SSET=[s] {j in JSET} <a[s,j]=col('Var'||(j+3))> price;
print a price;
var X {SSET, JSET} binary;
impvar NumPeople {s in SSET} = sum {j in JSET} j*X[s,j];
max Objective = (sum {s in SSET, j in JSET} price[s]*a[s,j]*X[s,j]) / (sum {s in SSET} price[s]);
con TotalPeople:
sum {s in SSET} NumPeople[s] <= 10;
con OnceChoice {s in SSET}:
sum {j in JSET} X[s,j] = 1;
solve;
print X NumPeople;
quit;
SAS Output
I think something is missing in your description. How do the decision variables relate to the objective function? What is the <= 100?
What do the rows in the table represent? Do you maybe have 15 decision variables and not just 3?
What is the optimal solution returned by Excel? And what is the corresponding objective value?
Hi Rob,
Thanks for your quick reply, Indeed I forgot to mention lot of things. Apologies for creating confusion. Here is the updated table mentioned below:-
1 | 2 | 3 | ||||||||
S.No. | Var1 | Var2 | Var3 | Var4 | Var5 | Var6 | SUM_P | No_of_People | Total | Price |
1 | 0 | 1 | 0 | 0.2000 | 0.2500 | 0.9900 | 0.2500 | 2 | 1 | 30 |
2 | 1 | 0 | 0 | 0.1000 | 0.2000 | 0.9000 | 0.1000 | 1 | 1 | 77 |
3 | 1 | 0 | 0 | 0.1500 | 0.2500 | 0.9500 | 0.1500 | 1 | 1 | 99 |
4 | 0 | 0 | 1 | 0.2500 | 0.3500 | 0.9500 | 0.9500 | 3 | 1 | 26 |
5 | 0 | 1 | 0 | 0.3500 | 0.4500 | 0.9900 | 0.4500 | 2 | 1 | 49 |
So column "No_of_people" is the sumproduct of var1,var2 & var3 with values 1,2 & 3. SUM_P is also the sumproduct between va1 , var2, var3 & var4,var5,var6. Now I mentioned about <=100, that is one of my constraint for column "No_of_people". So sum(No_of_people) <=100.
I have mentioned corrected objective function below:-
Maximize Objective function: SUMPRODUCT(SUM_P, Price)/SUM(Price)
Constraint 1:- sum(No_of_people) <=100
Constraint 2: SUM of all the three decision variables (Total column) should be equal to 1 for each row. (e.g any one of the decision variable can take value as 1 rest decision vars have to be 0)
Constraint 3: Var1,Var2 & Var3 can take only binary values.
Var4,Var5 & Var6 are the constant values.
I hope this time it is clear that objective functions depends on SUM_P which is the sumproduct between var1,var2,var3 & var4,var5,var6.
Thanks!
Kishore
Yes, that is clearer. You do have 15 binary variables. I think the following does what you want, although I have changed 100 to 10 because otherwise the first constraint would be redundant and the optimal solution would be trivial.
data indata;
input S Var4 Var5 Var6 Price;
datalines;
1 0.2000 0.2500 0.9900 30
2 0.1000 0.2000 0.9000 77
3 0.1500 0.2500 0.9500 99
4 0.2500 0.3500 0.9500 26
5 0.3500 0.4500 0.9900 49
;
proc optmodel;
set SSET;
set JSET = 1..3;
num a {SSET, JSET};
num price {SSET};
read data indata into SSET=[s] {j in JSET} <a[s,j]=col('Var'||(j+3))> price;
print a price;
var X {SSET, JSET} binary;
impvar NumPeople {s in SSET} = sum {j in JSET} j*X[s,j];
max Objective = (sum {s in SSET, j in JSET} price[s]*a[s,j]*X[s,j]) / (sum {s in SSET} price[s]);
con TotalPeople:
sum {s in SSET} NumPeople[s] <= 10;
con OnceChoice {s in SSET}:
sum {j in JSET} X[s,j] = 1;
solve;
print X NumPeople;
quit;
SAS Output
Hi Rob,
Just a follow up question, If I have to create an output datsest for variable X then How do I use "Create data" statement to do the same? I was able to create dataset for variable "NumPeople", however wasn't able to do it for variable "X".
I used this syntax " Create dataset output_model from [s] [NumPeople]";
Thanks!
Kishore
That statement is not correct and will generate errors. The correct syntax is:
Create data output_model from [s] NumPeople;
To create a data set for X, you can mimic the READ DATA statement from earlier, as follows:
create data output_x from [s]=SSET {j in JSET} <col('Var'||(j+3))=X[s,j]>;
You can find lots of variations of both READ DATA and CREATE DATA in this book of examples.
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.