17.9 C
London
Thursday, July 24, 2025
HomeSASDescriptive Statistics in SASSAS: How to Specify Number of Bins in Histogram

SAS: How to Specify Number of Bins in Histogram

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the midpoints statement to specify the number of bins that should be used in a histogram in SAS.

This statement uses the following basic syntax:

proc univariate data=my_data;
    histogram my_variable / midpoints=(9 to 36 by 3);
run;

This particular example creates a histogram with midpoints ranging from 9 to 36 at intervals of 3.

The following example shows how to use this syntax in practice.

Example: How to Specify Number of Bins in Histogram in SAS

Suppose we have the following dataset in SAS that contains information about various basketball players:

/*create dataset*/
data my_data;
    input team $ points rebounds;
    datalines;
A 29 8
A 23 6
A 20 6
A 21 9
A 33 14
A 35 11
A 31 10
B 21 9
B 14 5
B 15 7
B 11 10
B 12 6
B 10 8
B 15 10
;
run;

/*view dataset*/
proc print data=my_data;

We can use the following syntax to create a histogram for the points variable:

/*create histogram for points variable*/
proc univariate data=my_data;
    histogram points;
run;

The x-axis displays the values for the points variable and the y-axis displays the percentage of observations in the dataset that fall into various values.

Notice that the midpoints in the histogram occur at intervals of 6.

To increase the number of bins in the histogram, we can specify the midpoints to occur at intervals of 3 instead:

/*create histogram for points variable with custom bins*/
proc univariate data=my_data;
    histogram points / midpoints=(9 to 36 by 3);
run;

specify bins in histogram in SAS

Notice that this histogram has more total bins than the previous histogram since we made the intervals between the midpoints smaller.

To decrease the number of bins in the histogram, we can specify the midpoints to occur at intervals of 9 instead:

/*create histogram for points variable with custom bins*/
proc univariate data=my_data;
    histogram points / midpoints=(9 to 36 by 9);
run;

Notice that this histogram has fewer total bins than the previous histogram since we made the intervals between the midpoints larger.

Feel free to play around with the values in the midpoints statement to increase or decrease the number of bins in your histogram.

Additional Resources

The following tutorials explain how to create other charts in SAS:

How to Create Line Plots in SAS
How to Create Boxplots by Group in SAS
How to Create a Scatterplot with Regression Line in SAS

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories