BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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%?
6 REPLIES 6
GraphGuy
Meteorite | Level 14
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...
deleted_user
Not applicable
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.


/* -------------------------------------------------------------------
Code generated by SAS Task

Generated on: Friday, January 22, 2010 at 8:09:26 AM
By task: Bar Chart

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";
/* -------------------------------------------------------------------
End of task code.
------------------------------------------------------------------- */
RUN; QUIT;
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
TITLE; FOOTNOTE;
PATTERN1;
PATTERN2;
PATTERN3;
PATTERN4;
PATTERN5;
PATTERN6;
PATTERN7; Message was edited by: TONEY I
GraphGuy
Meteorite | Level 14
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;
deleted_user
Not applicable
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";
/* -------------------------------------------------------------------
End of task code.
------------------------------------------------------------------- */
RUN; QUIT;
%_eg_conditional_dropds(WORK.SORTTempTableSorted);
TITLE; FOOTNOTE;
PATTERN1;
PATTERN2;
PATTERN3;
PATTERN4;
PATTERN5;
PATTERN6;
PATTERN7;
GraphGuy
Meteorite | Level 14
Not sure which wizard you're using (EG?), so I'm not sure how to get it to re-run your gchart code to produce the chart (which should now contain the annotated %'s). Maybe provide some more details on which wizard you're using, and somebody can give you some tips on that part. [Sorry, but I'm not familiar with any of the wizard interfaces - just the graph code!]
deleted_user
Not applicable
Im not using the wizard now. Once the code is modified the wizard is no longer available. How do I modify the code to get the % in bar graph? The last code provided in this post is were im at. Message was edited by: TONEY I

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1385 views
  • 0 likes
  • 2 in conversation