* Richard A. DeVenezia 1/25/2001;
* gchart stacked bar example;
* create data for 10 bygroups;
* in each by group there will between 10 and 25 observations;
* the midpoint will be a random value between A and Z;
data fake;
do bygroup = 1 to 12;
range = int (10 * ranuni(7));
start = int (16 * ranuni(0));
N = 10 + int (15 * ranuni (0));
do j = 1 to N;
* midpoint = start + int (range * ranuni(0));
midpoint = byte (65 + start + int (range * ranuni(0)));
output;
end;
end;
keep bygroup midpoint;
label bygroup = 'By Group';
label midpoint = 'Midpoint';
run;
* calculate percentages used in vbar freq= option;
proc freq noprint data=fake ;
by bygroup;
table midpoint / out=bygrppct;
run;
proc sql;
* enumerate all midpoint values in all by groups,
* use a zero percentage if midpoint not present in data;
* since each by group has all midpoints, the patterns applied by
* gchart to the midpoint bars will be consistent across all by groups;
* 0 Percent midpoints will not be drawn;
create table vbar as
select
all.*
, some.count
, coalesce (some.percent,0) as Percent
, count(distinct some.midpoint) as nMidpt
FROM
( select * from
(select unique bygroup from bygrppct)
FULL OUTER JOIN
(select unique midpoint from bygrppct)
on 1
) all
LEFT JOIN
bygrppct some
ON
all.bygroup = some.bygroup
and all.midpoint = some.midpoint
GROUP BY
all.bygroup
ORDER BY
nMidpt desc, all.bygroup, all.midpoint
;
quit;
title;
footnote;
ods html path='c:\temp\report' body='vbar.html';
goptions
goutmode=replace
device=win
targetdevice=gif160
xpixels=160
ypixels=120
htext=5pct
ftext=simplex
hby=0pct
;
/*
Title1 j=l h=6pct f=simplex "Subgroup patterns are the same";
Title2 j=l h=6pct f=simplex "at same midpoint in each by group";
Title4 h=6pct f=simplex "By Group=#byval1";
footnote h=5pct "By groups ordered by number of midpoints";
*/
Title1 h=6pct f=simplex "By Group=#byval1";
axis1 label=none minor=none;
proc gchart data=vbar;
by bygroup notsorted;
vbar midpoint
/ freq = percent
descending
patternid = subgroup /* sugroup is the default, but stating it clears things up */
subgroup = midpoint /* note, same as vbar variable */
nolegend
raxis = axis1
maxis = axis1
space = 3
noframe
name = "bygrp001"
;
run;
quit;
ods html close;
goptions reset=(all);
%let pageTitle = SAS Sample code - maintaining midpoint patterns across bygroups;
%let border = 1;
%let tableTitle = Midpoint patterns are

maintained across all by groups;
%let tableFooter = By groups ordered by number of midpoints;
data _null_;
file "C:\temp\report\vbar.html";
put
""
/ ""
/ "&pageTitle"
/ ''
/ ""
/ ""
/ ""
/ ''
'"
;
do i = 1 to 12;
if mod (i,3) = 1 then put "";
put "";
if mod (i,3) = 0 then put "";
end;
put
''
'"
;
put "'
''
"**&tableTitle**"
""
" |

";
put '';
put " |

'
''
"**&tableFooter**"
""
" |

";
run;