Hi.
I need some help with SGplot. I would like to have a 100% stacked bar chart with a datalabel on top showing the value, not 100%.
I have my 100% stacked bar chart - but if I add datalabel, then I get the value "100%".
title "sashelp.Baseball";
proc sgplot data=sashelp.baseball noborder pctlevel=group;
styleattrs datacolors=(DarkBlue LightBlue bip ) ;
vbar league / response=crhits stat=percent nostatlabel
group=division missing seglabel /*datalabel*/ nooutline datalabelattrs=(size=10)
seglabelattrs=(color=white size=10 weight=bold);
xaxis display=(nolabel noline noticks);
yaxis display=(noline noticks) grid;
run;
The stacked bar chart with value - highlighted in yellow on the picture.
proc sgplot data=sashelp.baseball noborder ;
styleattrs datacolors=(DarkBlue LightBlue bip ) ;
vbar league / response=crhits stat=sum nostatlabel
group=division missing seglabel datalabel nooutline datalabelattrs=(size=10)
seglabelattrs=(color=white size=10 weight=bold);
xaxis display=(nolabel noline noticks);
yaxis display=(noline noticks) grid;
run;
How do I get the value to the 100% stacked bar chart?
My guess is some form of annotate, but I need some help.
Thanks.
Kind regards,
Steffen
Run the code below. If you want to bring the text down a little, add some OFFSETMAX to the YAXIS.
Hope this helps!
Dan
data anno;
retain x1space "datavalue" y1space "datapercent" y1 102 function "text" width 40;
input xc1 $ label $;
cards;
American 134889
National 105866
;
run;
title "sashelp.Baseball";
proc sgplot data=sashelp.baseball noborder pctlevel=group sganno=anno;
styleattrs datacolors=(DarkBlue LightBlue bip ) ;
vbar league / response=crhits stat=percent nostatlabel group=division
missing seglabel /*datalabel*/ nooutline datalabelattrs=(size=10)
seglabelattrs=(color=white size=10 weight=bold);
xaxis display=(nolabel noline noticks);
yaxis display=(noline noticks) grid;
run;
Run the code below. If you want to bring the text down a little, add some OFFSETMAX to the YAXIS.
Hope this helps!
Dan
data anno;
retain x1space "datavalue" y1space "datapercent" y1 102 function "text" width 40;
input xc1 $ label $;
cards;
American 134889
National 105866
;
run;
title "sashelp.Baseball";
proc sgplot data=sashelp.baseball noborder pctlevel=group sganno=anno;
styleattrs datacolors=(DarkBlue LightBlue bip ) ;
vbar league / response=crhits stat=percent nostatlabel group=division
missing seglabel /*datalabel*/ nooutline datalabelattrs=(size=10)
seglabelattrs=(color=white size=10 weight=bold);
xaxis display=(nolabel noline noticks);
yaxis display=(noline noticks) grid;
run;
Or use PROC MEANS or PROC FREQ to summarize the data and use VBARPARM and TEXT statements to display the bars and labels.
Thanks. It works just fine.
I changed the data step creating the anno data to be more datadriven, but I got the basic idear 😉
proc sql;
create table anno as
select
League as xc1
,put(sum(crhits), commax12.) as label
,"datavalue" as x1space
,"datapercent" as y1space
,102 as y1
,"text" as function
,40 as width
from sashelp.baseball
group by League
;
quit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.