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

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;

Butterfly.jpg

1 ACCEPTED SOLUTION

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

Ksharp_0-1676107637284.png

 

View solution in original post

4 REPLIES 4
ballardw
Super User

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.

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

Ksharp_0-1676107637284.png

 

SASGeek
Obsidian | Level 7
Thanks so much! This is perfect!
Ksharp
Super User


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;

Ksharp_0-1676190585529.png

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 714 views
  • 4 likes
  • 3 in conversation