Hi all,
First, thanks so much for the great answers I've gotten on all my questions. You are all the best! I'm almost there with my project and I just need help with one more part. . .
I'm running SAS 9.4 on EG 7.15 and I have a butterfly chart where some values show 4000% and most others show 5% - 20%. I am able to label everything (thanks to great code provided here) EXCEPT those that are massive values. So I need to label some inside the bars and some outside the bars. I know there's an option for this but that doesn't come in until SAS 9.5. Here's the code I'm using for now:
proc format;
picture posval low-<0='00000%'
(prefix='-')
0='9%'
0<-high='00000%';
run;
data have;
call streaminit(123);
do date='01jan2022'd to '20jan2022'd;
value=rand('integer',-100,100);
n=rand('integer',100,4000);
group=ifc(value>0,'pos_diff','neg_diff');
if value>0 then datalabel_pos=cats(value,"%|(",n,")");
else datalabel_neg=cats(value,"%|(",n,")");
output;
call missing(datalabel_pos,datalabel_neg);
end;
format date date9. value posval.;
run;
proc sgplot data=have;
hbarparm category=date response=value/group=group name='x' ;
text x=value y=date text=datalabel_pos/splitchar='|' strip contributeoffsets=none
SPLITCHAR='|' SPLITJUSTIFY=left SPLITPOLICY=splitalways position=right ;
text x=value y=date text=datalabel_neg/splitchar='|' strip contributeoffsets=none
SPLITCHAR='|' SPLITJUSTIFY=left SPLITPOLICY=splitalways position=left ;
keylegend 'x'/title='';
yaxis label=' ';
xaxis label=' ' offsetmin=0.1 offsetmax=0.1;
run;
Thank you!
Paula
proc format;
picture posval low-<0='00000%'
(prefix='-')
0='9%'
0<-high='00000%';
run;
data have;
call streaminit(123);
do date='01jan2022'd to '10jan2022'd;
value=rand('integer',-100,100);
n=rand('integer',100,4000);
group=ifc(value>0,'pos_diff','neg_diff');
datalabel=cats(value,"%|(",n,")");
/*greater than 80 data label is inside,otherwise outside*/
if value>=80 then _value=value-8;
else if value<=-80 then _value=value+8;
else if 0<value<=80 then _value=value+8;
else if -80<value<0 then _value=value-8;
output;
end;
format date date9. value posval.;
run;
proc sgplot data=have;
hbarparm category=date response=value/group=group name='x' ;
text x=_value y=date text=datalabel/splitchar='|' strip contributeoffsets=none
SPLITCHAR='|' SPLITJUSTIFY=center SPLITPOLICY=splitalways position=center ;
keylegend 'x'/title='';
yaxis label=' ';
xaxis label=' ';
run;
proc format;
picture posval low-<0='00000%'
(prefix='-')
0='9%'
0<-high='00000%';
run;
data have;
call streaminit(123);
do date='01jan2022'd to '10jan2022'd;
value=rand('integer',-100,100);
n=rand('integer',100,4000);
group=ifc(value>0,'pos_diff','neg_diff');
datalabel=cats(value,"%|(",n,")");
/*greater than 80 data label is inside,otherwise outside*/
if value>=80 then _value=value-8;
else if value<=-80 then _value=value+8;
else if 0<value<=80 then _value=value+8;
else if -80<value<0 then _value=value-8;
output;
end;
format date date9. value posval.;
run;
proc sgplot data=have;
hbarparm category=date response=value/group=group name='x' ;
text x=_value y=date text=datalabel/splitchar='|' strip contributeoffsets=none
SPLITCHAR='|' SPLITJUSTIFY=center SPLITPOLICY=splitalways position=center ;
keylegend 'x'/title='';
yaxis label=' ';
xaxis label=' ';
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.