/* Associate the libref MAPS with your SAS map     */
   /* library.  For many sites this is automatically  */
   /* done when the SAS System is invoked.            */
libname maps '
              /* SAS maps library */
                                    ';              

   /* Set options for rotation, default fonts, and    */   
   /* sizing units.  Also set NODISPLAY so that map   */   
   /* and bar chart are not displayed while they are  */   
   /* being created. DISPLAY is turned back on when   */   
   /* the graphs are combined with PROC GREPLAY.      */   
goptions rotate=landscape ftext=zapfb gunit=pct nodisplay;

   /* A blank title is used with the map to reserve   */   
   /* space for the title to be used with the bar     */   
   /* chart that follows.                             */   
title1 h=6 ' ';                                           

   /* use a solid fill for the map.                   */   
pattern1 color=graycc value=ms;                           

   /* Create the US map with PROC GMAP using the      */   
   /* MAPS.US map data set as both the map and        */   
   /* response data set. Specifying LEVELS=1          */   
   /* causes the same pattern to be used for all      */   
   /* the states. The NOLEGEND option suppresses      */   
   /* the map legend.                                 */   
proc gmap data=maps.us map=maps.us;                       
  id state;                                               
  choro state / name='map'                                
                levels=1                                  
                nolegend;                                 
run; 

   /* Generate the data to be used for the bar chart. */       
data mileage;                                                 
  input age $ sex $ amount;                                   
  cards;                                                      
65-up m 7199                                                  
65-up f 4502                                                  
55-64 m 12304                                                 
55-64 f 5428                                                  
45-54 m 14701                                                 
45-54 f 9915                                                  
35-44 m 15916                                                 
35-44 f 12653                                                 
25-34 m 16683                                                 
25-34 f 15124                                                 
16-24 m 10718                                                 
16-24 f 6047                                                  
run;                                                          

   /* Create an Annotate data set (LABELS) to be      */        
   /* used to place the male and female symbols       */        
   /* and statistics on the bars. This is done by     */        
   /* setting the MILEAGE data set and outputting     */        
   /* two observations to the LABELS data set for     */        
   /* each observation in the MILEAGE data set. The   */        
   /* first observation output will generate the      */        
   /* symbol and the second will generate the         */        
   /* statistic for each bar.                                 
data labels;  
   length style color function text $ 8;              
   retain function 'label' color 'white' when 'a'     
          xsys ysys '2' hsys '3' size 4;              
   set mileage;                                       

      /* The vertical position of the annotated       */
      /* text is determined by using the MIDPOINT     */
      /* and GROUP variables.                         */
   midpoint=sex;                                      
   group=age;                                         

      /* Use the MARKER font to display the male      */
      /* and female symbols. Setting X=0 sets the     */
      /* horizontal position of the symbol at the     */
      /* beginning of the bar and the POSITION        */
      /* variable left aligns the symbols.            */
   style='marker';                                    
   X=0;                                               
   position='>';                                      

      /* Use different symbols for male and female.   */
   if sex='f'                                         
   then text='R';                                     
   else text='Q';                                     
   output;                                            
                                                      
      /* Reset the font from MARKER to the default    */
      /* font (ZAPFB). Setting X=AMOUNT places the    */
      /* statistic at the end of the bar and the      */
      /* POSITION variable right aligns the           */
      /* statistic. The TEXT variable contains        */
      /* the statistic value formatted with a         */
      /* COMMA9. format.                              */
   style=' ';                                         
   x=amount;                                          
   position='<';                                      
   text=left(put(amount, comma9. ));                  
   output;                                            

         /* Drop variables not needed for             */
         /* annotation.                               */
      drop age sex amount;                            
run;                                                  

   /* Specify the title and footnote for the bar      */
   /* chart. The MOVE= option positions the           */
   /* registered trademark symbol slightly above      */
   /* the text and returns to the original            */
   /* vertical position for the rest of the text.     */
title1 height=6 color=black                           
   'Average Mileage Driven Annually';                 
footnote height=2 justify=r color=black 'SAS/GRAPH'   
   move=(+0,+.5) '02'x move=(+0,-.5) ' Software ';    

   /* The AXIS1 statement is assigned to the group    */
   /* axis (GAXIS) and specifies the height, color,   */
   /* and justification of the the group (AGE)        */
   /* axis label. The AXIS2 statement is assigned     */
   /* to both the midpoint (MAXIS) and response       */
   /* (RAXIS) axes and is used to suppress the        */
   /* axes, tick marks, and tick labels.              */
axis1 label=(justify=r color=black height=4 'Age')    
      value=(height=4 color=black)                    
      style=0;                                        
axis2 label=none value=none                           
      major=none minor=none                           
      style=0;                                        

   /* Specify a separate bar pattern for each sex.    */
pattern1 value=solid color=gray44;                    
pattern2 value=solid color=gray66;                    

   /* Produce the bar chart with annotation.          */
   /* PATTERNID=MIDPOINT causes separate patterns     */
   /* to be used for each sex. NOSTATS suppresses     */
   /* statistics that are printed to the right        */
   /* by default.                                     */
proc gchart data=mileage;                             
   hbar sex / name='bars'                             
              discrete                                
              anno=labels                             
              sumvar=amount                           
              group=age                               
              gaxis=axis1                             
              raxis=axis2                             
              maxis=axis2                             
              width=3
              space=0                                 
              gspace=1                                
              nostats                                 
              patternid=midpoint                      
              coutline=black;                         
run;                                                  

   /* Set the DISPLAY option so that the final        */
   /* graph is displayed on the output device,        */
   /* then use PROC GREPLAY to overlay the            */
   /* two graphs.                                     */
goptions display;                                     
proc greplay nofs igout=gseg;                         

      /* Create a temporary catalog (TEMPCAT)         */
      /* to contain a template (WHOLE) that is        */
      /* a single panel covering the whole            */
      /* display area.                                */
   tc tempcat;                                        
   tdef whole 1 /;                                    

      /* Replay both graphs into the same template    */
      /* panel. Specify the MAP graph first so it     */
      /* is drawn underneath the bar chart.           */
   template whole;                                    
   tplay 1:map 1:bars;                                
quit;