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

Hi!

 

I am looking to make a bar chart that is grouped. The tricky part is that I want the datalabels to have both the count and the percent. Below is a picture of what I am looking to make (I used excel to make this). Below that is the code for example data I would like to use to make the attached bar chart.

 Capture.PNG

data bar;
infile datalines delimiter=",";
input letter $ group1 group2 group3;
datalines;
A, 3, 2, 2
B, 2, 3, 1
C, 0, 1, 0
;
run;

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Come to think of it, it was not that complicated. This code

 

data bar; 
infile datalines dlm=',';
input letter $ group1 group2 group3; 
datalines; 
A,3,2,2
B,2,3,1
C,0,1,0
; 
run;

proc transpose data=bar out=barlong(rename=(_NAME_=Group COL1=value));
   by letter;
run;

proc sql;
   create table GraphData as
   select *,
          cats(value, "(", put(value/(sum(value)), percent.), ")") as datalabel
   from barlong
   group by Group;
quit;

proc sgplot data=GraphData noautolegend noborder;
   vbar Group / response=value group=letter groupdisplay=cluster datalabel=datalabel;
   keylegend / position=e title="" noborder;
   yaxis grid display=(nolabel);
   xaxis display=(nolabel);
run;

 

Gives you this


SGPlot16.png

 

 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

You can draw the bar chart doing something like below. About having counts and percentages as datalabels, it can be done, but not directly in PROC SGPLOT I believe. Are you still interested?

 

data bar; 
infile datalines dlm=',';
input letter $ group1 group2 group3; 
datalines; 
A,3,2,2
B,2,3,1
C,0,1,0
; 
run;

proc transpose data=bar out=barlong(rename=(_NAME_=Group COL1=value));
   by letter;
run;

proc sgplot data=barlong noautolegend noborder;
   vbar Group / response=value group=letter groupdisplay=cluster datalabel;
   keylegend / position=e title="" noborder;
   yaxis grid display=(nolabel);
   xaxis display=(nolabel);
run;

 

PeterClemmensen
Tourmaline | Level 20

Come to think of it, it was not that complicated. This code

 

data bar; 
infile datalines dlm=',';
input letter $ group1 group2 group3; 
datalines; 
A,3,2,2
B,2,3,1
C,0,1,0
; 
run;

proc transpose data=bar out=barlong(rename=(_NAME_=Group COL1=value));
   by letter;
run;

proc sql;
   create table GraphData as
   select *,
          cats(value, "(", put(value/(sum(value)), percent.), ")") as datalabel
   from barlong
   group by Group;
quit;

proc sgplot data=GraphData noautolegend noborder;
   vbar Group / response=value group=letter groupdisplay=cluster datalabel=datalabel;
   keylegend / position=e title="" noborder;
   yaxis grid display=(nolabel);
   xaxis display=(nolabel);
run;

 

Gives you this


SGPlot16.png

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 5782 views
  • 3 likes
  • 2 in conversation