How can the number of bins in a histogram be specified using SAS? 2

How can the number of bins in a histogram be specified using SAS?

In SAS, the number of bins in a histogram can be specified by using the “binwidth” option in the PROC UNIVARIATE statement. This option allows the user to specify the desired number of bins or the width of each bin. Additionally, the “nbins” option can be used to specify the exact number of bins to be displayed in the histogram. By using these options, the user can effectively control the number of bins in a histogram and customize it according to their specific needs.

SAS: Specify Number of Bins in Histogram


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 univariatedata=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 printdata=my_data;

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

/*create histogram for points variable*/
proc univariatedata=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 univariatedata=my_data;
    histogram points / midpoints=(9 to 36 by 3);
run;

specify bins in histogram in SAS

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 univariatedata=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.

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

Cite this article

stats writer (2024). How can the number of bins in a histogram be specified using SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-number-of-bins-in-a-histogram-be-specified-using-sas/

stats writer. "How can the number of bins in a histogram be specified using SAS?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-the-number-of-bins-in-a-histogram-be-specified-using-sas/.

stats writer. "How can the number of bins in a histogram be specified using SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-number-of-bins-in-a-histogram-be-specified-using-sas/.

stats writer (2024) 'How can the number of bins in a histogram be specified using SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-number-of-bins-in-a-histogram-be-specified-using-sas/.

[1] stats writer, "How can the number of bins in a histogram be specified using SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can the number of bins in a histogram be specified using SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top