Using SAS9.4
I have the following code:
proc sgplot data= peroneal.labeled pctlevel=graph;
styleattrs datacolors=(red blue green) ;
title 'Current Level of Function';
title2 'P=0.914';
vbar c_sx_technique / group=ratecurrent_level_func stat=freq;
vbar c_sx_technique / group=ratecurrent_level_func stat=percent_func datalabel;
xaxis display=(nolabel);
yaxis grid label='Frequency' values=(0 5 10 15 20 25 30 35 40);
run;
Is there a way to modify this code so the percent (created variable percent_func) within each stacked bar graph will be displayed on the bar? Thank you
@GS2 wrote:
Using SAS9.4
I have the following code:
proc sgplot data= peroneal.labeled pctlevel=graph;
styleattrs datacolors=(red blue green) ;
title 'Current Level of Function';
title2 'P=0.914';
vbar c_sx_technique / group=ratecurrent_level_func stat=freq;
vbar c_sx_technique / group=ratecurrent_level_func stat=percent_func datalabel;
xaxis display=(nolabel);
yaxis grid label='Frequency' values=(0 5 10 15 20 25 30 35 40);
run;
Is there a way to modify this code so the percent (created variable percent_func) within each stacked bar graph will be displayed on the bar? Thank you
You might be looking for the SEGLABEL option for the vbar to label segments.
Hi GS2,
I might try these options:
DATALABEL = COLPercent;
FORMAT COLPercent n.w; (to format as necessary)
The COLPercent option should label each column with the percent, no need to create a new variable.
@GS2 wrote:
I tried this and got an error saying to use "pct" or "percent" and neither produced the output I am looking for. Thank you
When you get errors then best practice on this forum is to go to your log, copy the entire procedure code with all the notes, warning or errors and then on the forum open a text box using the </> icon and paste the log text. The text box will preserve formatting of any diagnostic characters that SAS provides while the main message windows will reformat the text, sometimes resulting in very hard to read log.
Does this show something close to what you are looking for in terms of the percents on the bar:
proc sgplot data=sashelp.class; vbar sex /group=age stat=percent seglabel; run;
@GS2 wrote:
Yes, that is exactly how I would like to see it.
Your turn.
Show us the code with SEGLABEL from your log that throws the errors.
proc sgplot data= peroneal.labeled pctlevel=graph;
styleattrs datacolors=(red blue green) ;
title 'Current Level of Function';
title2 'P=0.914';
vbar c_sx_technique / group=ratecurrent_level_func stat=freq;
vbar c_sx_technique / group=ratecurrent_level_func stat=percent seglabel;
xaxis display=(nolabel);
yaxis grid label='Frequency' values=(0 5 10 15 20 25 30 35 40);
run;
146 proc sgplot data= peroneal.labeled pctlevel=graph;
147 styleattrs datacolors=(red blue green) ;
148 title 'Current Level of Function';
149 title2 'P=0.914';
150 vbar c_sx_technique / group=ratecurrent_level_func stat=freq;
151 vbar c_sx_technique / group=ratecurrent_level_func stat=percent seglabel;
152 xaxis display=(nolabel);
153 yaxis grid label='Frequency' values=(0 5 10 15 20 25 30 35 40);
154 run;
NOTE: PROCEDURE SGPLOT used (Total process time):
real time 0.27 seconds
cpu time 0.06 seconds
NOTE: There were 51 observations read from the data set PERONEAL.LABELED.
I do not get an error when submitting the code but the percents do not show on the graph either
Remove the
vbar c_sx_technique / group=ratecurrent_level_func stat=freq;
Run this code and see the difference between the two statistic and one statistic versions:
proc sgplot data=sashelp.class; title "Two Vbar statistics"; vbar sex /group=age stat=freq ; vbar sex /group=age stat=percent seglabel; run; proc sgplot data=sashelp.class; title "One Vbar statistic"; vbar sex /group=age stat=percent seglabel; run;
If you look closely at the two statistic version and count the number of segments in each you will note that there is a big difference in the number of segments displayed.
Quick: what is the decimal value of 15%?
If you remember that is 0.15 then you can tell what happens when combined with a frequency bar: ALL the percents are less than 1 and the size is so small there is no room by default for the text to display. If you make the graph large enough you then may have room enough for text to display for some values:
ods graphics / height=20in; proc sgplot data=sashelp.class; title "Two Vbar statistics"; vbar sex /group=age stat=freq ; vbar sex /group=age stat=percent seglabel; run; ods graphics / reset=all;
But stacking freq and percent on a single bar makes no sense any way.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.