BookmarkSubscribeRSS Feed
braam
Quartz | Level 8

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;

 

5 REPLIES 5
Ksharp
Super User
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;
ed_sas_member
Meteorite | Level 14

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;

Capture d’écran 2020-02-23 à 09.41.43.png

braam
Quartz | Level 8

@ed_sas_member 

@Ksharp 

 

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.

ed_sas_member
Meteorite | Level 14

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,

ed_sas_member
Meteorite | Level 14

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;

Unknown.png

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1446 views
  • 1 like
  • 3 in conversation