Data visualization with SAS programming

Format percentages in a histogram using sgplot

Reply
Contributor ckx
Contributor
Posts: 54

Format percentages in a histogram using sgplot

[ Edited ]

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/

Esteemed Advisor
Esteemed Advisor
Posts: 7,190

Re: Format percentages in a histogram using sgplot

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.

Contributor ckx
Contributor
Posts: 54

Re: Format percentages in a histogram using sgplot

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.

Esteemed Advisor
Esteemed Advisor
Posts: 7,190

Re: Format percentages in a histogram using sgplot

I can't find anything about a percent variable being created, doesn't mean it doesnt exist though.  Anyways from the documentation:

http://support.sas.com/documentation/cdl/en/grstatgraph/65377/HTML/default/viewer.htm#n0wqbuiavwnaxn...

 

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;
SAS Super FREQ
Posts: 3,390

Re: Format percentages in a histogram using sgplot

@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.

SAS Super FREQ
Posts: 888

Re: Format percentages in a histogram using sgplot

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.

SAS Super FREQ
Posts: 888

Re: Format percentages in a histogram using sgplot

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.

Contributor ckx
Contributor
Posts: 54

Re: Format percentages in a histogram using sgplot

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.

Esteemed Advisor
Esteemed Advisor
Posts: 7,190

Re: Format percentages in a histogram using sgplot

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.

SAS Super FREQ
Posts: 676

Re: Format percentages in a histogram using sgplot

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;
Ask a Question
Discussion stats
  • 9 replies
  • 128 views
  • 2 likes
  • 5 in conversation