Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Re: Number and Percentage by group in PROC REPORT hbar?

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 02-22-2020 05:18 PM
(972 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

```
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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Tags:
- proc gchart

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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,

- Tags:
- PROC SGPLOT

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Tags:
- annotate
- proc gchart

**SAS Innovate 2025** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

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.

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