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


libname sampfile '
                  /* your sas.data.library */
                                             ';
data _null_;
   set sampfile.state end=end;
   by state;
   if first.state then
      do;
         count+1;

            /* create variables for the file name and title   */
         call symput('ST_'||left(put(count,5.)),state);
         call symput('LN_'||left(put(count,5.)), 
              trim(stnamel(state)));
      end;

      /* create a variable that contains the number of states */
   if end then call symput('count',put(count,5.));
run;

goptions dev=cgm         /* device driver for CGM             */
         gsfname=grafout /* file reference to output file     */
         gsfmode=replace /* replace file if it exists         */
         gsflen=80;      /* set record length to 80           */

symbol1 value=none interpol=join color=black;

%macro outfile;
   %do i=1 %to &count;

         /* create a file reference                           */
      filename grafout "&&ST_&I...CGM";
      proc gplot data=sampfile.state;
           plot ave_tsp*month;

              /* subset the data to the value of ST_n         */
           where state="&&ST_&I";
           title1 height=1 "Average TSP for &&LN_&I";
      run;
      quit;
   %end;
%mend;

%outfile


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


filename grafout "userid.&&ST_&I...CGM" disp=new 
         space=(trk,(5,1)) recfm=vb;


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


libname sampfile '
                  /* your sas.data.library */
                                             ';
data _null_;
   set sampfile.state end=end;
   by state;
   if first.state then
      do;
         count+1;
         call symput('ST_'||left(put(count,5.)),state);
      end;
   if end then call symput('count',put(count,5.));
run;

options nobyline;

goptions dev=cgm nodisplay
         cpattern=black;

   /* delete all graphs in the catalog                        */
proc greplay igout=graphs nofs;
     delete _all_;
run;
quit;

   /* create the graphs and store them in a catalog           */
proc gchart data=sampfile.state gout=graphs;
   by state;
   vbar month / sumvar=ave_tsp discrete;
   title1 height=1 "Average TSP for #byval(state)";
run;
quit;

%macro outfile;
   %do i=1 %to &count;

         /* create a file reference to the output file        */
      filename grfout&i "&&ST_&i...CGM";

         /* name the file as the destination for the graphics */
         /* stream                                            */
      goptions gsfname=grfout&i;

         /* replay a graph in the catalog to the output file  */
      replay &i;
   %end;
%mend;

goptions

      /* replace file if it exists                            */
   gsfmode=replace

      /* set record length to 80                              */
   gsflen=80;

proc greplay igout=graphs nofs;
   %outfile
quit;


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