Sample 69804: Side-by-side grouped bar chart and pie chart
This SAS Note provides sample code to use the Graph Template Language, GTL, and the SGRENDER procedure to create a graph that contains a bar chart and a pie chart side by side. This SAS program requires you to use SAS® 9.4M2 (TS1M2) or later.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This SAS program uses the Graph Template Language, GTL, and PROC SGRENDER to create a graph that contains a grouped bar chart and a pie chart side by side. This SAS program requires you to use SAS 9.4M2 or later.
/*--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 graphics / reset width=8in height=4.5in imagename='BarPie';
proc sgrender data=Sedans template=BarPie;
dynamic _tsize=16 _lsize=13;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.

This SAS Note provides sample code to use the Graph Template Language, GTL, and PROC SGRENDER to create a graph that contains a bar chart and a pie chart side by side. This SAS program requires you to use SAS® 9.4M2 (TS1M2) or later.
| Date Modified: | 2023-01-18 07:48:07 |
| Date Created: | 2023-01-12 13:31:21 |
Operating System and Release Information
| SAS System | Base SAS | Solaris for x64 | 9.4 TS1M2 | |
| Linux for x64 | 9.4 TS1M2 | |
| HP-UX IPF | 9.4 TS1M2 | |
| 64-bit Enabled Solaris | 9.4 TS1M2 | |
| 64-bit Enabled AIX | 9.4 TS1M2 | |
| Windows 7 Ultimate x64 | 9.4 TS1M2 | |
| Windows 7 Ultimate 32 bit | 9.4 TS1M2 | |
| Windows 7 Professional x64 | 9.4 TS1M2 | |
| Windows 7 Professional 32 bit | 9.4 TS1M2 | |
| Windows 7 Home Premium x64 | 9.4 TS1M2 | |
| Windows 7 Home Premium 32 bit | 9.4 TS1M2 | |
| Windows 7 Enterprise x64 | 9.4 TS1M2 | |
| Windows 7 Enterprise 32 bit | 9.4 TS1M2 | |
| Microsoft Windows Server 2012 Std | 9.4 TS1M2 | |
| Microsoft Windows Server 2012 R2 Std | 9.4 TS1M2 | |
| Microsoft Windows Server 2012 R2 Datacenter | 9.4 TS1M2 | |
| Microsoft Windows Server 2012 Datacenter | 9.4 TS1M2 | |
| Microsoft Windows Server 2008 for x64 | 9.4 TS1M2 | |
| Microsoft Windows Server 2008 R2 | 9.4 TS1M2 | |
| Microsoft Windows Server 2008 | 9.4 TS1M2 | |
| Microsoft Windows 8.1 Pro x64 | 9.4 TS1M2 | |
| Microsoft Windows 10 | 9.4 TS1M2 | |
| z/OS | 9.4 TS1M2 | |
| z/OS 64-bit | 9.4 TS1M2 | |
| Microsoft® Windows® for x64 | 9.4 TS1M2 | |
| Microsoft Windows 8 Enterprise 32-bit | 9.4 TS1M2 | |
| Microsoft Windows 8 Enterprise x64 | 9.4 TS1M2 | |
| Microsoft Windows 8 Pro 32-bit | 9.4 TS1M2 | |
| Microsoft Windows 8 Pro x64 | 9.4 TS1M2 | |
| Microsoft Windows 8.1 Enterprise 32-bit | 9.4 TS1M2 | |
| Microsoft Windows 8.1 Enterprise x64 | 9.4 TS1M2 | |
| Microsoft Windows 8.1 Pro 32-bit | 9.4 TS1M2 | |