BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
DrAbhijeetSafai
Lapis Lazuli | Level 10

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

 

 

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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_0-1709456079769.png

 

View solution in original post

11 REPLIES 11
DrAbhijeetSafai
Lapis Lazuli | Level 10

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

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15
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;

yabwon_0-1709290448563.png

 

Bart

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



DrAbhijeetSafai
Lapis Lazuli | Level 10
Thanks a lot.
It think this is the solution I was looking for. I will mark this as a solution soon. Thanks once again.

- Dr. Abhijeet Safai
Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
Ksharp
Super User
/*柱形图-分类变量*/
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;





Ksharp_0-1709286380373.png

 

DrAbhijeetSafai
Lapis Lazuli | Level 10

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

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
yabwon
Onyx | Level 15
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;

yabwon_0-1709291339169.png

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Ksharp
Super User
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;

Ksharp_0-1709452934116.png

 

Ksharp
Super User
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_0-1709456079769.png

 

DrAbhijeetSafai
Lapis Lazuli | Level 10

@Ksharp , Correct! Thanks a lot!

 

Thank you.

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real

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