BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dbcrow
Obsidian | Level 7

I'm trying to get percentages inside a stacked bar chart, but they come out to two decimal places and I just need one.  (Note:  I had originally posted a version of this in this topic, but didn't get a response, so am posting it here):  

 

https://communities.sas.com/t5/SAS-Procedures/proc-gchart-format-inside-subpct/td-p/25122

 

Here're my data and code: 

 

Picture1.png

 

And the code: 

/*Figure 6:  Coalition Progress on Individual Activities (All Coalitions)*/
title1 "Progress on Individual Activities (All Coalitions)";
axis1 label=("Activity") value=(angle=45 rotate=0);
axis2 label=none value=none;
axis3 label=(angle=90 "Percent") value=("0%" "10%" "20%" "30%" "40%" "50%" "60%" "70%" "80%" "90%" "100%") 
	minor=none;
PROC GCHART data=temp;
	format prog1 prog.;
	vbar count / type=percent subgroup=prog1 g100 group=Activity inside=pct
	gaxis=axis1 maxis=axis2 raxis=axis3 width=20;
RUN;

 

I get this graph, which is very nearly what I need, just that the two decimal points give an impression of a great precision that just isn't in the data! 

 

Picture2.png

 

How can I quickly and easily get the data labels to just one decimal point?  Is there anything like the "maxdec" option?  Can I apply a variable format?  Do I have to do the complex "anno" procedure recommended recommended in this SAS User Community post?  

 

Thanks,

David

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Pre-summarize the data until SAS add a similar G100

 

proc freq data=sashelp.class;
   tables sex*age / outpct out=work.classfreq;
run;

Proc sgplot data=work.classfreq;
   vbar sex / group=age response=pct_row 
         seglabel seglabelformat=f2.;
run;

View solution in original post

9 REPLIES 9
ballardw
Super User

One of the reasons those sorts of Annotate macros exist is because the older device based graphics did not have options available to format values on bars (among other places in graphs).

 

Also there is not much updating going on for the older graphics procedures because they were based around devices and device files are not always a good way to go for modern document output.

 

So Proc SGplot as the options SEGLABEL to add a label to segments of a vertical or horizontal bar chart, which works with the SEGLABELFORMAT to set the display format for the values displayed on the segment. Other options involve SEGLABELFITPOLICY to help fit values into segments (or not) and SEGLABELATTRS to set the text options like font family, color and size of segment text.

dbcrow
Obsidian | Level 7

Thanks for this detailed advice! 🙂 I'll apply it and update you with the results.  As I mention below to Reeza, I'm still getting up to speed in SAS, so am not familiar with the program's history, commands (oops, procedures!), &c., so I appreciate the explanation on annotate, and on the newer graphics procedures. 

dbcrow
Obsidian | Level 7

I now remember the reason I used PROC GCHART:  the handy "g100" option to ensure that the percentages sum to 100% within each column.  My data are frequency counts ("count") of a category ("prog1") within another category ("Activity").  Here's a screenshot of the data.  

 

Picture1.png

 

This code:  

 

/*Figure 6:  Coalition Progress on Individual Activities (All Coalitions)*/
ods listing gpath="H:\Coalitions\";
ods graphics on / imagename="figure6" reset=index;
title1 "Figure 6. Progress by Individual Activities (All Coalitions)";
PROC SGPLOT data=temp2;
	format prog1 prog.;
	vbar Activity / response=count group=prog1 groupdisplay=stack seglabel;
	xaxis label="Activity";
	yaxis label="Percentage of Total for Each Activity";
RUN; 


Produces this graphic:  

 

figure61.png

 

which is farther from what I want than the graphic I produced with PROC GCHART.  

 

It looks like the options I have are: 

 

(1) doing more (or different) data pre-processing to get percentages in the "count" variables in the above data (e.g., the first cell in the count column would be 1/23 = 4.3), then getting the seglabel and segformat options to put in data labels with percent signs, or

 

(2) using the "annotate" statement with the "g100" option of PROC GCHART.  

 

The first option is probably easier, but isn't their something yet easier--the equivalent of "g100" for PROC SGPLOT?  

 

Thanks for your patience,

David

 

ballardw
Super User

Pre-summarize the data until SAS add a similar G100

 

proc freq data=sashelp.class;
   tables sex*age / outpct out=work.classfreq;
run;

Proc sgplot data=work.classfreq;
   vbar sex / group=age response=pct_row 
         seglabel seglabelformat=f2.;
run;
Reeza
Super User
FYI - I've moved your post to the graphics forum so the right people will see it. Regarding your previous post, please make sure to look at the dates. That one happens to be from 2009 and the only people who would likely see it are those who initially responded then, assuming they're still active on the community.
dbcrow
Obsidian | Level 7
Thanks, Reeza. I'm relatively new to SAS, and very new to the SAS community, so I appreciate the advice on getting posts to the correct forum. Of course, looking at dates of posts in *any* forum is *always* good advice. My bad. 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 4796 views
  • 5 likes
  • 3 in conversation