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

Hi All,

 

Apologies if this has been answered. I've tried to find it in the topics to no avail... maybe not using the right key words?

 

Thanks to @Reeza for helping me realize I should switch to SGPLOT from using GCHART pretty much exclusively until now.

 

My problem is that I want to be able to do something in SGPLOT that I can do in GCHART, which is show percent labels above the VBARs, but show the SUMVAR sum on the Y Axis. So far in SGPLOT I have been able to get percent over both VBAR and on YAXIS, or response sums over VBAR and on YAXIS.

 

Below is some code that shows first what I want to do in SGPLOT (but using GCHART as example), then second and third where I am so far with SGPLOT:

 

proc format;
	value HPBins
		low -< 100  = '<100'
		100 -< 200  = '<200'
		200 -< 300  = '<300'
		300 -< 400  = '<400'
		400 -  high = '400+';
run;

/* FIRST */ title1 'What I want to show in SGPLOT, using GCHART as example'; title2 'GCHART: Percent label over VBAR, sumvar SUM on Y-Axis'; proc gchart data=SASHELP.CARS; format Horsepower HPBins.; vbar Horsepower / discrete sumvar=MSRP outside=percent; run;
/* SECOND */ title1 'Not quite there...'; title2 'SGPLOT: Response SUM labels over VBAR and on Y-Axis'; proc sgplot data=SASHELP.CARS; format Horsepower HPBins.; vbar Horsepower / response=MSRP datalabel; xaxis values=('<100' '<200' '<300' '<400' '400+'); run; run;
/* THIRD */ title1 'Not quite there...'; title2 'SGPLOT: Percent labels over VBAR and on Y-Axis'; proc sgplot data=SASHELP.CARS; format Horsepower HPBins.; vbar Horsepower / response=MSRP stat=percent datalabel; xaxis values=('<100' '<200' '<300' '<400' '400+'); run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Someone also asked this recently and I posted a solution....if you search through my post history you'll find it. The trick is to do it twice and suppress one plot or pre-summarize and customize the label with a different variable.

View solution in original post

4 REPLIES 4
Reeza
Super User

Someone also asked this recently and I posted a solution....if you search through my post history you'll find it. The trick is to do it twice and suppress one plot or pre-summarize and customize the label with a different variable.

ballardw
Super User

One way that gets pretty close

proc sgplot data=SASHELP.CARS noautolegend;
	format Horsepower HPBins.;
   vbar horsepower / response=msrp stat=sum name='sum'

                     ;
	vbar Horsepower / response=MSRP stat=percent 
                    datalabel y2axis name='perc' ;
	xaxis values=('<100' '<200' '<300' '<400' '400+');
run;

Personally I never had good luck with Gchart and created my own summary values so I could explicitly state variables and/or values.

Which with SGPLOT and overlay of Textplot or similar provides a lot of control.

subpar_actuary
Fluorite | Level 6

@Reeza thanks I found the post.

 

Pretty nifty way to rig the graph. I had some problems with the back (first) vbar showing up in the background, so I added transparency=1 which fixed that. I assume that issue is because the left and right axes are different bases, and since SAS fits each axis to nice round numbers, they sometimes don't line up horizontally.  

 

Running @ballardw 's code and adding 'by make', you can see the issue with the Dodge graph and a number of others:

SGPLOT_Example_1.PNG

 

Here's the final code that adds the transparency option (along with other options to get percents at by level, remove the y2axis labels, and change the ugly orange to blue 😝. Thanks all for your help!

 

proc sgplot data=SASHELP.CARS noautolegend pctlevel=by;
	format horsepower HPBins.;
	vbar horsepower / response=msrp stat=sum
		name='sum' transparency=1;
	vbar Horsepower / response=msrp stat=percent
		datalabel y2axis name='perc' fillattrs=(color=vligb) outlineattrs=(color=bib);
	xaxis values=('<100' '<200' '<300' '<400' '400+');
	y2axis display=none;
	by make;
	run;

 

SGPLOT_Example_2.PNG

ballardw
Super User

You could use VALUES=() lists on both axis to restrict the range of values, make things align and should address the "overlap"/"peek-a-bou" issue arising from the SAS underlying rules for building the axis range, or possibly as simple as MAX= on each axis.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 872 views
  • 2 likes
  • 3 in conversation