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

Hi.

I need some help with SGplot. I would like to have a 100% stacked bar chart with a datalabel on top showing the value, not 100%.

I have my 100% stacked bar chart - but if I add datalabel, then I get the value "100%".

title "sashelp.Baseball";
proc sgplot data=sashelp.baseball noborder pctlevel=group;
styleattrs datacolors=(DarkBlue LightBlue bip ) ;
  vbar league / response=crhits stat=percent nostatlabel
       group=division missing seglabel /*datalabel*/ nooutline datalabelattrs=(size=10)
	seglabelattrs=(color=white size=10 weight=bold);
  xaxis display=(nolabel noline noticks);
  yaxis display=(noline noticks) grid;
run;

Stacked_100%.JPG

The stacked bar chart with value - highlighted in yellow on the picture.

proc sgplot data=sashelp.baseball noborder ;
styleattrs datacolors=(DarkBlue LightBlue bip ) ;
  vbar league / response=crhits stat=sum nostatlabel
       group=division missing seglabel datalabel nooutline datalabelattrs=(size=10)
	seglabelattrs=(color=white size=10 weight=bold);
  xaxis display=(nolabel noline noticks);
  yaxis display=(noline noticks) grid;
run;

Stacked_with_value.JPG

How do I get the value to the 100% stacked bar chart?

My guess is some form of annotate, but I need some help.

Thanks.

Kind regards,

   Steffen

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanH_sas
SAS Super FREQ

Run the code below. If you want to bring the text down a little, add some OFFSETMAX to the YAXIS.

 

Hope this helps!

Dan

 

data anno;
retain x1space "datavalue" y1space "datapercent" y1 102 function "text" width 40;
input xc1 $ label $;
cards;
American 134889
National 105866
;
run;

 title "sashelp.Baseball";
 proc sgplot data=sashelp.baseball noborder pctlevel=group sganno=anno;
 styleattrs datacolors=(DarkBlue LightBlue bip ) ;
   vbar league / response=crhits stat=percent nostatlabel group=division        
            missing seglabel /*datalabel*/ nooutline datalabelattrs=(size=10)
 	    seglabelattrs=(color=white size=10 weight=bold);
   xaxis display=(nolabel noline noticks);
   yaxis display=(noline noticks) grid;
 run;

View solution in original post

3 REPLIES 3
DanH_sas
SAS Super FREQ

Run the code below. If you want to bring the text down a little, add some OFFSETMAX to the YAXIS.

 

Hope this helps!

Dan

 

data anno;
retain x1space "datavalue" y1space "datapercent" y1 102 function "text" width 40;
input xc1 $ label $;
cards;
American 134889
National 105866
;
run;

 title "sashelp.Baseball";
 proc sgplot data=sashelp.baseball noborder pctlevel=group sganno=anno;
 styleattrs datacolors=(DarkBlue LightBlue bip ) ;
   vbar league / response=crhits stat=percent nostatlabel group=division        
            missing seglabel /*datalabel*/ nooutline datalabelattrs=(size=10)
 	    seglabelattrs=(color=white size=10 weight=bold);
   xaxis display=(nolabel noline noticks);
   yaxis display=(noline noticks) grid;
 run;
Rick_SAS
SAS Super FREQ

Or use PROC MEANS or PROC FREQ to summarize the data and use VBARPARM and TEXT statements to display the bars and labels.

SteffenRannes
Fluorite | Level 6

Thanks. It works just fine.

I changed the data step creating the anno data to be more datadriven, but I got the basic idear 😉

 

proc sql;
create table anno as
select
League as xc1
,put(sum(crhits), commax12.) as label
,"datavalue" as x1space 
,"datapercent" as y1space 
,102 as y1 
,"text" as function 
,40 as width 
from sashelp.baseball
group by League
;
quit;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1336 views
  • 5 likes
  • 3 in conversation