By specifiying "datalabel=percent in the histogram statment of proc sglot/sgpanel, you can get a percentage above each bar. Useful as far as it goes, but the percentages use the best6. format. Is there a way to specify e.g. 8.1 as the format for these percentages?
Here's some sample code:
proc sgplot data=sashelp.class;
histogram age / datalabel=percent;
run;
Specifying a format for age, percent, or datalabel has no impact. The percentages displayed have values like 42.105 or 5.2632. Is there a way to change this in proc sgplot? I've seen examples on how to do this using proc gplot but that's not what I'm interested in. I'm using SAS 9.4M1/
I assume "percent" is a variable you have created in your dataset yes? If so apply the formatting you want in the dataset then it should carry through to the graph. If not then just round(percent,0.01) in your data.
No, "percent" and "datalabel" were guesses at some internal variable in proc sgplot. The only variable I'm using from the sashelp.class dataset in this example is age. Assigning a format to age has no impact.
I can't find anything about a percent variable being created, doesn't mean it doesnt exist though. Anyways from the documentation:
If you leave SAS to decide it will assign best6. so as with anything, do it yourself to make sure:
Have a datatstep before teh sgplot:
data want set sashelp.class; pc=(val / div) * 100; /* or use round */ format pc percent5.2; run; proc sgplot data=want; histogram age / datalabel=pc; run;
@RW9, the OP is asking about the DATALABEL=PERCENT option in the HISTOGRAM statement. PERCENT is a keyword, not a variable. (Yes, this is different than for plots with markers, such as the SCATTER stmt.)
OP: I think the answer is that you cannot control the format of the bar labels from PROC SGPLOT, but perhaps @DanH_sas will prove me wrong.
Rick is correct. To control the format of the labels on histogram bar, you would need to precompute your histogram data (setting your format on the column) and render it using the HISTOGRAMPARM statement in GTL.
Rick is correct. To control the format of the labels on histogram bar, you would need to precompute your histogram data (setting your format on the column) and render it using the HISTOGRAMPARM statement in GTL.
No, this won't work. The counts and percentages are created by proc sglot and depend on the number of bins. You can specify the nbins option or let SAS determine the number of bins, as was done in my simple example. The counts and percentages aren't available in your input data.
Sorry, I can't find anything on this anywhere (typical trying to search for graph items, can never find anything in the docs) other thn the default is best6. You can try looking through this blog:
http://blogs.sas.com/content/graphicallyspeaking/
It has plenty of examples of pretty much all graphs, but again I couldn't find this option used there either.
Fo rmy graphs I always get the data up front in a dataset then graph from that data rather than using inbuilt functions as its far simpler then to alter data/sort, group etc exactly as I want.
Using this code you can get the Y axis to display in percent, I do not know of a way to display the label at the top of the bar.
proc sgplot data=sashelp.class;
histogram age / scale=proportion datalabel=percent;
yaxis display=(nolabel) valuesformat=percent9.2;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.