FOCUS AREAS

Graphics

Market Share for Sedans
Base SAS: Graph Template Language

%let graphs='.';

%let dpi=100;
%let w=8in;
%let h=4.5in;

title;
footnote;
ods html close;

/*--Macro by Perry Watts--*/
%macro RGBHex(rr,gg,bb);
  %sysfunc(compress(CX%sysfunc(putn(&rr,hex2.))
  %sysfunc(putn(&gg,hex2.))
  %sysfunc(putn(&bb,hex2.))))
%mend RGBHex;

/*--Mark all non-Sedans as "Other"--*/
data Sedans;
  set sashelp.cars(keep=origin type mpg_city mpg_highway horsepower);
  if type ne 'Sedan' then type='Other';
  run;
/*proc print;run;*/

/*--Define Bar Pie template--*/
proc template;
  define statgraph BarPie;
    dynamic _tsize _lsize;
    begingraph;
      entrytitle "Market Share for Sedans" / textattrs=(size=_tsize);

      /*--Define an attribute map to use specific colors by type--*/
      discreteattrmap name='origin' / ignorecase=true;
        value 'Sedan' / fillattrs=(color=%rgbhex(100, 150, 40));
        value 'Other' / fillattrs=(color=%rgbhex(180, 110, 50));
      enddiscreteattrmap;

       /*--Associate the Attribute Map to the TYPE variable--*/
      discreteattrvar attrvar=type var=type attrmap='origin';

      /*--Define a one row x two columns layout --*/
      layout lattice / columns=2 columnweights=(0.6 0.4) columngutter=20;

        /*--First cell has a Bar Chart--*/
        layout overlay / xaxisopts=(display=(tickvalues)) walldisplay=none
                         yaxisopts=(display=(tickvalues) linearopts=(tickvalueformat=percent.)
                         griddisplay=on offsetmax=0.2);
          barchart category=origin / name='a' barlabel=true stat=proportion
                   dataskin=pressed group=type groupdisplay=cluster barlabelformat=percent6.1
                   baselineattrs=(thickness=0) barlabelattrs=(size=_lsize) grouporder=descending
                   /*--FILLTYPE= requires SAS 9.4 (TS1M2) or later--*/
                   filltype=gradient
          ;
          discretelegend 'a' / location=inside halign=center valign=top autoitemsize=true valueattrs=(size=_lsize);
        endlayout;

        /*--Second cell has a Pie Chart--*/
        layout region / pad=(bottom=30);
          piechart category=type /  dataskin=sheen centerfirstslice=true start=270 stat=pct
                   datalabelattrs=(size=_lsize);;
        endlayout;
      endlayout;
    endgraph;
  end;
run;

/*--Render the graph--*/
ods listing style=listing gpath=&graphs image_dpi=&dpi;
ods graphics / reset width=&w height=&h imagename='BarPie';
proc sgrender data=Sedans template=BarPie;
  dynamic _tsize=16 _lsize=13;
run;