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

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 5017 views
  • 3 likes
  • 2 in conversation