Hello! I have a stacked bar graph created using HBARPARM and am trying to have the segment labels show the percentage, not the count. Is there a way to do that?
My code is:
proc sgplot data = path_resistance;
yaxis display = (nolabel) valueattrs = (color=black size=12pt);
xaxis grid min=0 label="Number of infections" ;
hbarparm category = pathGroup response = resCount /
group = pathResistant
seglabel;
run;
I have a column with the percentages (resPct). The structure of my data is:
data path_resistance;
infile datalines delimiter = ',';
input pathGroup pathResistant resCount resPct;
datalines;
Pathogen A,No,170,98
Pathogen A,Yes,3,2
Pathogen B,No,100,86
Pathogen B,Yes,6,14
Pathogen C,No,53,65
Pathogen C,Yes,28,35
;
run;
Thank you for the help!
data path_resistance;
infile datalines delimiter = ',';
input pathGroup :$40. pathResistant $ resCount resPct;
format resPct percent.;
datalines;
Pathogen A,No,170,.98
Pathogen A,Yes,3,.2
Pathogen B,No,100,.86
Pathogen B,Yes,6,.14
Pathogen C,No,53,.65
Pathogen C,Yes,28,.35
;
run;
data have;
set path_resistance;
by pathGroup;
if first.pathGroup then cum=0;
cum+resCount;
p=cum-resCount/2;
run;
proc sgplot data = have;
yaxis display = (nolabel) valueattrs = (color=black size=12pt);
xaxis grid min=0 label="Number of infections" ;
hbarparm category = pathGroup response = resCount /group = pathResistant;
text x=p y=pathGroup text=resPct /strip CONTRIBUTEOFFSETS=none;
run;
Use Respct as the Response variable is one way.
Or option Datalabel=respct on the hbarparm statement
data path_resistance;
infile datalines delimiter = ',';
input pathGroup :$40. pathResistant $ resCount resPct;
format resPct percent.;
datalines;
Pathogen A,No,170,.98
Pathogen A,Yes,3,.2
Pathogen B,No,100,.86
Pathogen B,Yes,6,.14
Pathogen C,No,53,.65
Pathogen C,Yes,28,.35
;
run;
data have;
set path_resistance;
by pathGroup;
if first.pathGroup then cum=0;
cum+resCount;
p=cum-resCount/2;
run;
proc sgplot data = have;
yaxis display = (nolabel) valueattrs = (color=black size=12pt);
xaxis grid min=0 label="Number of infections" ;
hbarparm category = pathGroup response = resCount /group = pathResistant;
text x=p y=pathGroup text=resPct /strip CONTRIBUTEOFFSETS=none;
run;
This solution worked great, thank you! Follow up question: is there a way to prevent the labels from overlapping when the bar is very small, such as in the last bar below?
You need change the position of text according to your data.
data path_resistance; infile datalines delimiter = ','; input pathGroup :$40. pathResistant $ resCount resPct; format resPct percent8.0; datalines; Pathogen A,No,170,.98 Pathogen A,Yes,3,.02 Pathogen B,No,100,.86 Pathogen B,Yes,6,.14 Pathogen C,No,4,.60 Pathogen C,Yes,2,.40 ; run; data have; set path_resistance; by pathGroup; if first.pathGroup then cum=0; cum+resCount; p=cum-resCount/2; run; data have2; set have; if pathGroup=lag(pathGroup) and dif(p)<10 then p=p+10; *Change it according to your data.; run; proc sgplot data = have2; yaxis display = (nolabel) valueattrs = (color=black size=12pt); xaxis grid min=0 label="Number of infections" ; hbarparm category = pathGroup response = resCount /group = pathResistant; scatter x=p y=pathGroup /markerchar=resPct; run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.