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.
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>;
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>;
@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.
create data windata from [week] win;
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.