goptions reset=all device=ps300 lfactor=5
         gunit=pct htext=2.5 vsize=4.375 in hsize=6.75 in
         ftext=swissb nodisplay;

proc greplay nofs igout=work.gseg; delete _all_; run; quit;

/* Input DATA step */
data psitest(drop=minpsi maxpsi days) summary(keep=maxpsi vendor days);
input @1 vendor $ @3 installd date7. @11 removed date7. @19 minpsi @23 maxpsi;
format installd removed date date7.;
length psicolor pattern $8;
/* Assign sequential test number and calculate test duration */
test=_n_;
days=intck('day',installd,removed);
output summary;

/* Create macro variable containing first test date */
if _n_=1 then call symput('stdate',put(installd,date7.));

/* Assign color and pattern values and create macro variables */
if vendor='A' then pattern='m1n45';
else if vendor='B' then pattern='m1x45';
else if vendor='C' then pattern='m1n135';
if (maxpsi lt 200) then psicolor='red';
else if ( 200 le maxpsi lt 300) then psicolor='yellow';
else if (maxpsi ge 300) then psicolor='green';
call symput('pat'||left(test),pattern);
call symput('color'||left(test),psicolor);

/* define polygon */
date=installd; psi=minpsi; output psitest;
psi=maxpsi; output psitest;
date=removed; output psitest;
psi=minpsi; output psitest;
date=installd; output psitest;

cards;
A 01jan95 07jan95 95  195
B 07jan95 20jan95 50  435
C 20jan95 25jan95 100 210
A 25jan95 02feb95 135 265
B 02feb95 15feb95 175 425
C 15feb95 23feb95 75  205
A 23feb95 28feb95 125 360
B 28feb95 10mar95 85  375
C 10mar95 17mar95 125 195
A 17mar95 20mar95 105 210
B 20mar95 25mar95 220 440
C 25mar95 01apr95 45  175
;
run;


/* Compile Annotate macros */
%annomac;

/* Create Annotate data set */
data anno;
set psitest end=eof;
by test;
/* Set lengths and type for Annotate variables */
%dclanno;
length text $10;

/* If first observation in the data set, label the first date on the
horizontal axis. */
if _n_=1 then do;
   %system(2,1,3);
   %move("&stdate"d,0);
   %cntl2txt;
   %system(2,9,3);
   %label("&stdate"d,-2.5,upcase(trim(left("&stdate"))),black,45,0,2,swissb,4);
end;

/* If last observation for each polygon, label the removal date on the
horizontal axis. */
if last.test then do;
   %system(2,1,3);
   %move(removed,0);
   %cntl2txt;
   %system(2,9,3);
   %label(removed,-2.5,trim(left(put(removed,date7.))),black,45,0,2,swissb,4);
end;

if eof then do;
/* Create macro variables containing the total number of samples
and the last test date */
call symput('total',test);
call symput('enddate',put(removed,date7.));

/* draw PSI legend */
%system(3,3,3);
%label(20,4,'MAX PSI:',black,0,0,2,swissb,<);

%bar(25,2,35,6,red,0,solid);
%cntl2txt;
%system(9,3,3);
%label(2,4,'0 - 200',black,0,0,2,swissb,>);

%system(3,3,3);
%bar(50,2,60,6,yellow,0,solid);
%cntl2txt;
%system(9,3,3);
%label(2,4,'200 - 300',black,0,0,2,swissb,>);

%system(3,3,3);
%bar(75,2,85,6,green,0,solid);
%cntl2txt;
%system(9,3,3);
%label(2,4,'> 300',black,0,0,2,swissb,>);

/* draw VENDOR legend */
%system(3,3,3);
%label(20,10,'VENDOR:',black,0,0,2,swissb,<);

%bar(25,8,35,12,*,0,R1);
%cntl2txt;
%system(9,3,3);
%label(2,10,'A',*,0,0,2,swissb,>);

%system(3,3,3);
%bar(50,8,60,12,*,0,X1);
%cntl2txt;
%system(9,3,3);
%label(2,10,'B',*,0,0,2,swissb,>);

%system(3,3,3);
%bar(75,8,85,12,*,0,L1);
%cntl2txt;
%system(9,3,3);
%label(2,10,'C',*,0,0,2,swissb,>);
end;
run;


/* Define macro to specify solid-filled map/plot interpolations */
%macro barcolor;
%do i=1 %to &total;
  SYMBOL&i INTERPOL=MSOLID COLOR=&&color&i VALUE=NONE;
%end;
%mend;

/* Define macro to specify patterned map/plot interpolations */
%macro barpatrn;
%do i=1 %to &total;
%let j=%eval(&i+&total);
  SYMBOL&j INTERPOL=&&pat&i COLOR=BLACK VALUE=NONE;
%end;
%mend;

/* Invoke the macros */
%barcolor;
%barpatrn;

proc gplot data=psitest;
title1 h=4 'Pressure Test for Valve #3162';
axis1 order="&stdate"d to "&enddate"d by day minor=none offset=(2,2) value=none
      label=none;
axis2 offset=(0,0) label=(a=90 r=0 "Pressure (psi)") origin=(10,);
axis3 offset=(0,0) label=none value=(j=l);
plot psi*date=test  / haxis=axis1  /* plot solid-colored polygons */
                      vaxis=axis2
                      nolegend     /* suppress the legend on the plot */
                      frame        /* draw a frame around the axis area */
                      vzero        /* start the vertical axis with a value of 0 */
                      vref=200 300 /* draw reference lines */
                      anno=anno;   /* specify the Annotate data set to apply */
plot2 psi*date=test / haxis=axis1  /* plot patterned polygons */
                      vaxis=axis3
                      nolegend
                      vzero;
/* Add FOOTNOTE to provide blank space at bottom of graph */
footnote1 h=18  '   ';
run;
quit;


/* Sort the SUMMARY data set */
proc sort data=summary;
by vendor;

/* Calculate average test duration and maximum pressure for each vendor */
proc means data=summary mean noprint;
var days maxpsi;
by vendor;
output out=vstats mean=avedays avemax;
run;

/* Prepare data for summary chart */
data vstats(keep=i avemax vendor tot) fmtdata(keep=fmtname start label type);
set vstats end=eof;
length psicolor $8 label $20;
/* Assign counter variable */
count=_n_;
/* Round the average number of days to the nearest 1/2 day */
tot=round(avedays,.5);

/* Create variables to use with CNTLIN= data set with PROC FORMAT */
fmtname='vfmt'; /* Identifies the format name */
type='c';       /* Format type will be Character */
start=vendor;   /* Variable to be formatted is VENDOR */
label=trim(vendor)||': AVE '||trim(left(put(tot,5.1)))||' DAYS'; /* Formatted value */
output fmtdata;

/* Assign color values and create macro variables */
if (avemax lt 200) then psicolor='red';
else if ( 200 le avemax lt 300) then psicolor='yellow';
else if (avemax ge 300) then psicolor='green';
call symput('col'||left(count),psicolor);

/* Output multiple observations based on value
of variable TOT */
do i = .5 to tot by .5;
   output vstats;
end;

if eof then do;
/* Create macro variable containing total number of vendors */
call symput('totv',count);
end;
run;

/* Use the CNTLIN= option with the FORMAT procedure to
create data-dependent formatted values for each VENDOR code */
proc format cntlin=fmtdata;
run;

/* Reset global graphics options before producing second chart */
goptions reset=global;

/* Define macro to specify solid bar patterns with data-dependent color */
%macro vpats;
  %do i=1 %to &totv;
    PATTERN&i V=SOLID C=&&col&i;
  %end;
%mend;

/* Invoke the VPATS macro */
%vpats;

/* Produce summary chart */
proc gchart data=vstats;
vbar i / discrete
         sumvar=avemax
         group=vendor
         space=0
         ref=200 300     /* draw reference lines */
         clipref         /* clip the reference lines behind the bars */
         nozeros
         maxis=axis1
         raxis=axis2
         gaxis=axis3
         patternid=group /* apply pattern by GROUP value */
         frame;
axis1 value=none label=none offset=(2,2);
axis2 label=(a=90 r=0 'Average Maximum Pressure (psi)') origin=(15,) offset=(0,0);
axis3 label=none value=(h=2) length=75;
format vendor $vfmt.;
run;
quit;

/* Specify graphics options before combining graphs */
goptions device=xcolor target=ps300 display vsize=8.75 in;

/* Define template and combine graphs together on one page */
proc greplay nofs igout=work.gseg tc=work.templt;
tdef v2
     1 / llx=0 lly=50 ulx=0 uly=100 urx=100 ury=100 lrx=100 lry=50
     2 / llx=0 lly=0  ulx=0 uly=50  urx=100 ury=50  lrx=100 lry=0;
template v2;
tplay 1:gplot 2:gchart;
run;
quit;