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;
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.
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.
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.
@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:
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;
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.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.