Category: Chapter 3 – Programs

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

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