N/A
Posts: 0

# Bar Graph

In a stacked bar graph how do I display the percentages of each piece of the bar that makes up the 100%? Can I have the percentage display inside each piece that will add to 100%?
SAS Employee
Posts: 990

## Re: Bar Graph

If your response values really are percentages, then you can use "inside=sum". Here is an example:

data foo;
format value percent7.0;
name='Bill';
value=.20; output;
value=.50; output;
value=.30; output;
run;

axis1 label=none;
proc gchart data=foo;
vbar name / type=sum sumvar=value subgroup=value inside=sum
raxis=axis1 width=12;
run;

If your bar values are something other than percentages, and you want to print the percentages in the bar chart segments, you could calculate the percentages in another variable, and then annotate that value into the bar segments.

Those are the first 2 techniques that come to mind - there might be others...
N/A
Posts: 0

## Re: Bar Graph

This is the code using the bar chart in enterprise. I need to modify this code to show the percent in each block. The x axes is year the y axes is dollar value.

/* -------------------------------------------------------------------

Generated on: Friday, January 22, 2010 at 8:09:26 AM

Input Data: WORK.COA_BOTKI_DATA
Server: SASApp
------------------------------------------------------------------- */

%_eg_conditional_dropds(WORK.SORTTempTableSorted);
/* -------------------------------------------------------------------
Sort data set WORK.COA_BOTKI_DATA
------------------------------------------------------------------- */

PROC SQL;
CREATE VIEW WORK.SORTTempTableSorted AS
SELECT T.F1, T.F3, T.F2
FROM WORK.COA_BOTKI_DATA as T
;
QUIT;
PATTERN1 COLOR=MAROON;
PATTERN2 COLOR=CXFF6600;
PATTERN3 COLOR=OLIVE;
PATTERN4 COLOR=CXFFFFCC;
PATTERN5 COLOR=BLUE;
PATTERN6 COLOR=GRAY;
PATTERN7 COLOR=RED;
PATTERN8 COLOR = _STYLE_;
PATTERN9 COLOR = _STYLE_;
PATTERN10 COLOR = _STYLE_;
PATTERN11 COLOR = _STYLE_;
PATTERN12 COLOR = _STYLE_;
Legend1
FRAME
LABEL=(FONT='Microsoft Sans Serif' HEIGHT=8pt JUSTIFY=LEFT )
;
Axis1
STYLE=1
WIDTH=1
MINOR=NONE

;
Axis2
STYLE=1
WIDTH=1

;
TITLE;
TITLE1 "Support and Revenue";
FOOTNOTE;
PROC GCHART DATA=WORK.SORTTempTableSorted
;
VBAR3D
F1
/
SUMVAR=F2
SUBGROUP=F3
SHAPE=HEXAGON
FRAME TYPE=SUM
NOZERO
LEGEND=LEGEND1
COUTLINE=BLACK
RAXIS=AXIS1
MAXIS=AXIS2
;
LABEL F1="Year"
F3="Name"
F2="Dollar";
/* -------------------------------------------------------------------
------------------------------------------------------------------- */
RUN; QUIT;
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
TITLE; FOOTNOTE;
PATTERN1;
PATTERN2;
PATTERN3;
PATTERN4;
PATTERN5;
PATTERN6;
PATTERN7; Message was edited by: TONEY I
SAS Employee
Posts: 990

## Re: Bar Graph

Hmm ... it's a little tricky to try to add this to code without some data, so I've retro-fitted your code to use sashelp.revhub2 (sample data) - should be simple for you to switch back to your own data, since I kept all the names the same!

My code/changes are in lower-case, so they should be easy to find. Basically, I create a temporary data set in which I sum up the data so I can calculate the % (right now I'm calculating each bar segment as the % of that bar ... to calculate as the % of all the bars instead, just remove the "group by f1"). I make this temporary data set into an annotate data set, and then specify it in the anno= in the gchart ...

-------------------------------------------------------------------------------------------------

PROC SQL;
CREATE VIEW WORK.SORTTempTableSorted AS
SELECT unique T.hub as F1, T.source as F3, sum(T.revenue) as F2
FROM sashelp.revhub2 as T
group by hub, source
;
create table my_anno as
select unique *, sum(f2) as sum
from WORK.SORTTempTableSorted
group by f1; /* remove the 'group by f1' to calculate % of whole chart */
QUIT;

data my_anno; set my_anno;
when='a'; xsys='2'; ysys='2'; hsys='3';
function='label'; style='"arial/bold"'; size=2; position='5';
xc=f1; subgroup=f3;
text=trim(left(put(f2/sum,percent7.0)));
run;

PATTERN1 COLOR=MAROON;
PATTERN2 COLOR=CXFF6600;
PATTERN3 COLOR=OLIVE;
PATTERN4 COLOR=CXFFFFCC;
PATTERN5 COLOR=BLUE;
PATTERN6 COLOR=GRAY;
PATTERN7 COLOR=RED;
PATTERN8 COLOR = _STYLE_;
PATTERN9 COLOR = _STYLE_;
PATTERN10 COLOR = _STYLE_;
PATTERN11 COLOR = _STYLE_;
PATTERN12 COLOR = _STYLE_;
Legend1
FRAME
LABEL=(FONT='Microsoft Sans Serif' HEIGHT=8pt JUSTIFY=LEFT )
;
Axis1
STYLE=1
WIDTH=1
MINOR=NONE

;
Axis2
STYLE=1
WIDTH=1

;
TITLE;
TITLE1 "Support and Revenue";
FOOTNOTE;
PROC GCHART DATA=WORK.SORTTempTableSorted anno=my_anno;
VBAR3D
F1
/
SUMVAR=F2
SUBGROUP=F3
SHAPE=HEXAGON
FRAME TYPE=SUM
NOZERO
LEGEND=LEGEND1
COUTLINE=BLACK
RAXIS=AXIS1
MAXIS=AXIS2
;
LABEL F1="Year"
F3="Name"
F2="Dollar";
run;
N/A
Posts: 0

## Re: Bar Graph

Ok. I've copied code to a seperate file to edit. In the output data the percentages are now calculated. How do I get those percentages to display in each section of the bar graph to show 100%? The wizard is no longer available and I know just enough about programing to get me in trouble. This is what me edited code looks like now.

PROC SQL;
CREATE VIEW WORK.SORTTempTableSorted AS
SELECT T.F1, T.F3, T.F2
FROM WORK.COA_BOTKI_DATA as T
;
create table my_anno as
select unique *, sum(f2) as sum
from WORK.SORTTempTableSorted
group by f1; /* remove the 'group by f1' to calculate % of whole chart */
QUIT;

data my_anno; set my_anno;
when='a'; xsys='2'; ysys='2'; hsys='3';
function='label'; style='"arial/bold"'; size=2; position='5';
xc=f1; subgroup=f3;
text=trim(left(put(f2/sum,percent7.0)));
run;

QUIT;
PATTERN1 COLOR=MAROON;
PATTERN2 COLOR=CXFF6600;
PATTERN3 COLOR=OLIVE;
PATTERN4 COLOR=CXFFFFCC;
PATTERN5 COLOR=BLUE;
PATTERN6 COLOR=GRAY;
PATTERN7 COLOR=RED;
PATTERN8 COLOR = _STYLE_;
PATTERN9 COLOR = _STYLE_;
PATTERN10 COLOR = _STYLE_;
PATTERN11 COLOR = _STYLE_;
PATTERN12 COLOR = _STYLE_;
Legend1
FRAME
LABEL=(FONT='Microsoft Sans Serif' HEIGHT=8pt JUSTIFY=LEFT )
;
Axis1
STYLE=1
WIDTH=1
MINOR=NONE

;
Axis2
STYLE=1
WIDTH=1

;
TITLE;
TITLE1 "Support and Revenue";
FOOTNOTE;
PROC GCHART DATA=WORK.SORTTempTableSorted anno=my_anno
;
VBAR3D
F1
/
SUMVAR=F2
SUBGROUP=F3
SHAPE=HEXAGON
FRAME TYPE=SUM
NOZERO
LEGEND=LEGEND1
COUTLINE=BLACK
RAXIS=AXIS1
MAXIS=AXIS2
;
LABEL F1="Year"
F3="Name"
F2="Dollar";
/* -------------------------------------------------------------------
------------------------------------------------------------------- */
RUN; QUIT;
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
TITLE; FOOTNOTE;
PATTERN1;
PATTERN2;
PATTERN3;
PATTERN4;
PATTERN5;
PATTERN6;
PATTERN7;
SAS Employee
Posts: 990