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

I'm trying to optimize a football team in PROC OPTMODEL. I've successfully created the code which produces the report, but I want to save my decision variables outcome into a dataset. I read the following SAS documentation which suggests to use 'create data' statement, but I'm very new to PROC OPTMODEL and I'm not able to get the expected output. 

 

The code is as follows:

 

proc optmodel;

set <string> PLAYERS;
set <string> DEF = {"Alonso",	"Azpilicueta",	"Cahill",	"Christensen",	"David Luiz",	"Emerson",	"Moses",	"Rudiger",	"Zappacosta"};
set <string> FWD = {"Abraham","Giroud","Morata"};
set <string> GKP = {"Caballero","Courtois","Kepa"};
set <string> MID = {"Ampadu",	"Bakayoko",	"Barkley",	"Drinkwater",	"Fabregas",	"Hazard",	"Hudson-Odoi",	"Jorginho",	"Kante",	"Kovacic",	"Loftus-Cheek",	"Pedro",	"Willian"};
set <string> WeekMatch;

 
number NewPoints{PLAYERS};
number threshold{WeekMatch};

read data WORK.PLAYERS into PLAYERS = [Name] NewPoints;
read data WORK.THRESH into WeekMatch = [week] threshold;


var x{PLAYERS, WeekMatch} binary;
var win{WeekMatch} binary;


Maximize numberofwin = sum {i in WeekMatch} win[i];

/*constraint on maximum number of times a player can play*/
con ConsecutivePlay {i in PLAYERS}: sum {j in WeekMatch} x[i,j] <= 25; 

/*opponent threshold constraint*/
con WinTotal {j in WeekMatch} : sum {i in PLAYERS} x[i,j] * NewPoints[i] >= threshold[j] * win[j];

/*position constraints*/
con weekDEF {j in WeekMatch} : sum {i in DEF} x[i,j] = 4;

con weekFWD {j in WeekMatch} : sum {i in FWD} x[i,j] = 1;

con weekGKP {j in WeekMatch} : sum {i in GKP} x[i,j] = 1;

con weekMID {j in WeekMatch} : sum {i in MID} x[i,j] = 5;

solve with milp;

print numberofwin x win;
create data numberofwin from count=numberofwin;

quit;

The supporting data is attached.

1 ACCEPTED SOLUTION

Accepted Solutions
RobPratt
SAS Super FREQ

The following statement will create a data set with one observation for each assignment that appears in the solution:

 

create data solution from [i j]={i in PLAYERS, j in WeekMatch: x[i,j].sol > 0.5};

If you prefer the column names to be player and week, you can do this instead:

 

create data solution from [player=i week=j]={i in PLAYERS, j in WeekMatch: x[i,j].sol > 0.5};

If you prefer week to appear as the first column, you can do this:

create data solution from [week=j player=i]={j in WeekMatch, i in PLAYERS: x[i,j].sol > 0.5};

If you want a two-dimensional table of 0 and 1, you can do this:

create data solution from [player=i]=PLAYERS {j in WeekMatch} <col(j)=x[i,j].sol>;

View solution in original post

3 REPLIES 3
RobPratt
SAS Super FREQ

The following statement will create a data set with one observation for each assignment that appears in the solution:

 

create data solution from [i j]={i in PLAYERS, j in WeekMatch: x[i,j].sol > 0.5};

If you prefer the column names to be player and week, you can do this instead:

 

create data solution from [player=i week=j]={i in PLAYERS, j in WeekMatch: x[i,j].sol > 0.5};

If you prefer week to appear as the first column, you can do this:

create data solution from [week=j player=i]={j in WeekMatch, i in PLAYERS: x[i,j].sol > 0.5};

If you want a two-dimensional table of 0 and 1, you can do this:

create data solution from [player=i]=PLAYERS {j in WeekMatch} <col(j)=x[i,j].sol>;
lawilap
Fluorite | Level 6

@RobPratt Thank you so much, this works very well. I was particularly interested in the last solution you mentioned.

But, this only give me the dataset for x, can you please suggest how to store 'win'.

Thank you again.

RobPratt
SAS Super FREQ
create data windata from [week] win;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 3 replies
  • 1548 views
  • 2 likes
  • 2 in conversation