Hello,
I'm using sgplot to plots bar graphs. I would like to add the number associated with each bar to be printed on each bar. When I use vbar statement, by the following options I can do so:
datalabel datalabelfitpolicy=rotate/none
But when I use hbar statement the datalabel option doesn't work with either of datalabelfitpolicy=none or datalabelfitpolicy=rotate or without datalabelfitpolicy.
I will be thankful if anyone knows how I can activate datalabel option when using hbar.
Thanks 🙂
It should work the way you are describing it. Label rotation is not helpful for HBAR. Please include full code and data to debug.
What release of SAS are you using?
I just realixed that when I have only two bars, it works, but in my actual example, I have about 100 bars (that's why I want to use hbar instead of vbar) and it doesn't show the labels.
I don't think the code is useful, but here you are:
The chapter variable has about 50 categories, and case variable has two categories. So when I run the code, I get about 100 bars:
options nodate nonumber;
ods pdf style=barcolors file = "R:\working\finished datasets\Chapters\admin\Specialty.pdf";
goptions device = pdf colors=(black) rotate=landscape;
ods graphics / height = 800 width = 700;
title font="helvetica/bold" height=10pt "Overall weighted Frequency of Physician Specialty";
title2 font="helvetica/italic" height=8pt "Stratified by Cases & Controls";
proc sgplot data = Overall_Freq2_Specialty;
vbar Chapter/group = case response = WgtFreq groupdisplay=cluster datalabel;
xaxis label = "Physician Specialty" ;
yaxis label = "Frequency" grid;
keylegend / position=right location=outside;
where chapter ~= "General Practice";
run;title;title2;
ods pdf close;
Helps to have the code that is NOT working (HBAR), WITH THE DATA. Also SAS version.
Sorry, I copied the other one. Just change "vbar" to "hbar". It doesn't work. Also, data is stored in a secure environment and I can't take it out. But the data layput is similar to this, but 50 chapters not 5:
chapter case WgtFreq
1 1 5
1 0 10
2 1 2
2 0 6
3 1 5
3 0 9
4 1 10
4 0 15
5 1 20
5 0 15
......
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.