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.
data bar;
infile datalines delimiter=",";
input letter $ group1 group2 group3;
datalines;
A, 3, 2, 2
B, 2, 3, 1
C, 0, 1, 0
;
run;
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
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;
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
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!
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.