I have two related questions.
1. Can I report the number of observations by "MAKE" and "TYPE" in the outcome that the below code generates? There are 7 observations for Acura. It seems that a majority of 7 Acuras are Sedan, but I cannot see the exact number.
2. Can there be percentages, instead of numbers, presented? It seems to me that there are 1 SUV, 5 Sedan, and 1 Sports in the first car maker group (i.e., Acura). I would like to report the percentages, if possible (i.e., 14%, 72% and 14%).
Thanks!
ods graphics/ height= 8in;
proc sgplot data= sashelp.cars;
hbar make / group= type datalabel ;
yaxis fitpolicy= none; run;
proc sgplot data= sashelp.cars;
where make in ('Audi' 'BMW');
hbar make / group= type datalabel stat=freq groupdisplay=cluster;
yaxis fitpolicy= none; run;
proc sgplot data= sashelp.cars;
where make in ('Audi' 'BMW');
hbar make / group= type datalabel stat=pct groupdisplay=cluster;
yaxis fitpolicy= none; run;
Hi @braam
You can also display both frequency and percentage using PROC GCHART:
axis1 label=none;
proc gchart data= sashelp.cars;
where make in ('Audi' 'BMW');
hbar type /group=make subgroup=type freq percent space=0 raxis=axis1;
run;
Thanks for your answers! But isn't there a way to solve this with the style of graph being unchanged?
I mean, one single bar for one car maker where there are multiple types of cars.
Hi @braam
using SGPLOT, you can just change the option 'data label' to 'seglabel' as follows:
proc sgplot data= sashelp.cars;
where make in ('Audi' 'BMW');
hbar make / group= type seglabel stat=percent;
yaxis fitpolicy= none;
run;
Best,
Hi @braam
Here is another approach, using a file for annotation:
proc freq data=sashelp.cars noprint order=freq;
table type / out=cars_freq;
by make;
run;
/* file for annotation */
data bar_anno;
set cars_freq;
function='label';
retain xsys '2' ysys '2' color 'black' when 'a';
midpoint=make;
subgroup=type;
position='+';
text=strip(put(percent/100, percent8.0));
run;
/* Graph */
axis1 label=(angle=90 "Make");
axis2 label=none;
proc gchart data=sashelp.cars;
hbar make / subgroup=type nostats space=0 annotate=bar_anno maxis=axis1 raxis=axis2;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.