BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mksredl
Fluorite | Level 6

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;

 

mksredl_0-1655327153016.png

 

 

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

Ksharp_0-1655384948715.png

 

View solution in original post

6 REPLIES 6
ballardw
Super User

Use Respct as the Response variable is one way.

 

Or option Datalabel=respct on the hbarparm statement

Ksharp
Super User
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;

Ksharp_0-1655384948715.png

 

mksredl
Fluorite | Level 6

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?

mksredl_0-1655488800941.png

 

Ksharp
Super User

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;

Ksharp_0-1655545056309.png

 

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