Category: Program

Program 1.1


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/

* Program 1.1: An example of PROC OPTMODEL, populating data (preparing SAS datasets prior to call OPTMODEL);
* Creating dataset of cost of shipments from each warehouse to publisher;

data d_trans;
input warehouse pub1-pub8 supply;
datalines;
1 10 15 12 13 15 10 20 15 1000
2 8 12 15 10 12 16 12 17 1500
3 12 12 16 14 12 15 10 12 2000
4 20 10 20 12 15 14 17 12 2000
;

* Creating dataset of demands by each publisher;
data d_demand;
input pub demand;
datalines;
1 200
2 500
3 700
4 800
5 900
6 900
7 1000
8 1500
;

/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-1-1

Program 1.2


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/

* Program 1.2: An example of PROC OPTMODEL, populating data,
* constructing linear programming and solving the model;

data d_trans;
input warehouse pub1-pub8 supply;
datalines;
1 10 15 12 13 15 10 20 15 1000
2 8 12 15 10 12 16 12 17 1500
3 12 12 16 14 12 15 10 12 2000
4 20 10 20 12 15 14 17 12 2000
;

data d_demand;
input pub demand;
datalines;
1 200
2 500
3 700
4 800
5 900
6 900
7 1000
8 1500
;

proc optmodel;

set WAREHOUSES;
set PUBS;

number demand{PUBS};
number cost{WAREHOUSES, PUBS};
number supply{WAREHOUSES};

var X{WAREHOUSES, PUBS} >= 0;

read data d_demand
into PUBS=[pub] demand[pub]=col(“demand”);

read data d_trans
into WAREHOUSES=[warehouse] supply[warehouse]=col(“supply”);

read data d_trans
into WAREHOUSES=[warehouse]
{p in PUBS} ;

min obj = sum{W in WAREHOUSES, P in PUBS} cost[w,p]*x[w,p];
con req_supply{w in WAREHOUSES}:

sum{p in PUBS} x[w,p] <= supply[w]; con req_demand{p in PUBS}: sum{w in WAREHOUSES} x[w,p] >= demand[p];

solve with lp/solver=primal;

create data optimout
from [warehouse pub]
={w in WAREHOUSES, p in PUBS: x[w,p]^=0}
amount=x;
quit;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-1-2

Program 1.3


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/

*Program 1.3: An example of PROC OPTMODEL;
proc optmodel;

* Declare parametrs a, b and c;
number c{1..4}=[4, 2, 1, 3] ;
number b{1..3}=[4, 3, 8];
number a{1..3, 1..4}= [2, 1, 0, 4,
4, -1, 4, 0,
3, 2, 1, 2];


* Declare variable x;
var x{1..4} >=0;

* Define objective function;
max z = sum{i in 1..4}(c[i]*x[i]);

* Define constraints;
con constraint{i in 1..3}:


sum{j in 1..4} a[i,j]*x[j] <= b[i];

* Print the linear programming;
expand;

* Solve the model;
solve;


* Print optimum solution for x;
print x.sol;
%put &_OROPTMODEL_;
quit;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-1-3

Program 1.4


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/

*Program 1.4: Set and indexing in PROC OPTMODEL;
proc optmodel;

set row ;
set col ;
row={“Bank1”, “Bank2”, “Bank3”, “Bank4”};
col={“Labour”, “Capital”, “Profit”};

number bank{r in row, c in col}=
[10, 2000, 30,
50, 40000, 68,
8, 25000, 45,
18, 70000, 50;

print bank;
quit;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/>

Permanent link to this article: http://sas-or.com/book/program-1-4

Program 1.5


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/

*Program 1.5: An example of PROC OPTMODEL using read statement;
data bankdata;

input Bank $ Labour Capital Profit;
datalines;
Bank1 10 2000 30
Bank2 50 40000 68
Bank3 8 25000 45
Bank4 18 70000 50
;

proc optmodel;


* Define parameters;

set <string> row ;
set <string> col ;
col={“Labour”, “Capital”, “Profit”};

number bankmatrix{r in row, c in col};
* Populating name of banks from the first column of the dataset to ‘row’;

read data bankdata
into row=[Bank] ;
* Populating value of Capital, Labour and Profit to each bank from the dataset;

read data bankdata
into
{r in row} <bankmatrix[r, “Labour”]=col(“Labour”)
bankmatrix[r, “Capital”]=col(“Capital”)
bankmatrix[r, “Profit”]=col(“Profit”)>;

* Printing bankmatrix;
print bankmatrix;

quit;

/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-1-5

Program 2.1


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/


*Program 2.1: Using PROC OPTMODEL for Transportation problem;
option nodate ;
%let _title=’Example 2.1: Transportation problem for four suppliers and five customers.’;
%let _data=’c:\sasor\data2_1.txt’;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;

proc import
datafile=&_data
out=dcost
dbms=tab
replace;
getnames=yes;
run;
%mend data;

%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;

* Define sets;
set SUPPLIERS;
set CUSTOMERS={1..5};

* Define parameters;
number demand{CUSTOMERS};
number supply{SUPPLIERS};
number cost{SUPPLIERS, CUSTOMERS};

* Define variables;
var X{SUPPLIERS, CUSTOMERS} >= 0;

* Load the supplier set and their amount of supply;
read data dcost (where= (supply ne .))
into SUPPLIERS=[Supplier] supply[Supplier]=col(“supply”);

* Load the customer set and their demand;
read data dcost (where= (supply eq .)) into
{c in CUSTOMERS} ;

* Load the cost of shipment from each supplier to each customer;
read data dcost (where= (supply ne .))
into SUPPLIERS=[Supplier]
{c in CUSTOMERS} ;

* Define objective function;
min obj = sum{s in SUPPLIERS, c in CUSTOMERS} cost[s,c]*x[s,c];

* Define constraints;
con req_supply{s in SUPPLIERS}:
sum{c in CUSTOMERS} x[s,c] <= supply[s];

con req_demand{c in CUSTOMERS}: sum{s in SUPPLIERS} x[s,c] >= demand[c];

con zero{c in CUSTOMERS, s in SUPPLIERS : cost[s,c]=1E10}: x[s,c]=0;

* Solve the model;
solve with lp/solver=primal;

* Create optimum values in a SAS dataset ‘optimout’;
create data optimout
from [SUPPLIERS CUSTOMERS]
={s in SUPPLIERS, c in CUSTOMERS: x[s,c]^=0}
amount=x;

* End of OPTMODEL Procedure;
quit;
%mend model;

* The report writing macro;
%macro report;

* report the results in a tabulated form;
proc tabulate data=optimout;
title &_title;
class SUPPLIERS CUSTOMERS ;
var amount;
table SUPPLIERS =” Suppliers”,
CUSTOMERS*amount*sum
/ BOX=’Amount of suppliers to customers’ ;
run;
%mend report;

* The ortrans macro for transportation problem;
%macro ortrans;
%data;
%model;
%report;
%mend ortrans;

%ortrans;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-2-1

Program 2.2


/****************************************************************************
* This program is taken from the following book:                            *
* Emrouznejad, A. and Ho, W. (2010) Applied Operational Research with SAS,  *
* Chapman and Hall/CRC – 284 Pages.                                         *
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/


* Using PROC OPTMODEL for Assignment problem;
option nodate ;
%let _title=’Assignment network for five workers and five tasks.’;
%let _data=’c:\sasor\data2_2.txt’;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;

proc import
datafile=&_data
out= dataassign
dbms=tab
replace;
getnames=yes;
run;
%mend data;

%macro model;

* Starting OPTMODEL Procedure;
proc optmodel;

* Define sets;
set WORKERS;
set TASKS={1..5};

* Define parameters;
number costprofit{WORKERS, TASKS};

* Define variables;
var X{WORKERS, TASKS} integer >= 0;

* Load the cost/profit matrix ;
read data dataassign
into WORKERS=[worker]
{t in TASKS} ;

* Define objective function;
min obj = sum{w in WORKERS, t in TASKS} costprofit[w,t]*x[w,t];

* Define constraints;
con req_supply{w in WORKERS}:
sum{t in TASKS} x[w,t] = 1;

con req_demand{t in TASKS}:
sum{w in WORKERS} x[w,t] = 1;

* Solve the model;
solve with milp;

* Create optimum values in a SAS dataset ‘optimout’;
create data optimout
from [WORKERS TASKS]
={w in WORKERS, t in TASKS: x[w,t]^=0}
amount=x cp= costprofit[w,t];

* End of OPTMODEL Procedure;
quit;

%mend model;

%macro report;
* Report the results in a tabulated form;
proc tabulate data=optimout;
title &_title;
class WORKERS TASKS ;
var amount;
table WORKERS =” Workers”,
TASKS*amount*sum
/ BOX=’Assigning workers to each tasks’ ;
run;

* Report the results in a tabulated form;
proc tabulate data=optimout;
title &_title;
class WORKERS TASKS ;
var cp;
table WORKERS =” Workers”,
TASKS*cp*sum
/ BOX=’Cost-profit of workers to each tasks’ ;
run;
%mend report;

* The orassign macro for assignment problem;

%macro orassign;
%data;
%model;
%report;
%mend orassign;

%orassign;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-2-2

Program 2.3


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/


*Program 2.3: Using PROC OPTMODEL for Transportation problem;
option nodate ;
%let _title=’Example 2.3: Transshipment problem for two suppliers, two warehouses and two customers.’;
%let _data=’c:\sasor\data2_3.txt’;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dcost
dbms=tab
replace;
getnames=yes;
run;
%mend data;

%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;

* Define sets;
set SUPPLIERS;
set CUSTOMERS={1..4};

* Define parameters;
number demand{CUSTOMERS};
number supply{SUPPLIERS};
number cost{SUPPLIERS, CUSTOMERS};

* Define variables;
var X{SUPPLIERS, CUSTOMERS} >= 0;

* Load the supplier set and their amount of supply;
read data dcost (where= (supply ne .))
into SUPPLIERS=[Supplier] supply[Supplier]=col(“supply”);

* Load the customer set and their demand;
read data dcost (where= (supply eq .)) into
{c in CUSTOMERS} <demand[c]=col(“Cstmer”||c)>;

* Load the cost of shipment from each supplier to each customer;
read data dcost (where= (supply ne .))
into SUPPLIERS=[Supplier]
{c in CUSTOMERS} <cost[Supplier,c]=col(“Cstmer”||c)>;

* Define objective function;
min obj = sum{s in SUPPLIERS, c in CUSTOMERS} cost[s,c]*x[s,c];

* Define constraints;
con req_supply{s in SUPPLIERS}:
sum{c in CUSTOMERS} x[s,c] <= supply[s];

con req_demand{c in CUSTOMERS}:
sum{s in SUPPLIERS} x[s,c] >= demand[c];

con zero{c in CUSTOMERS, s in SUPPLIERS : cost[s,c]=1E10}: x[s,c]=0;

* Solve the model;
solve with lp/solver=primal;

* Create optimum values in a SAS dataset ‘optimout’;
create data optimout
from [SUPPLIERS CUSTOMERS]
={s in SUPPLIERS, c in CUSTOMERS: x[s,c]^=0}
amount=x;

* End of OPTMODEL Procedure;
quit;
%mend model;

* The report writing macro;
%macro report;
* report the results in a tabulated form;
proc tabulate data=optimout;
title &_title;
class SUPPLIERS CUSTOMERS ;
var amount;
table SUPPLIERS =” Suppliers”,
CUSTOMERS*amount*sum
/ BOX=’Amount of suppliers to customers’ ;
run;
%mend report;

* The ortrans macro for transportation problem;

%macro ortrans;
%data;
%model;
%report;
%mend ortrans;

%ortrans;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-2-3

Program 3.1


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/

* SAS procedure for minimum cost flow problem;
option nodate ;
%let _data=’c:/sasor/data3_1.txt’;
%let _title=’Example 3.1. Minimum cost flow problem’;
%let _tail=origin;
%let _head=destination;
%let _cost=cost;
%let _capac=capacity;
%let _demand=demand;
%let _supply=supply;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;

proc import
datafile=&_data
out=dcost
dbms=tab
replace;
getnames=yes;
run;

%mend data;
* The model building macro;
%macro model;

proc netflow
arcdata=dcost
arcout=arcout1
nodeout=nodeout1;
tail &_tail;
head &_head;
cost &_cost;
capac &_capac;
demand &_demand;
supply &_supply;
run;

%put &_ORNETFL;
%mend model;

* The report writing macro;
%macro report;
title &_title;
proc print
data=arcout1;
sum _fcost_;
proc print
data=nodeout1;
run;
%mend report;

* A SAS macro for minimum cost capacitated flow problem;

%macro ormcflow;
%data;
%model;
%report;
%mend ormcflow;
%ormcflow;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-3-1

Program 3.2


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/


* A SAS procedure for maximum flow problem;
option nodate;
%let _data=’c:/sasor/data3_2.txt’;
%let _title=’Example 3.5. Maximum flow problem ‘;
%let _sourcenode=’a0′;
%let _sinknode=’a9’;
%let _tail=origin;
%let _head=dest;
%let _lowerb=lowerb;
%let _capac=upperb;
* The data handling macro;

%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dcost
dbms=tab
replace;
getnames=yes;
run;
%mend data;

* The model building macro;
%macro model;

proc netflow
maxflow
arcdata=dcost
source=&_sourcenode
sink=&_sinknode
arcout=arcout1
nodeout=nodeout1;
minflow &_lowerb;
capac &_capac;
tail &_tail;
head &_head;
run;

%put &_ORNETFL;
%mend model;

* The report writing macro;
%macro report;
title &_title ‘, how to get from ‘ &_sourcenode ‘ to ‘ &_sinknode ;

proc print
data=arcout1;
sum _fcost_;

proc print
data=nodeout1;
run;
%mend report;

* A SAS macro for maximum flow problem;
%macro ormaxflow;
%data;
%model;
%report;
%mend ormaxflow;

%ormaxflow;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-3-2

Program 3.3


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/


* SAS macro for shortest path problem;
%let _title=’Example 3.3: An example of a shortest path network.’;
%let _data=’c:\sasor\data3_3.txt’;
%let _sourcenode=’City1′;
%let _sinknode=’City7′;
%let _cost=cost;
%let _tail=origin;
%let _head=dest;
option nodate;

* The data macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_data
out=dpath
dbms=tab
replace;
getnames=yes;
run;
%mend data;

* The model building macro;
%macro model;
proc netflow
shortpath
sourcenode=&_sourcenode
sinknode=&_sinknode
arcdata=dpath
arcout=arcout1;
cost &_cost;
tail &_tail;
head &_head;
run;
%put &_ORNETFL;
%mend model;

* The report writing macro;
%macro report;
title &_title ‘, how to get from ‘ &_sourcenode ‘ to ‘ &_sinknode ;
* Sort results by origin;

proc sort
data=arcout1
out=result1;
by _fcost_;
run;

* Print results sorted by origin;
proc print
data=result1;
sum _fcost_;
run;

* Sort results by total destination;
proc sort
data=arcout1
out=result2;
by _anumb_;
run;

* Print results sorted by total destination;
proc print
data=result2 (where=(_fcost_ ne 0));
sum _fcost_;
run;
%mend report;

%macro orshortpath;
%data;
%model;
%report;
%mend orshortpath;

%orshortpath;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-3-3

Program 4.1


/****************************************************************************
* This program is taken from the following book. If using please cite it as:                            *
* Emrouznejad, A. and W. Ho (2012). Applied Operations Research with SAS, *
* CRC Press: Taylor Francis Ltd, ISBN: 9781439841303.*
* For details please visit: http://www.sas-or.com.                          *
****************************************************************************/


* A SAS procedure for project network of type Activity-On-Node;
%let _title=’Example 4.1. A project network: Activity-On-Node Format : Critical Path Analysis’;
%let dcpm=’c:/sasor/Data4_1.txt’;
%let _activity=task;
%let _duration=days;
option nodate;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&dcpm
out=dcpm
dbms=tab
replace;
getnames=yes;
run;

%mend data;
* The model building macro;
%macro model;
proc cpm
out=cpmout1;
activity &_activity;
duration &_duration;
successor succ1 succ2 succ3;
run;

%put &_ORCPM_;
%mend model;

%macro report;
data cpmout (where=(T_float=0));
set cpmout1;
run;

title &_title;
proc print
data =cpmout1 ;
run;

title &_title;
proc print
data =cpmout ;
sum &_duration;
run;
%mend report;

* Invoke PROC CPM to schedule the project specifying
* the ACTIVITY, DURATION and SUCCESSOR variables;
%macro orcpm;
%data;
%model;
%report;
%mend orcpm;

%orcpm;


/****************************************************************************
******************************END of the program*****************************
****************************************************************************/

Permanent link to this article: http://sas-or.com/book/program-4-1