The sample code on the Full Code tab illustrates how to reorder the subgroups in each bar using a RANK variable which is based on the total sales for each product. The default order of the subgroups is alphabetical based on the values of the SUBGROUP= variable, ordered from bottom to top of each bar. Each subgroup is displayed only once in the legend.
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.
The main portion of this program reorders the subgroups in each bar using a RANK variable which is based on the total sales for each product. This method by default creates a legend entry for each subgroup in each bar. However, the example demonstrates how to display each subgroup only once in the legend.
The graphics output on the Results tab was produced using SAS® 9.2. Submitting the sample code with releases of SAS prior to SAS 9.2 might produce different results.
/* Set the graphics environment */
goptions reset=all cback=white border htitle=12pt htext=10pt;
/* Calculate totals for region and product */
proc means data=sashelp.shoes noprint;
by region;
class product;
var sales;
output out=shoes1(where=(_type_=1)) sum=;
run;
/* Within region (the midpoint) make highest sales come first */
proc sort data=shoes1;
by region descending sales product;
run;
/* The code below generates the default results. */
title1 'Default Graph of Sales Totals';
legend1 label=('Product Category') ;
pattern1 color=CX66A5A0 value=l3;
pattern2 color=CX7C95CA value=solid;
pattern3 color=CX94BDE1 value=solid;
pattern4 color=CXDE7E6F value=solid;
pattern5 color=CXA9865B value=r3;
pattern6 color=CXBABC5C value=solid;
pattern7 color=CXB689CD value=solid;
pattern8 color=CXCD7BA1 value=solid;
axis1 label=none split='/' value=(height=1);
axis2 minor=(number=3) label=(angle=90 height=1 "Total Sales (in millions)")
value=(height=1 "0" "$1" "$2" "$3" "$4" "$5" "$6");
proc gchart data=shoes1;
vbar region / sumvar=sales subgroup=product legend=legend1
maxis=axis1 raxis=axis2 space=.5;
run;
quit;
/* The code below reorders the subgroups. */
goptions reset=all cback=white border htitle=12pt htext=10pt;
/* Create a RANK variable. This will be used as the SUMVAR= */
/* variable to ensure that the product with the highest sales */
/* is drawn at the bottom of each bar. */
/* Assign a color and pattern value for each product. */
/* Assign color and pattern values to a macro variable. Each */
/* observation creates one macro variable. This ensures that */
/* each product has the same color and pattern across midpoints */
/* even though the RANK value is different for each product. */
data shoes2;
set shoes1 end=eof;
by region descending sales product;
length color $20 value $5;
region = tranwrd(strip(region)," ","/");
rank + 1;
select;
when (product="Boot") do;
color="CX66A5A0";
value="L3";
end;
when (product="Sandal") do;
color="CXDE7E6F";
value="solid";
end;
when (product="Sport Shoe") do;
color="CXBABC5C";
value="solid";
end;
when (product="Slipper") do;
color="CXA9865B";
value="R3";
end;
when (product="Men's Dress") do;
color="CX94BDE1";
value="solid";
end;
when (product="Men's Casual") do;
color="CX7C95CA";
value="solid";
end;
when (product="Women's Dress") do;
color="CXCD7BA1";
value="solid";
end;
when (product="Women's Casual") do;
color="CXB689CD";
value="solid";
end;
otherwise;
end;
if eof then call symput('tot',trim(left(put(_n_,8.))));
call symput('pattern'||trim(left(put(_n_,8.))),'color=' || color || ' value=' || value ||';');
run;
/* Define and execute the macro to create the PATTERN statements */
%macro pattern;
%do j=1 %to &tot;
pattern&j &&pattern&j;
%end;
%mend pattern;
%pattern;
/* Keep one observation for each product */
proc sort data=shoes2 out=unqshoes nodupkey;
by product;
run;
/* Create macro variables to use in the legend. */
/* ORD contains the list of ranks that will be */
/* used in the ORDER= option. */
/* VAL contains the list of product names that */
/* will be used in the VALUE= option. */
data _null_;
set unqshoes end=eof;
length ord val $200;
retain ord val ' ';
ord = trim(left(ord)) || ' ' || trim(left(put(rank,8.)));
val = trim(left(val)) || ' "' || product || '"';
if eof then do;
call symput('ord', ord);
call symput('val', val);
end;
run;
title1 "Reorder Subgroups Based on Sales Totals";
legend1 label=('Product Category') order=(&ord) value=(j=l &val);
axis1 label=none split='/' value=(height=1);
axis2 minor=(number=3) label=(angle=90 height=1 "Total Sales (in millions)")
value=(height=1 "0" "$1" "$2" "$3" "$4" "$5" "$6");
proc gchart data=shoes2;
vbar region / sumvar=sales subgroup=rank legend=legend1
maxis=axis1 raxis=axis2 space=.5;
run;
quit;
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.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> GCHART Query and Reporting ==> Creating Reports ==> Graphical ==> Graph Types ==> Charts ==> Bar |
Date Modified: | 2010-11-17 11:48:22 |
Date Created: | 2010-11-15 14:07:19 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | SAS/GRAPH | z/OS | 9.1 TS1M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M0 | |||
Microsoft Windows 2000 Advanced Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Professional | 9.1 TS1M0 | |||
Microsoft Windows NT Workstation | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M0 | |||
Microsoft Windows XP Professional | 9.1 TS1M0 | |||
64-bit Enabled AIX | 9.1 TS1M0 | |||
64-bit Enabled HP-UX | 9.1 TS1M0 | |||
64-bit Enabled Solaris | 9.1 TS1M0 | |||
HP-UX IPF | 9.1 TS1M0 | |||
Linux | 9.1 TS1M0 | |||
OpenVMS Alpha | 9.1 TS1M0 | |||
Tru64 UNIX | 9.1 TS1M0 |