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