Author's posts

Program 9.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.                                                                *
************************************************************************************/


* Using PROC OPTMODEL for Efficiency Measurement with Data Envelopment Analysis (DEA);
option nodate ;
%let _title=’Example 9.2: Efficiency Measurement Using Data Envelopment Analysis (DEA)’;
%let _InData=’C:\sasor\Data9_2_In.txt’;
%let _OutData=’C:\sasor\Data9_2_Out.txt’;
%let _nInput=2;
%let _nOutput=3;
%let _nUnits=10;
%let _Orienta=’OUTPUT’; *alternative option is ‘OUTPUT’ for output orientation;
%let _outdea1=EffOutOri;
%let _outdea2=EffInOri;

/*NOTE to Convexity constraints in both models for VRS;*/

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

* Import text tab delimited output variables to SAS data file;
proc import
datafile=&_OutData
out=data17_output
dbms=tab
replace;
getnames=yes;
run;
%mend data;

* A DEA macro for CRS output orientation;
%macro model_outOri (j0);
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set INPUTS=1..&_nInput;
set OUTPUTS=1..&_nOutput;
set UNITS=1..&_nUnits;

* Define variables;
var L{UNITS} >=0;
var Sin{INPUTS} >=0 ;
var Sout{OUTPUTS} >=0 ;
var h;

* Define parameters;
number Xin{UNITS, INPUTS};
number Yout{UNITS,OUTPUTS};
string DMUS{UNITS};
number eff;
string DMUj0;

* Load unit names;
read data data17_input
into [_N_]
DMUS[_N_]=col(‘dmu’);


* Load matrix of input variables;
read data data17_input
into [_N_]
{i in INPUTS} ;

* Load matrix of output variables;
read data data17_output
into [_N_]
{r in OUTPUTS} ;

* Define objective function ;
max obj = h;

* Define constrains;
con InpCon_outOri{i in INPUTS}:
sum{j in UNITS} L[j]*Xin[j,i] + Sin[i]=Xin[&j0,i];

con OutCon_outOri{r in OUTPUTS}:
sum{j in UNITS} L[j]*Yout[j,r] – Sout[r]=h*Yout[&j0,r];

* Define convexity constraints for VRS models;
con Convexity: sum{j in UNITS} L[j]=1;

* Solve the model;
solve ;

* Save the efficiency scores in a SAS dataset;
eff=1/h.sol;
DMUj0=DMUS[&j0];
create data solj0 from DMUj0 eff;

*End of PROC OPTMODE;
quit;
%mend model_outOri;

* A DEA macro for CRS input orientation;
%macro model_inOri (j0);
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set INPUTS=1..&_nInput;
set OUTPUTS=1..&_nOutput;
set UNITS=1..&_nUnits;

* Define variables;
var L{UNITS} >=0;
var Sin{INPUTS} >=0 ;
var Sout{OUTPUTS} >=0 ;
var h;

* Define parameters;
number Xin{UNITS, INPUTS};
number Yout{UNITS,OUTPUTS};
string DMUS{UNITS};
number eff;
string DMUj0;

* Load unit names;
read data data17_input
into [_N_]
DMUS[_N_]=col(‘dmu’);

* Load matrix of input variables;
read data data17_input
into [_N_]
{i in INPUTS} ;

* Load matrix of output variables;
read data data17_output
into [_N_]
{r in OUTPUTS} ;

* Define objective function ;
min obj = h;

* Define constrains;
con InpCon_inOri{i in INPUTS}:
sum{j in UNITS} L[j]*Xin[j,i] + Sin[i]=h*Xin[&j0,i];

con OutCon_inOri{r in OUTPUTS}:
sum{j in UNITS} L[j]*Yout[j,r] – Sout[r]=Yout[&j0,r];

* Define convexity constraints for VRS models;
/*con Convexity: sum{j in UNITS} L[j]=1;*/

* Solve the model;
solve ;

* Save the efficiency scores in a SAS dataset;
eff=h.sol;
DMUj0=DMUS[&j0];
create data solj0 from DMUj0 eff ;

*End of PROC OPTMODE;
quit;
%mend model_inOri;

%macro report;
* Delete previusly created datasets;
proc datasets nolist;
delete &_outdea1 &_outdea2;
run;

* Select the model and execute it for each unit of assessment;
%do j0=1 %to &_nUnits;
%if &_Orienta=”INPUT”
%then %do;
%model_inOri (&j0);
* Save the efficiency scores in a SAS dataset;
proc datasets nolist;
append base=&_outdea2=solj0;
run;
%end;
%else %do;
%model_outOri (&j0);
proc datasets nolist;
append base=&_outdea1 data=solj0;
run;
%end;
%end;

%if &_Orienta=”INPUT”
%then %do;
* Sort and print the results by efficiency score;
proc sort data=&_outdea2;
by eff;
run;
proc print data=&_outdea2;
run;

* Sort and print the results by unit name;
proc sort data=&_outdea2;
by DMUj0;
run;
proc print data=&_outdea2;
run;
%end;
%else %do;
* Sort and print the results by efficiency score;
proc sort data=&_outdea1;
by eff;
run;
proc print data=&_outdea1; title &_title;
run;

* Sort and print the results by unit name;
proc sort data=&_outdea1;
by DMUj0;
run;
proc print data=&_outdea1; title &_title;
run;
%end;

%mend report;

%macro ordea;
%data;
%report;
%mend ordea;

%ordea;


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

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

Program 9.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.                                                                *
************************************************************************************/


* Using PROC OPTMODEL for Malmquist Productivity Index and its Components;
option nodate ;
%let _title=’Example 9.3. Malmquist Productivity Index and its Components’;
%let _InData_P1=’C:\sasor\Data9_3_In_P1.txt’;
%let _OutData_P1=’C:\sasor\Data9_3_Out_P1.txt’;
%let _InData_P2=’C:\sasor\Data9_3_In_P2.txt’;
%let _OutData_P2=’C:\sasor\Data9_3_Out_P2.txt’;
%let _nInput=2;
%let _nOutput=2;
%let _nUnits=6;
%let _outMalm1=outMalm1;
%let _outMalm2=outMalm2;
%let _Orienta=’INPUTMIN’; * Alternative selection is ‘OUTPUTMAX’;

%macro importdata (txtFile, sasFile);
proc import
datafile=&txtFile
out=&sasFile
dbms=tab
replace;
getnames=yes;
run;
%mend importdata;

* The data handling macro;
%macro data;
* Import text tab delimited input variables to SAS data file, period1;
%importdata(&_InData_P1,data9_3_input_P1);
* Import text tab delimited output variables to SAS data file, period1;
%importdata(&_OutData_P1,data9_3_output_P1);
* Import text tab delimited input variables to SAS data file, period2;
%importdata(&_InData_P2,data9_3_input_P2);
* Import text tab delimited output variables to SAS data file, period2;
%importdata(&_OutData_P2,data9_3_output_P2);
%mend data;

* A DEA macro for CRS output orientation;
%macro model_outOri ( per1, per2,j0);
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set INPUTS=1..&_nInput;
set OUTPUTS=1..&_nOutput;
set UNITS=1..&_nUnits;
set PERIODS=1..2;

* Define variables;
var L{UNITS} >=0;
var Sin{INPUTS} >=0 ;
var Sout{OUTPUTS} >=0 ;
var h;

* Define parameters;
number Xin{PERIODS,UNITS, INPUTS};
number Yout{PERIODS,UNITS,OUTPUTS};
string DMUS{UNITS};
number eff;
string DMUj0;
number p1;
number p2;

* Load unit names;
read data data9_3_input_P1
into [_N_]
DMUS[_N_]=col(‘dmu’);

* Load matrix of input variables;
read data data9_3_input_P1
into [_N_]
{i in INPUTS} <Xin[1,_N_,i]=col(‘input’||i)>;

* Load matrix of input variables;
read data data9_3_input_P2
into [_N_]
{i in INPUTS} <Xin[2,_N_,i]=col(‘input’||i)>;

* Load matrix of input variables;
read data data9_3_output_P1
into [_N_]
{r in OUTPUTS} <Yout[1,_N_,r]=col(‘output’||r)>;

* Load matrix of input variables;
read data data9_3_output_P2
into [_N_]
{r in OUTPUTS} <Yout[2,_N_,r]=col(‘output’||r)>;

* Define objective function ;
max obj = h;

* Define constrains;
con InpCon_outOri{i in INPUTS}:
sum{j in UNITS} L[j]*Xin[&Per1,j,i] + Sin[i]=Xin[&Per2,&j0,i];

con OutCon_outOri{r in OUTPUTS}:
sum{j in UNITS} L[j]*Yout[&Per1,j,r] – Sout[r]=h*Yout[&Per2,&j0,r];
* Solve the model;
solve ;

* Save the efficiency scores in a SAS dataset;
eff=1/h.sol;
DMUj0=DMUS[&j0];
p1=&Per1;
p2=&Per2;
create data solj0 from DMUj0 p1 p2 eff;

*End of PROC OPTMODE;
quit;
%mend model_outOri;

%macro report(per1,per2);
* Select the model and execute it for each unit of assessment;
%do j0=1 %to &_nUnits;
%model_outOri (&per1,&per2,&j0);
* Save the results for report writing;
proc datasets nolist;
append base=&_outMalm1 data=solj0;
run;
%end;
%mend report;

%macro ormalm;
%data;
* Delete previously created datasets;
proc datasets nolist;
delete &_outMalm1;
run;

%report(1,1);
%report(2,2);
%report(1,2);
%report(2,1);

data &_outMalm2;
merge &_outMalm1(WHERE=(p1=1 and p2=1) RENAME=(eff=DEA11))
&_outMalm1(WHERE=(p1=1 and p2=2) RENAME=(eff=DEA12))
&_outMalm1(WHERE=(p1=2 and p2=1) RENAME=(eff=DEA21))
&_outMalm1(WHERE=(p1=2 and p2=2) RENAME=(eff=DEA22));
by dmuj0;
drop p1 p2;
run;

data &_outMalm2(drop=DEA21 DEA12 rename=(DEA11=EffPeriod1 DEA22=EffPeriod2));
set &_outMalm2;
EffChange=DEA22/DEA11;
TechChange=sqrt((DEA12*DEA11)/(DEA22*DEA21));
MalmIndex=EffChange*TechChange;
run;

proc print; run;

%mend ormalm;

%ormalm;


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

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

Exercise 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_exercise: Using PROC OPTMODEL for Transportation problem;
option nodate ;
%let _title=’Example 2.1_exercise: Transportation problem for three suppliers and four customers.’;
%let _data=’c:\sasor\data2_1_exercise.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} ;

* 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/exercise-2-1

Exercise 2.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.                                                                *
************************************************************************************/


* Using PROC OPTMODEL for Assignment problem;
option nodate ;
%let _title=’Assignment network: solution to exercise 2.2′;
%let _data=’c:\sasor\data2_2_exercise.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..4};


* 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} <costprofit[worker,t]=col(“task”||t)>;


* 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/exercise-2-2

Exercise 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=’solution to exercise 2.3′;
%let _data=’c:\sasor\data2_3_exercise.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/exercise-2-3

Exercise 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 macro for minimum cost flow problem: solution to exercise 3.4;
%let _title=’Minimum cost flow problem, solution to exercise 3.1′;
option nodate ;
%let _data=’c:/sasor/Data3_1_exercise.txt’;
%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/exercise-3-1

Exercise 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: solution to exercise 3.2;
%let _title=’Maximum flow problem: solution to exercise 3.2′;
option nodate;
%let _data=’c:/sasor/Data3_2_exercise.txt’;
%let _sourcenode=’a0′;
%let _sinknode=’a8′;
%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/exercise-3-2

Exercise 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: solution to exercise 3.6.;
%let _title=’An example of a shortest path network, solution to exercise 3.3′;
%let _data=’c:\sasor\Data3_3_exercise.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/exercise-3-3

Exercise 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: solution to exercise 4.1.;
%let _title=’A project network: Activity-On-Node Format: Critical Path ,Analysis, solution to exercise 4.1.’;
%let dcpm=’c:/sasor/Data4_1_exercise.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/exercise-4-1

Exercise 4.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 project network of type Activity-On-Node: solution to exercise 3.8.;
%let _title=’A project network: Activity-On-Node Format: solution to exercise 4.2.’;
option nodate;
%let _data=’c:/sasor/Data4_2_exercise.txt’;
%let _activity=task;
%let _duration=days;

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

data dpert;
set dpert;
days=(Opti+4*most+pessi)/6;
variance=((Opti-pessi)/6)**2;
run;
%mend data;

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

* The report writing macro;
%macro report;

data pertout1 ;
merge dpert pertout;
by task;
run;

data pertout2 (where=(T_float=0));
merge dpert pertout;
by task;
run;

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

proc print
data =pertout2 ;
sum T_float;
sum days;
sum variance;
run;
%mend report;

%macro orpert;
%data;
%model;
%report;
%mend orpert;

%orpert;


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

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

Exercise 5.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 macro for assembly line balancing problem;
%let _title=’ A project network using PROC OPTMODEL, Assembly line balancing problem: solution to exercise 5.1′;
%let dataalb=’c:/sasor/Data5_1_exercise.txt’;
%let _activity=task;
%let _duration=time;
%let _earliest =Ei;
%let _latest =Li;
%let _nTask=8;
%let _nWork=4;
%let _CycleT=1.8;
%let _outdual=outdual ;
%let _outprimal=outprimal ;
%let _outtable=outtable;
option nodate;


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


data dataalb2 (drop= i);
set dataalb;
array W(&_nWork);
do i=1 to &_nWork;
if Ei<=i<=Li then w(i)=1; else w(i)=0;
end;
run;
%mend data;


* The model building macro;
%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set TASKS=1..&_nTask;
set WORKS=1..&_nWork;


* Define variables;
var A{WORKS} binary >=0;
var x{TASKS,WORKS} binary >=0 ;
var z;


* Define parameters;
number Time{TASKS};
number EL{TASKS,WORKS};
number Succ1{TASKS};
number Succ2{TASKS};
number Succ3{TASKS};


* Load time (duration of each task);
read data dataalb2
into [_N_]
Time[_N_]=col(‘time’);


* Load EL (Earliest/Latest workstations to which the tasks can be assigned);
read data dataalb2
into [_N_]
{i in WORKS} <EL[_N_,i]=col(‘W’||i)>;


* Load Succ1 (First Immediate Successor);
read data dataalb2
into [_N_]
Succ1[_N_]=col(‘Succ1’);


* Load Succ2 (Second Immediate Successor);
read data dataalb2
into [_N_]
Succ2[_N_]=col(‘Succ2’);


* Load Succ3 (Third Immediate Successor);
read data dataalb2
into [_N_]
Succ3[_N_]=col(‘Succ3’);


* Define objective function ;
min obj = z;


* Define constrains;
con Objective: z= sum{i in WORKS} A[i];


con assignment_one_workstation {j in TASKS}: sum{i in WORKS} EL[j,i]*x[j,i]=1;


con cycle_time {i in WORKS}: sum{j in TASKS} EL[j,i]*Time[j]*x[j,i]<=&_CycleT;


con precedence1 {j in TASKS: succ1[j]>0}: sum{i in WORKS} i*EL[j,i]*x[j,i]-sum{i in WORKS} i*EL[succ1[j],i]*x[succ1[j],i]<=0; con precedence2 {j in TASKS: succ2[j]>0}: sum{i in WORKS} i*EL[j,i]*x[j,i]-sum{i in WORKS} i*EL[succ2[j],i]*x[succ2[j],i]<=0; con precedence3 {j in TASKS: succ3[j]>0}: sum{i in WORKS} i*EL[j,i]*x[j,i]-sum{i in WORKS} i*EL[succ3[j],i]*x[succ3[j],i]<=0;


con workstation {i in WORKS}: sum{j in TASKS} EL[j,i]*x[j,i]-sum{j in TASKS} EL[j,i]*A[i]<=0;


con utilize_earlier {i1 in WORKS, i2 in WORKS: i2>i1}: A[i2]-A[i1]<=0;


expand;
* Solve the model;
solve with MILP;
%put &_OROPTMODEL_;
* Create optimum values in a SAS dataset ‘optimout’;
create data optimout
from [TASKS WORKS]
={j in TASKS, i in WORKS}
amount=x;


*End of PROC OPTMODE;
quit;
%mend model;


* The report writing macro;
%macro report;
* report the results in a tabulated form;


proc tabulate data=optimout;
title &_title;
class TASKS WORKS ;
var amount;
table TASKS =” TASKS”,
WORKS*amount*sum
/ BOX=’Assigning Task to Workstation’ ;
run;
%mend report;


* Assembly line problem;
%macro oralbp;
%data;
%model;
%report;
%mend oralbp;


%oralbp;


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

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

Exercise 6.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 macro for traveling salesman problem: solution to exercise 3.10.;
%let _title=’Traveling salesman problem: solution to exercise 3.10.’;
%let _data=’c:/sasor/Data3_10_exercise.txt’;
%let _travelP=city;
option nodate;

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


data dtsp(drop=&_travelP);
set dtsp1;
run;
%mend data;

* The model building macro;
%macro model;
proc ga Matrix1 = dtsp;
call SetEncoding (‘S6’);
call SetObj(‘TSP’,0,’distances’,Matrix1);
call SetCross(‘Order’);
call Initialize(‘DEFAULT’, 6);
call ContinueFor(85);
run;
%mend model;

* The report writing macro;
%macro report;
title &_title;
proc print;
run;
%mend report;

* A SAS macro for traveling salesman problem;
%macro ortsp;
%data;
%model;
%report;
%mend ortsp;
* SAS procedure for traveling salesman problem;

%ortsp;


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

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