/**********************************************************************/
/**********************************************************************/

goptions dev=ps hby=0;

   /* Delete entries from the catalog WORK.GSEG.                      */

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

   /* Specify macro variables containing the number of                */
   /* template panels across and down, and then calculate             */
   /* the total number of panels.                                     */

%let across=4;
%let down=3;
%let npanels=%eval(&down*&across);

/**********************************************************************/

data garea;
   rc=ginit();                            /* Initialize DSGI.         */
   call gask('hsize',hsize,rc);           /* Get default HSIZE        */
                                          /* for driver.              */
   hsze=hsize/&across;                    /* Calculate horizontal     */
                                          /* panel size.              */
   call symput('hsize',left(trim(hsze))); /* Create HSIZE macro       */
                                          /* variable.                */
   call gask('vsize',vsize,rc);           /* Get default VSIZE for    */
                                          /* driver.                  */
   vsze=vsize/&down;                      /* Calculate vertical       */
                                          /* panel size.              */
   call symput('vsize',left(trim(vsze))); /* Create VSIZE macro       */
                                          /* variable.                */
   rc=gterm();                            /* Terminate DSGI.          */
run;

/**********************************************************************/

data one;
   do year=1980 to 1991;
      do quarter=1 to 4;
         sales=10+(10*uniform(5555));
         output;
      end;
   end;
run;

/**********************************************************************/

goptions nodisplay hsize=&hsize vsize=&vsize;

proc gchart data=one;
   vbar quarter /discrete sumvar=sales raxis=axis1;
   by year;
   pattern1 v=solid c=black;
   title "#byval(year)";
   axis1 order=(0 to 20 by 5) label=('SALES') minor=none;
run;
quit;

/**********************************************************************/

data numgraf;
   rc=gset('catalog','work','gseg');       /* Open WORK.GSEG catalog. */
   rc=ginit();
   call gask('numgraph',grsegcnt,rc);      /* Get # of graphs in      */
                                           /* catalog.                */
   if grsegcnt<&npanels then num=grsegcnt; /* If more graphs than     */
      else num=&npanels;                   /* panels, replay only as  */ 
                                           /* many graphs as there    */
                                           /* are panels.             */
   call symput('numgrph',left(trim(num))); /* # of graphs to replay.  */
   ymult=100/&down;                        /* Y increment for panels. */
   xmult=100/&across;                      /* X increment for panels. */
   rc=gterm();                             /* Terminate DSGI.         */
run;

/**********************************************************************/

data coord;
   set numgraf;

   do x=0 to (100-xmult) by xmult;    /* Calculate X coordinate       */
                                      /* values.                      */
      llx=x;                          /* Lower-left X.                */
      ulx=x;                          /* Upper-left X.                */
      urx=x+xmult;                    /* Upper-right X.               */
      lrx=x+xmult;                    /* Lower-right X.               */   
      do y=0 to (100-ymult) by ymult; /* Calculate Y coordinate       */
                                      /* values.                      */
         lly=y;                       /* Lower-left Y.                */
         uly=y+ymult;                 /* Upper-left Y.                */
         ury=y+ymult;                 /* Upper-right Y.               */
         lry=y;                       /* Lower-right Y.               */
         output;
      end;
   end;
run;

proc sort data=coord;
   by descending y;
run;

/**********************************************************************/

data mccoord;
   set coord end=eof;
   call symput('llx'||left(_n_),llx);
   call symput('ulx'||left(_n_),ulx);
   call symput('urx'||left(_n_),urx);
   call symput('lrx'||left(_n_),lrx);
   call symput('lly'||left(_n_),lly);
   call symput('uly'||left(_n_),uly);
   call symput('ury'||left(_n_),ury);
   call symput('lry'||left(_n_),lry);

   if eof then call symput('total',_n_);
run;

/**********************************************************************/

%macro tempdef;
   %do i=1 %to &total;
   &i  / llx=&&llx&i     lly=&&lly&i
         ulx=&&ulx&i     uly=&&uly&i
         urx=&&urx&i     ury=&&ury&i
         lrx=&&lrx&i     lry=&&lry&i
   %end;
%mend tempdef;

/**********************************************************************/

%macro tplay;
   %do i=1 %to &numgrph;
   &i:&i
   %end;
%mend tplay;

/**********************************************************************/

goptions reset=goptions target=ps;

/**********************************************************************/

proc greplay nofs igout=work.gseg tc=work.templt;
   tdef spec&npanels
   %tempdef;              /* Invoke macro TEMPDEF to create template. */
   template spec&npanels;
   treplay            
   %tplay;                /* Invoke macro TPLAY to replay graphs.     */
run;
quit;

/**********************************************************************/
/**********************************************************************/