Home » SAS: How to Specify Number of Bins in Histogram

SAS: How to Specify Number of Bins in Histogram

by Tutor Aspire

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

You may also like