BookmarkSubscribeRSS Feed
ckx
Quartz | Level 8 ckx
Quartz | Level 8

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/

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ckx
Quartz | Level 8 ckx
Quartz | Level 8

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
Rick_SAS
SAS Super FREQ

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

DanH_sas
SAS Super FREQ

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.

DanH_sas
SAS Super FREQ

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.

ckx
Quartz | Level 8 ckx
Quartz | Level 8

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

BrunoMueller
SAS Super FREQ

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-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
  • 9 replies
  • 9196 views
  • 3 likes
  • 5 in conversation