SAS Enterprise Guide

Desktop productivity for business analysts and programmers
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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