Category: Solution to Exercises

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

* A SAS procedure for SMT placement machines or the component sequencing problem;
%let _title=’Example 7.3: SMT placement machines or the component sequencing problem’;
%let _dataC=’c:/sasor/Data7_3_C_exercise.txt’;
%let _dataF=’c:/sasor/Data7_3_F_exercise.txt’;
%let _nFeeder=6;
%let _nCompon=6;
%let _outprimal= outprimal;
%let _outdual= outdual;
%let _outtable= outtable;
option nodate;

* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_dataC
out=dataC
dbms=tab
replace;
getnames=yes;
run;
proc import
datafile=&_dataF
out=dataF
dbms=tab
replace;
getnames=yes;
run;
%mend data;
* The model building macro;
%macro model;
* Starting OPTMODEL Procedure;
proc optmodel;
* Define sets;
set COMP=0..&_nCompon;
set Feed=0..&_nFeeder;

* Define variables;
var u{COMP} integer >=0;
var x{COMP,COMP} binary >=0 ;
var z;

* Define parameters;
number d{COMP,COMP};
number cx {COMP};
number cy {COMP};
number type {COMP};
number fx {FEED};
number fy {FEED};
number randfeeder {FEED};
number d1;
number d2;
number Distance{COMP,FEED};
number ds2{COMP,FEED};

* Load component data;
read data Datac into [_N_] type[_N_]=col(‘type’);
read data Datac into [_N_] cx[_N_]=col(‘cx’);
read data Datac into [_N_] cy[_N_]=col(‘cy’);

* Load feeder data;
read data Dataf into [_N_] fx[_N_]=col(‘fx’);
read data Dataf into [_N_] fy[_N_]=col(‘fy’);
read data Dataf into [_N_] randfeeder[_N_]=col(‘randfeeder’);

cx[0]=0; cy[0]=0; fx[0]=0; fy[0]=0;

for {i in FEED, j in COMP}
do;
if (i ne 0 & j ne 0)
then do; d1=(cx[i]-fx[j])**2; d2=(cy[i]-fy[j])**2; end;
else if (i=0 & j=0)
then do; d1=0; d2=0; end;
else if (i=0)
then do; d1=(cx[j]**2); d2=(cy[j]**2); end;
else if (j=0)
then do; d1=(fx[i]**2); d2=(fy[i]**2); end;

Distance[i,j]=round(sqrt( d1+d2),0.000001);
end;
print Distance;
for {i in FEED, j in COMP}
do;
if (i=j) then ds2[i,j]=0;
else if i=0 then ds2[j,i]=Distance[randfeeder[j],0]+Distance[j,randfeeder[j]];
else if j=0 then ds2[j,i]=Distance[j,i];
else ds2[j,i]=Distance[j,randfeeder[j]]+Distance[i,randfeeder[j]];
end;

* Define objective function ;
min obj = z;

* Define constrains;
con Objective: z= sum{i in FEED, j in COMP} ds2[j,i]*x[i,j];

con component_immediately_before1 {i in FEED}: sum{j in COMP: i ne j} x[j,i]=1;
con component_immediately_before2 {j in COMP}: sum{i in FEED: i ne j} x[j,i]=1;
con subtour_elimination {i in FEED, j in COMP : i ne 0 & j ne 0 & i ne j}: u[i]-u[j] + &_nCompon * x[i,j]<=&_nCompon-1;

solve with MILP;

expand;

%put &_OROPTMODEL_;

* Create optimum values of x in a SAS dataset ‘optimout1’;
create data optimout1
from [COMP FEED]
={i in COMP, j in FEED: x[i,j] ne 0}
amount=x ;

* Create optimum values of u in a SAS dataset ‘optimout2’;
create data optimout2
from [COMP]
={i in COMP: u[i]}
amount=u ;
*End of PROC OPTMODE;
vquit;
%mend model;
* The report writing macro;
%macro report;
title &_title;

proc tabulate data=optimout1;
title &_title;
class COMP FEED ;
var amount;
table COMP =”COMP”,
FEED*amount*sum
/ BOX=’Component sequencing’;
run;

proc tabulate data=optimout2;
title &_title;
class COMP ;
var amount;
table COMP =”COMP”,
amount*sum
/ BOX=’Component sequencing’;
run;

%mend report;

* A SAS macro for SMT placement machines or the component sequencing problem;
%macro orcsp;
%data;
%model;
%report;
%mend orcsp;

%orcsp;


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

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

Exercise 8.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 Analytical Hierarchy Process: solution to exercise 8.1.;
%let _title=’Analytical Hierarchy Process, solution to exercise 8.1′;
option nodate;
%let _dataC=’c:/sasor/Data8_1_C_exercise.txt’;
%let _dataA=’c:/sasor/Data8_1_A_exercise.txt’;
%let _nCriteria=4;
%let _nAlternative=3;
options linesize=100;

* A macro to import data;
%macro data;
* To import Criteria Pair;
proc import
datafile=&_dataC
out=dataC
dbms=tab
replace;
getnames=no;
run;

* To import Alternative Pair;
proc import
datafile=&_dataA
out=dataA
dbms=tab
replace;
getnames=no;
run;

data dataA(rename=(VAR1=Criteria VAR2=Alternative));
set dataA;
run;
%mend data;
*A macro for calculating AHP measures including Consistency;
%macro Consist(nDim, LamArray, AverageArray,PrArray, Lambda, CI, CR, RI,RIr);

do i1=1 to &nDim;
&LamArray(i1)=0;
do j1=1 to &nDim;
&LamArray(i1)=&LamArray(i1)+&AverageArray(j1)*&PrArray(i1,j1);
end;
end;

&Lambda=0;
do i1=1 to &nDim;
&Lambda=&Lambda+&LamArray(i1)/&AverageArray(i1);
end;

&Lambda=&Lambda/&nDim;
&CI=(&Lambda-&nDim)/(&nDim-1);
RI=&RIr(&nDim);
&CR=&CI/RI;
%mend Consist;

* A macro for calculating average priorities;
%macro Priority (MyMatrix,myDimension,AverageMyMatrix,DivideMyMatrix,SumMyMatrix);
do j=1 to &myDimension;
&SumMyMatrix(j)=0;
do i=1 to &myDimension;
&SumMyMatrix(j)=&SumMyMatrix(j)+&MyMatrix(i,j);
end;
end;

do i=1 to &myDimension;
do j=1 to &myDimension;
&DivideMyMatrix(i,j)=&MyMatrix(i,j)/&SumMyMatrix(j);
end;
end;

do i=1 to &myDimension;
&AverageMyMatrix(i)=0;
do j=1 to &myDimension;
&AverageMyMatrix(i)=&AverageMyMatrix(i)+&DivideMyMatrix(i,j);
end;
&AverageMyMatrix(i)=&AverageMyMatrix(i)/&myDimension;
end;
%mend Priority;
* A macro for writing priority matrix;
%macro report1 (Name,nDim,MyName,MyMatrix,AverageMyMatrix,Lambda, CI, CR, RI);
File Print;
title &_Title;
put // ” ” &Name @@;
do j1= 1 to &nDim;
c=9*j1+3;
put @c &MyName(j1) @@ ;
end;
c=c+9; put @c “Priori” @@ ;
c=c+9; put @c “LamMax” @@ ;
c=c+9; put @c “CI” @@ ;
c=c+9; put @c “RI” @@ ;
c=c+9; put @c “CR”;

do j1= 1 to &nDim+5;
c=9*j1+1; put @c ” ——– ” @@ ;
end;

do i1= 1 to &nDim;
put / ” ” &MyName(i1) @@;
do j1= 1 to &nDim;
c=9*j1+1; put @c &MyMatrix(i1,j1) 8.4 @@;
end;
c=c+10; put @c &AverageMyMatrix(i1) 8.4 @@;
end;

put;
do j1= 1 to &nDim+5;
c=9*j1+1; put @c ” ——– ” @@ ;
end;

put ; c=9*&nDim+10;put @c “Total ===>” @@;

c=&nDim*9+20; put @c Lambda 8.4 @@ ;
c=c+9; put @c CI 8.4 @@ ;
c=c+9; put @c RI 8.4 @@ ;
c=c+9; put @c CR 8.4 @@ ;
%mend report1;
* A macro for writing AHP resluts;
%macro report2 (nDimA,nDimC,RankAarray,AveragePAarray,AveragePCarray);
do j=1 to &nDimA;
RankAarray(j)=0;
do i=1 to &nDimC;
RankAarray(j)=RankAarray(j)+AveragePAarray(i,j)*AveragePCarray(i);
end;
end;

put // @10 “————————–” ;
put @10 “AHP Result “;
put @10 &_Title;
put @10 “————————–” ;
put @10 “Alternative” @25 “Priority” ;
put @10 “———–” @25 “———” ;

do j1= 1 to &nDimA;
put @10 NAarray(j1) @@ ;
put @25 RankAarray(j1) 8.4 ;
end;
put @10 “————————–” ;
%mend report2;

* A macro for AHP calculation;
%macro model;

data _NULL_;
array NCarray(&_nCriteria) $ NC1-NC&_nCriteria;
array NAarray(&_nAlternative) $ NA1-NA&_nAlternative;

array PCarray(&_nCriteria,&_nCriteria) ;
array PAarray(&_nCriteria,&_nAlternative,&_nAlternative) ;

array TempPAarray(&_nAlternative,&_nAlternative) ;
array TempAeragePAarray(&_nAlternative) ;

array AveragePAarray(&_nCriteria,&_nAlternative) ;
array AveragePCarray(&_nCriteria) ;

array DividePCarray(&_nCriteria,&_nCriteria) ;
array SumPCarray(&_nCriteria) ;

array DividePAarray(&_nAlternative,&_nAlternative) ;
array SumPAarray(&_nAlternative) ;

array RankAarray(&_nAlternative) ;
array LambdaCArray(&_nCriteria);
array LambdaAArray(&_nAlternative);

array RIr(10) RIr1-RIr10;
RIr3=0.58;
RIr4=0.90;
RIr5=1.12;
RIr6=1.24;
RIr7=1.32;
RIr8=1.41;

do i=1 to &_nCriteria;
Link ReaddataC;
NCarray(i)=VAR1;
%do j=2 %to &_nCriteria+1;
PCarray(i,&j-1)=VAR&j;
%end;
end;

do i=1 to &_nCriteria;
do j=1 to &_nAlternative;
Link ReaddataA;
NAarray(j)=Alternative;
%do k=3 %to &_nAlternative+2;
PAarray(i,j,&k-2)=VAR&k;
%end;
end;
end;

do i=1 to &_nCriteria;
do j=1 to &_nCriteria;
if PCarray(i,j)=. then PCarray(i,j)=1/PCarray(j,i);
end;
end;

do i=1 to &_nAlternative;
RankAarray(i)=0;
end;

do i=1 to &_nCriteria;
do j=1 to &_nAlternative;
do k=1 to &_nAlternative;
if PAarray(i,j,k)=. then PAarray(i,j,k)=1/PAarray(i,k,j);
end;
end;
end;

%Priority (PCarray,&_nCriteria,AveragePCarray,DividePCarray,SumPCarray);
%Consist (&_nCriteria,LambdaCArray,AveragePCarray,PCarray,Lambda, CI, CR, RI,RIr);
%report1 (“Criteria”, &_nCriteria,NCarray,PCarray,AveragePCarray,Lambda, CI, CR, RI);

do m=1 to &_nCriteria;
do j=1 to &_nAlternative;
do k=1 to &_nAlternative;
TempPAarray(j,k)=PAarray(m,j,k);
end;
end;
%Priority (TempPAarray,&_nAlternative,TempAeragePAarray,DividePAarray,SumPAarray);
do k=1 to &_nAlternative;
AveragePAarray(m,k)=TempAeragePAarray(k);
end;
%Consist (&_nAlternative,LambdaAArray,TempAeragePAarray,TempPAarray,Lambda, CI, CR, RI,RIr);
%report1 (NCarray (m),&_nAlternative,NAarray,TempPAarray,TempAeragePAarray,Lambda, CI, CR, RI);
end;

%report2 (&_nAlternative,&_nCriteria,RankAarray,AveragePAarray,AveragePCarray);

ReaddataC: set dataC; return;
ReaddataA: set dataA; return;
run;
%mend model;

* A SAS macro for analytic hierarchy process problem;
%macro orahp;
%data;
%model;
%mend orahp;

%orahp;


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

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

Exercise 8.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 MCDM;
options nodate;
%let _data=’c:\sasor\MCDM.txt’;
%let _title=’Example 8.2: An example of MCDM: solution to exercise 8.2.’;
%let _dataMCDM=’c:/sasor/Data8_2_exercise.txt’;
%let _nWareh=5;
%let _nCustomer=7;
%let _TotalCost=458000;
%let _outprimal=outprimal;
%let _outdual=outdual;
%let _outtable=outtable;
%let _myLarge=100000;
* The data handling macro;
%macro data;
* Import text tab delimited data file to SAS data file;
proc import
datafile=&_dataMCDM
out=dataC
dbms=tab
replace;
getnames=yes;
run;
%mend data;
* The model building macro;
%macro model (_stage);

* Starting OPTMODEL Procedure;
proc optmodel;

* Define sets;
set WAREHOUSE=1..&_nWareh;
set CUSTOMER=1..&_nCustomer;
set WAREHOUSE_CUSTOMER_TWO= 1..&_nCustomer+2*&_nWareh+2;
set Priority = 1..&_nWareh+3;
set stage ;
stage = {&_stage};


* Define variables;
var X{WAREHOUSE, CUSTOMER} integer >=0;
var u {WAREHOUSE} binary ;
var v {WAREHOUSE} binary ;
var w {WAREHOUSE} binary ;
var dPlus {WAREHOUSE_CUSTOMER_TWO} >=0 ;
var dNeg {WAREHOUSE_CUSTOMER_TWO} >=0 ;
var z{Priority} ;

* Define parameters;
number dc{WAREHOUSE, CUSTOMER};
number demand{CUSTOMER};
number QQ{WAREHOUSE};
number q{WAREHOUSE};
number hc{WAREHOUSE};
number fc{WAREHOUSE};
number pc{WAREHOUSE};
number WarehPriority{WAREHOUSE};
number P{Priority,Priority};


for {i in Priority , j in Priority: i ne j} P[i,j]=0;
for {i in Priority , j in Priority: i eq j} P[i,j]=1;


* Load the Unit delivery cost matrix;
read data Datac (where =(Wareh ne “demand”))
into [_N_]
{j in CUSTOMER} <dc[_N_,j]=col(‘Cust’||j)>;


* Load the demand array;
read data Datac (where =(Wareh eq “demand”))
into
{j in CUSTOMER} <demand[j]=col(‘Cust’||j)>;


* Load the Maximum throughput of warehouse array;
read data Datac (where =(Wareh ne “demand”))
into [_N_]
QQ[_N_]=col(‘QQi’);


* Load the Minimum throughput of warehouse array;
read data Datac (where =(Wareh ne “demand”))
into [_N_]
q[_N_]=col(‘qi’);


* Load the Unit holding cost array;
read data Datac (where =(Wareh ne “demand”))
into [_N_]
hc[_N_]=col(‘hci’);


* Load the Fixed cost array;
read data Datac (where =(Wareh ne “demand”))
into [_N_]
fc[_N_]=col(‘fci’);


* Load the Penalty cost array;
read data Datac (where =(Wareh ne “demand”))
into [_N_]
pc[_N_]=col(‘pci’);


* Load the Warehouse Priority that is obtained from AHP;
read data Datac (where =(Wareh ne “demand”))
into [_N_]
WarehPriority[_N_]=col(‘WarehPriority’);


* Define system constrains;
con sumV: sum{i in WAREHOUSE} v[i] <= &_nWareh;
con Mu {i in WAREHOUSE}: sum{j in CUSTOMER} x[i,j] + &_myLarge * u[i] <= q[i];
con Mv {i in WAREHOUSE}: sum{j in CUSTOMER} x[i,j] – &_myLarge * v[i] <= 0;
con wuv {i in WAREHOUSE}: w[i] – u[i] – v[i] = -1;
* Define Resource constraints;


* Priority 1 (P1);
con P1_1{ s in Priority, i in WAREHOUSE: s >= 1}: sum{j in CUSTOMER} x[i,j] – dPlus[i] + dNeg[i] <= QQ[i]; con P1_2{ s in Priority, j in CUSTOMER : s >= 1}: sum{i in WAREHOUSE} x[i,j] – dPlus[j+&_nWareh] + dNeg[j+&_nWareh] <= demand[j];


* Priority 2 (P2);
con P2{s in Priority : s >= 2}: sum{i in WAREHOUSE,j in CUSTOMER} (hc[i]+dc[i,j])*x[i,j] + sum{i in WAREHOUSE} fc[i]*v[i] – dPlus[&_nCustomer+&_nWareh+1] + dNeg[&_nCustomer+&_nWareh+1] <= &_TotalCost;


* Priority 3 (P3);
con P3{s in Priority : s >= 3}: sum{i in WAREHOUSE} pc[i]*w[i] – dPlus[&_nCustomer+&_nWareh+2] + dNeg[&_nCustomer+&_nWareh+2] = 0;


* Define AHP (P4) priority constraints;
con P4{s in Priority, i in WAREHOUSE: s >= i+3}: v[WarehPriority[i]]- dPlus[&_nCustomer+&_nWareh+i+2] + dNeg[&_nCustomer+&_nWareh+i+2]=1;


con z [1] = (sum {k in 1..&_nWareh} dPlus[k] + sum {k in &_nWareh+1..&_nWareh+&_nCustomer} (dPlus[k]+dNeg[k]));
con z [2] = (dPlus[&_nWareh+&_nCustomer+1]);
con z [3] = (dPlus[&_nWareh+&_nCustomer+2]);
con z [4] = (dPlus[&_nWareh+&_nCustomer+3]+ dNeg[&_nWareh+&_nCustomer+3]);
con z [5] = (dPlus[&_nWareh+&_nCustomer+4]+ dNeg[&_nWareh+&_nCustomer+4]);
con z [6] = (dPlus[&_nWareh+&_nCustomer+5]+ dNeg[&_nWareh+&_nCustomer+5]);
con z [7] = (dPlus[&_nWareh+&_nCustomer+6]+ dNeg[&_nWareh+&_nCustomer+6]);
* Define objective function;


* objective function;
min obj = sum {s1 in Priority, s in stage} P[s1 , s ]* z[s1];


solve with MILP; %put &_OROPTMODEL_; expand;


create data _optimout
from [WAREHOUSE CUSTOMER]
={i in WAREHOUSE, j in CUSTOMER}
amount=X ;


quit;
%mend;

* The report writing macro;
%macro report (_stage);
* Report the results in a tabulated form;
title &_title ‘(P=’ &_stage ‘)’;
proc tabulate data=_optimout;
title &_title;
class WAREHOUSE CUSTOMER ;
var amount;
table WAREHOUSE =” WAREHOUSE”,
CUSTOMER*amount*sum
/ BOX=’x[WAREHOUSE& CUSTOMER] ‘ ;
run;
%mend report;


* A SAS macro for multiple criteria decision making problem ;
%macro ormcdm;
%data;
%do i=1 %to &_nWareh+3;
%model(&i);
%report(&i);
%end;
%mend ormcdm;


%ormcdm;


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

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

Exercise 9.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 Ordered Weighted Averaging Operators: solution to exercise 9.1.;
%let _title=’Ordered Weighted Averaging Operators, solution to exercise 9.1′;
option nodate ;
%let _data=’c:/sasor/Data9_1_exercise.txt’;
%let _alpha=0.25;
%let _ncriteria=7;
%let _nalternative=15;
%let _owa1=’c:/sasor/owaOut1.txt’;
%let _owa2=’c:/sasor/owaOut2.txt’;
%let _owaOut1=owaOut1;
%let _owaOut2=owaOut2;

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

%macro model;
proc optmodel;
set CRITERIA = 1..&_ncriteria;
set CRITERIA1 = 1..&_ncriteria-1;
set ALTERNATIVE = 1..&_nalternative;
number c{ALTERNATIVE, CRITERIA};
number rankAE{ALTERNATIVE} ;
number rankMIN{ALTERNATIVE} ;
number rankMAX{ALTERNATIVE} ;
number rankMEAN{ALTERNATIVE} ;
number corder{ALTERNATIVE, CRITERIA};
number wMAX{ALTERNATIVE};
number wMIN{ALTERNATIVE};
number wMEAN{ALTERNATIVE};
number ctemp;
number altName{ALTERNATIVE};
number alpha=&_alpha;
number n=&_ncriteria;
var w{CRITERIA} ;
var delta;

read data alts
into [ALTERNATIVE]
{j in CRITERIA} ;

min obj =delta;

con orness: (1/(n-1))* sum{i in CRITERIA} (n-i)*w[i] = alpha;

con wji{i in 1..n, j in i+1..n }:
w[j]-w[i]+delta >= 0;

con wij{i in 1..n, j in i+1..n }:
w[i]-w[j]+delta >= 0;

con swumw:
sum{i in 1..n} w[i]= 1;

solve ;
%report;

quit;
%mend model;

%macro report;
* MAX-OWA;
for {i in CRITERIA}
if i=1 then wMAX[i]=1; else wMAX[i]=0;

* MIN-OWA;
for {i in CRITERIA}
if i=n then wMIN[i]=1; else wMIN[i]=0;

* MEAN-OWA;
for {i in CRITERIA}
wMEAN[i]=1/n;

*Order the criteria;
for {k in ALTERNATIVE}
do;
for {i in CRITERIA}
for {j in CRITERIA}
do;
if c[k,j] do;
ctemp=c[k,i]; c[k,i]=c[k,j];c[k,j]=ctemp;
end;
end;
end;

*Rank by AE-OWA model;
for {k in ALTERNATIVE}
do;
rankAE[k]=0;
for {i in CRITERIA}
rankAE[k]=rankAE[k]+c[k,i]*w[i];
end;

*Rank by MIN-OWA model;
for {k in ALTERNATIVE}
do;
rankMIN[k]=0;
for {i in CRITERIA}
rankMIN[k]=rankMIN[k]+c[k,i]*wMIN[i];
end;

*Rank by MIN-OWA model;
for {k in ALTERNATIVE}
do;
rankMAX[k]=0;
for {i in CRITERIA}
rankMAX[k]=rankMAX[k]+c[k,i]*wMAX[i];
end;

*Rank by MEAN-OWA model;
for {k in ALTERNATIVE}
do;
rankMEAN[k]=0;
for {i in CRITERIA}
rankMEAN[k]=rankMEAN[k]+c[k,i]*wMEAN[i];
end;

* To save the OWA weight in a text file;
file &_owa1;
put ‘Criteria,’ ‘w,’ ‘wMAX,’ ‘wMIN,’ ‘wMEAN’;
for {i in CRITERIA}
do;
put i ‘,’ w[i] ‘,’ wMAX[i] ‘,’ wMIN[i]’,’ wMEAN[i];
end;

* To save the Alternative ranks in a text file;
file &_owa2;
put ‘Alternative,’ ‘rankAE,’ ‘rankMAX,’ ‘rankMIN,’ ‘rankMEAN’;
for {k in ALTERNATIVE}
do;
put k ‘,’ rankAE[k] ‘,’ rankMAX[k] ‘,’ rankMIN[k]’,’ rankMEAN[k];
end;

closefile &_owa1;
closefile &_owa2;

* To save the OWA weight in a SAS dataset;
proc import
datafile=&_owa1
out=&_owaOut1
dbms=csv
replace;
getnames=yes;
run;

proc print; title &_title; run;

* To save the Alternative ranks in a SAS dataset;
proc import
datafile=&_owa2
out=&_owaOut2
dbms=csv
replace;
getnames=yes;
run;
proc print; title &_title; run;

%mend report;

%macro orowa;
%data;
%model;
%mend orwa;

%orowa;


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

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

Exercise 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): solution to exercise 9.2;
option nodate ;
%let _title=’Efficiency Measurement Using Data Envelopment Analysis (DEA): solution to exercise 9.2′;
%let _InData=’C:\sasor\Data9_2_In_exercise.txt’;
%let _OutData=’C:\sasor\Data9_2_Out_exercise.txt’;
%let _nInput=3;
%let _nOutput=2;
%let _nUnits=20;
%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} <Xin[_N_,i]=col(‘input’||i)>;

* Load matrix of output variables;
read data data17_output
into [_N_]
{r in OUTPUTS} <Yout[_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[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} <Xin[_N_,i]=col(‘input’||i)>;

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

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

Exercise 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: solution to exercise 9.3;
option nodate ;
%let _title=’Malmquist Productivity Index and its Components: solution to exercise 9.3′;
%let _InData_P1=’C:\sasor\Data9_3_In_P1_exercise.txt’;
%let _OutData_P1=’C:\sasor\Data9_3_Out_P1_exercise.txt’;
%let _InData_P2=’C:\sasor\Data9_3_In_P2_exercise.txt’;
%let _OutData_P2=’C:\sasor\Data9_3_Out_P2_exercise.txt’;
%let _nInput=2;
%let _nOutput=2;
%let _nUnits=10;
%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} ;

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

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

* Load matrix of input variables;
read data data9_3_output_P2
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[&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/exercise-9-3