SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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