Dear All,
Greetings!
I would like to know how to display percent on top of each bar and counts inside each bar in a graph. Answer with the specific option for the same will be really helpful. Thanks in advance!
Thanking you,
Yours sincerely,
- Dr. Abhijeet Safai
proc sort data=sashelp.heart out=temp;by bp_status;run;
proc freq data=temp noprint order=freq;
/*by bp_status; */
table bp_status/out=temp2;
run;
data temp3;
set temp2;
length _datalabel $ 40;
_datalabel=cats(count,'/',put(percent,8.2),'%');
run;
proc sgplot data=temp3 noautolegend;
vbarparm category=bp_status response=count /nooutline;
text x=bp_status y=count text=_datalabel/splitchar='/' SPLITPOLICY=splitalways strip contributeoffsets=none;
xaxis display=(nolabel);
yaxis offsetmax=0.1;
run;
Do you mean something like this:
?
Bart
Yes @yabwon , the way in which the percent are appearing inside the bar, that is correct. However, the counts should be inside the bar, immediately below the top line of the bar and percent should be at the top of the bar.
Thank you.
- Dr. Abhijeet Safai
For something like this the annotations data set will be required, give me a minute will try to prepare something.
Do you have any ""test data"?
Bart
proc sort data=sashelp.heart out=temp;
by bp_status status;
run;
proc freq data=temp noprint order=data;
by bp_status;
table status/out=temp2 outcum;
run;
proc sort data=temp2;
by bp_status status;
run;
data annotation;
set temp2;
by bp_status;
lag_percent=LAG(cum_pct);
function="text";
drawspace="datavalue";
justify="center";
textsize=10;
justify= "center";
width=100;
widthunit="percent";
XC1=bp_status;
if first.bp_status then lag_percent=0;
label=put(count,9.-L);
anchor="bottom";
y1=lag_percent+1;
output;
label=strip(put(percent,8.2) !! '%');
anchor="top";
y1=cum_pct-1;
output;
run;
proc sgplot data=temp2 sganno=annotation ;
vbar bp_status /
group=status
response=percent
grouporder=data
;
run;
Bart
/*柱形图-分类变量*/
title c=black '按地区 分性别的柱形图';
proc sort data=sashelp.heart out=temp;by bp_status;run;
proc freq data=temp noprint order=freq;
by bp_status;
table status/out=temp2;
run;
data temp3;
set temp2;
length _datalabel $ 40;
_datalabel=cats(put(percent,8.2),'%/',count);
run;
proc sgplot data=temp3 ;
/*styleattrs datacolors=(grayaa lightblue lightred );*/
vbarparm category=bp_status response=count/ group=status groupdisplay=cluster
nooutline datalabel=_datalabel splitchar='/' datalabelpos=data ;
xaxis display=(nolabel);
keylegend /autooutline ;
run;
Thanks @yabwon and @Ksharp for the response. Something like this is expected. Only the difference is that - the counts inside need not be associated with some other bar (which are associated with the pink bar inside the blue bar as shown in this image) but the counts should be immediately below the bar (inside) and the percent should be immediately above the individual bar (outside). Thanks a lot.
Thank you.
- Dr. Abhijeet Safai
data have;
call streaminit(123);
do cat = "A","B","C";
val1=rand('uniform',0,10);
p=rand('uniform',0.2,0.5);
val2=(1-p)*val1;
output;
end;
run;
proc sgplot data=have;
vbarparm category=cat response=val1 /
nooutline datalabel=val1 datalabelpos=data ;
vbarparm category=cat response=val2 /
nooutline datalabel=val2 datalabelpos=data
BARWIDTH=0.5
;
run;
Bart
proc sgplot data=sashelp.heart;
vbar bp_status/stat=freq datalabel legendlabel='Freq' ;
vbar bp_status/stat=percent datalabel y2axis barwidth=0.6 legendlabel='Percent';
yaxis valueattrs=graphdata1 labelattrs=graphdata1;
y2axis values=(0 to 1 by 0.2) valueattrs=graphdata2 labelattrs=graphdata2;
run;
proc sort data=sashelp.heart out=temp;by bp_status;run;
proc freq data=temp noprint order=freq;
/*by bp_status; */
table bp_status/out=temp2;
run;
data temp3;
set temp2;
length _datalabel $ 40;
_datalabel=cats(count,'/',put(percent,8.2),'%');
run;
proc sgplot data=temp3 noautolegend;
vbarparm category=bp_status response=count /nooutline;
text x=bp_status y=count text=_datalabel/splitchar='/' SPLITPOLICY=splitalways strip contributeoffsets=none;
xaxis display=(nolabel);
yaxis offsetmax=0.1;
run;
@Ksharp , Correct! Thanks a lot!
Thank you.
- Dr. Abhijeet Safai
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.