Hi there,
I am trying to create a butterfly graph and it's almost there but not yet. I need to add the values of the bars and convert everything to percentages. I would also like to add 2 vertical dashed lines at -20% and + 20%.
I've done what I can so can someone get me a bit further?
Thank you so much!
Paula
proc format;
picture posval low-<0='00000%'
(prefix='-')
0='9%'
0<-high='00000%';
run;
data sales;
set sales;
if pct_diff < 0 then neg_diff = pct_diff;
if pct_diff >= 0 then pos_diff = pct_diff;
zero = 0;
run;
title1 '% Change of Sales between Dates';
proc sgplot data=sales;
format pct_diff posval.;
hbarparm category=rundt response=neg_diff /
datalabel=pct_diff datalabelattrs=(size=10);
hbarparm category=rundt response=pos_diff /
datalabel=pct_diff datalabelattrs=(size=10);
xaxis values=(-10 to 10 by .25) display=(nolabel) grid;
yaxis display=(nolabel);
run;
/*
As Ballardw said,
post some data,so we can test your code.
*/
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);
group=ifc(value>0,'pos_diff','neg_diff');
output;
end;
format date date9. value posval.;
run;
proc sgplot data=have;
hbarparm category=date response=value/group=group datalabel=value
GROUPDISPLAY=CLUSTER DATALABELFITPOLICY=INSIDEPREFERRED
;
keylegend /title='';
yaxis label='';
xaxis label='';
run;
You included a format in your code but do not show where you might be using it.
This is interesting because you are using variable names like Pct_dif which leads one to expect percentages of some type are involved. The interesting thing about that shown format is that most values between -1 and 1 do not display (i.e. decimal values such as 0.99 that one might in some circumstances expect to see displayed as 99%. So if you have the format attached to your datalabel variable that might be surpressing the results
It may be that a TEXT plot superimposed on your hbarparm could be the best way to display the text other than where default datalabels might go.
Data.
Can't plot without actual data.
/*
As Ballardw said,
post some data,so we can test your code.
*/
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);
group=ifc(value>0,'pos_diff','neg_diff');
output;
end;
format date date9. value posval.;
run;
proc sgplot data=have;
hbarparm category=date response=value/group=group datalabel=value
GROUPDISPLAY=CLUSTER DATALABELFITPOLICY=INSIDEPREFERRED
;
keylegend /title='';
yaxis label='';
xaxis label='';
run;
proc sgplot data=have;
hbarparm category=date response=value/group=group datalabel=value
GROUPDISPLAY=CLUSTER DATALABELFITPOLICY=INSIDEPREFERRED
;
keylegend /title='';
refline -20 20/axis=x lineattrs=(pattern=shortdash) ;
yaxis label=' ';
xaxis label=' ' values=(-100 to 100 by 20);
run;
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.