sas >> SAS Graph: Getting sub-percents on a stacked bar graph

by pjmiller_57 » Mon, 22 Feb 2010 23:12:37 GMT

Hello Eveyone,
I am trying to create a stacked bar grpah with subpercents. The graph lists=
the number of chemotherapy cycles patients received by whether or not they=
have completed treatment. There are 32=A0patients, 16 of whom have complet=
ed treatment and 16 of whom have not.
My code is pasted below.=20
I would like to get the overall percents at the top of each bar (i.e., in t=
he outside =3D position) and the subpercents=A0within the segments of each =
bar (i.e., in the inside=3D position).=A0For example,=A0I have a total of 6=
patients who have undergone=A02 cycles of treatment. Four patients have co=
mpleted treatment and two patients are still on treatment. Thus, the overal=
l percent for this bar should be 18.75% (6/32) and the two inside percents =
should be 12.5% (2/16)=A0and 25.0% (4/16)=A0respectively.=20
Getting the outside percent to display correctly is no problem but=A0gettin=
g the inside percents is proving to be much more difficult.
Can anyone show me how to get the inside percents?
A couple of additional questions arose as I was trying to figure out how to=
make this graph.
The first concerns how to draw an appropriately sized box around the legend=
.=A0In my code, I can use /*cborder=3Dblack*/ to=A0draw a box. However, the=
box is far too wide and, in the end, I wound up using an annotate dataset =
to draw the box.=20
Is there a way to control the size of the box around the legend without res=
orting to the use of an annotate dataset?
Finally, I also had the idea of just drawing the percent values on the bars=
using something like:
note height =3D 0.75 j =3D left
move =3D (10, 15) "(12.50%)";
The problem is that SAS puts the text behinnd the bars instead of in front =
so that they cannot be seen. I looked but was unable to find a command that=
would enable me to bring the text to the foreground.
Can anyone show me how to do this?
Thanks foy your help.
**** Create vertical bar graph;
**** Define graphics options;
goptions reset =3D all;
goptions device =3D Cgmof97l gsfname =3D wordfile ftext=3D htext=3D1;
filename wordfile "N:\Studies\MBC 0606\Output\Cycles Received.cgm";
**** List Title;
title h =3D 2 "Number of Cycles Received by Patients (N=3D&pt)";
**** Define horizontal axis options;
axis1 label =3D (h =3D 1.5 "Cycles") /*order =3D (1 to 16 by 1)*/ minor =3D=
**** Define vertical axis options;
axis2 label =3D (h =3D 1.5 a =3D 90 "Frequency") order =3D (0 to 7 by 1) mi=
nor =3D none;
pattern1 color=3Dvlib; /* very-light-blue*/
pattern2 color=3Dpalg; /* pale-light-green*/
legend1 across=3D2 shape=3Dbar(2,1)
label=3D(h=3D1 "Completed Treatment" position=3D(top center))
/*cborder=3Dblack*/ position=3D(top inside right)
offset=3D(0,-3) /*mode=3Dprotect value=3D(h=3D0.90)*/ order=3D(" Yes" "No")=
data anote;=20
function =3D'move'; x=3D59; y=3D44; output;
function =3D'draw'; x=3D80; y=3D44; size=3D1;line=3D1;color=3D'black'; outp=
function =3D'draw'; x=3D80; y=3D38; size=3D1;line=3D1;color=3D'black'; outp=
function =3D'draw'; x=3D59; y=3D38; size=3D1;line=3D1;color=3D'black'; outp=
function =3D'draw'; x=3D59; y=3D44; size=3D1;line=3D1;color=3D'black'; outp=
proc gchart data=3Dcycles annotate=3Danote;
vbar cycles /
width =3D 6
subgroup =3D off_treatment
maxis =3D axis1
raxis =3D axis2
footnote1 "Note: Results are based on &pt subjects.";
footnote2 h=3D2;
note height =3D 0.75 j =3D left
move =3D (10, 15) "(12.50%)";
goptions reset =3D all;

=A0=0A=0A=0A _________________________________________________________=
_________=0ALooking for the perfect gift? Give the gift of Flickr! =0A=0Aht=

Similar Threads

1. Graph Template Language: Display Bar Stat At End Of Bar

When creating a bar chart, how can I display the bar statistic value at the=
 end of the bar?

I've tried the BARCHART statement's BARLABEL option, but it generates an er=
ror when running PROC TEMPLATE.


proc template;
  define statgraph b_test;
    begingraph / border=3Dfalse ;
      layout overlay / xaxisopts=3D(display=3Dnone) yaxisopts=3D(display=3D=
        barchart x=3Dvalue / name=3D"legend" orient=3Dvertical display=3Dst=
andard barlabel=3Dtrue barwidth=3D1
        fillattrs=3D(color=3DRED) OUTLINEATTRS=3D(color=3Dblack);

2. Graph Template Language: Display Bar Stat At End Of Bar

3. SAS-GRAPH bar color

Hi All,

Is it possible to color only one bar out of the other in a different
color?? I have all Asia pacific countries BAR, plus a AP Region
bar(computed). I wann to show the AP region Bar in a Diffrent color.

Also, I've arranged the BARS in ascending order. Is it possible to have
all countries bars ascending , and a final bar for the AP
region(irrespective of its value).

any help will be helpful!! :)


4. SAS/Graph changing width of bars dynamically based on number in

5. How to annotate bar chart with within subgroup % with SAS/GRAPH

Hi All,

I am trying to annotate a vertical bar chart with within subgroup
percentages. I have tried unsuccessfully using the INSIDE= & OUTSIDE=
options within VBAR statement to get SAS to automatically print the subgroup
% either inside the bars or outside. However I get an error message noting
that the bar width is insufficient. I have tried different spacing criteria
to no avail.

This is possibly because I am graphing 11 groups with two bars within each
group 9 thus a total of 22 bars on the x axis). The individual bars are
further subgrouped according to another binary variable, whose % I am trying
to obtain on the graph.

Another approach I tried was to use the SAS ANNOTATE facility to annotate
the subgroups. There is an example program on SAS website which shows how to
annotate subgroups of vertical bar charts. I tried to using this example to
fix my graph. However I can't get it to print the %. Instead it prints the
binary value of the subgroup variable in the sub-bar ( i.e 1 or 0). I am
trying to get SAS to print the percentage of 1's & 0's.

Any help is appreciated.

Thanks much,

Below you will find the code I am using -

To create the annotate dataset :

data* barlabel;

length color style $ *8*;

retain color 'red' when 'a' style 'swiss'

xsys ysys '2' position 'E' size *3* hsys '3';

set chart;


group = VISIT;

subgroup=SCORE;   /* SCORE is abinary variable 0/1 */;

text=left(put(subgroup,*5.*));   -----> This just puts the value      of the
corresponding subgroup 0/1 in the subgroup bars ( this probably needs some
tweaking inorder to get SAS to produce percentages ??? )


proc* *gchart* data=chart;

vbar TRT /


subgroup = SCORE

group = VISIT /* specify group variable */

width = *3* /* specify bar width */

gspace = *2* /* specify space between groups */

type= pct /* specify type of statistic */

g100 /* specify within group pct calculation*/

maxis = axis1 /* assign midpoint axis options */

gaxis = axis2 /* assign group axis options */

raxis = axis3 /* assigns response axis options*/

legend = legend1

annotate = barlabel



6. SAS/Graph changing width of bars dynamically based on number

7. Error bars in SAS Graph (or elsewhere)?

Hi all,

  What I want to do is pretty simple, but I don't know off hand how to do it
in SAS or if it can be done.  I am plotting some data to go along with a
simple one way ANOVA.  The dep variable is on the Y axis and the treatment
groups (group 1, group2,.., group 8) are on the x axis.  What I want to do
is to plot the means of each group with error bars above and below.  On the
error bars I want to put the upper and lower limits of the confidence
interval for each mean.  Is there any way to do this?

  Thanks so much for your help!

  Keith Dunnigan
  Sr. Biostatistician
  Mallinckrodt Medical

8. sas gchart help for a vertical bar graph