Written and maintained by CASRAI Editorial Board
Published
On this page
To create a histogram in SAS, use PROC SGPLOT with a HISTOGRAM statement naming a numeric variable. Use PROC UNIVARIATE when you also need distribution summaries or fitted distributions. For reproducible bin placement, record both the width and the center of the first bin: in SGPLOT, BINSTART is a center, rather than a lower endpoint.
What SAS is used for in research
A histogram helps researchers inspect the distribution of one numeric measurement before deciding how to summarize or model it. NIST identifies center, spread, asymmetry, unusual observations and multiple modes as features a histogram can reveal. SAS provides both a graphics route and a statistical-analysis route. The choice depends on whether the immediate deliverable is a figure or a broader distribution analysis.
SGPLOT is a direct route to the figure: identify the input dataset, identify the measurement, and request its histogram. UNIVARIATE can combine that inspection with descriptive statistics and distribution fitting. These procedures use different option names for controlling bins, so transferring a plotting specification requires more than changing the procedure name. The data table below compares their bin controls with the corresponding controls in R and Matplotlib.
For the underlying distinction between numeric intervals and categories, see the guide to histograms for research data. Here the task is implementing that figure in SAS. A defensible caption should identify the measurement and its units, the observations represented, and the vertical scale. Save the program with the analysis so another researcher can inspect those choices.
How to install or access SAS
Use your institution’s SAS environment for its research workflows. SAS also documents SAS OnDemand for Academics as a cloud service for teaching and learning, with access to SAS Studio for independent learners. Its official support page provides registration and getting-started resources. That learning route is useful for practicing syntax; it does not establish institutional approval to upload a research dataset.
Start with the supplied example dataset below before adapting the program to your own files. This keeps the initial plotting check separate from file preparation. If the example works but your own program does not, compare the dataset reference and measurement variable first. For the surrounding program structure, use the introduction to DATA and PROC steps in SAS.
The code here follows the linked SAS ODS Graphics and Base SAS procedure documentation. It uses the established SGPLOT and UNIVARIATE statements rather than version-specific task menus. Keep the procedure documentation alongside your program when reviewing a figure produced in a different SAS installation.
How to create a histogram in SAS step by step
-
Run a minimal documented example. SAS’s histogram-and-density example uses
SASHELP.HEARTand its numericCholesterolvariable. Begin with just the histogram. This first run lets SAS choose bins and uses the default percentage scale.proc sgplot data=sashelp.heart; histogram cholesterol; run;Read the program as two decisions:
DATA=selects the input dataset, andHISTOGRAMselects the measurement to bin. Replace both deliberately when moving to research data. If your measurements are still in a spreadsheet or CSV file, complete the SAS import and input-data checks before treating the plot as an analysis result. -
Choose the vertical scale explicitly. For a figure intended to show numbers of observations, request
SCALE=COUNT. For percentages, requestSCALE=PERCENT;SCALE=PROPORTIONexpresses fractions instead. The selection changes how bar heights are read, so include it in the saved program.proc sgplot data=sashelp.heart; histogram cholesterol / scale=count; run;Inspect the axis before writing a caption. A bar at 20 on a percent axis means something different from a bar at 20 on a count axis. When comparing two figures, establish that they use compatible scales before interpreting differences in their heights.
-
Control bin width and placement together. The next example requests bins of width 20 with the first center at 10.
SHOWBINSrequests horizontal tick marks at bin midpoints. These are example plotting settings, not a recommendation that every dataset use this width.proc sgplot data=sashelp.heart; histogram cholesterol / binwidth=20 binstart=10 scale=count showbins; run;For a desired lower endpoint a and width h, calculate the first center as a + h/2. A lower endpoint of 0 and width 20 therefore gives
BINSTART=10. This conversion follows SAS’s explanation of custom bins. Check the displayed result and log: a requested start that excludes the entire data range can be ignored.If you instead want to request an approximate number of bins, use
NBINS=. Avoid supplying it together withBINWIDTH=: the documented precedence makes the width option ineffective. SAS can adjust the requested bin count to obtain readable positions, so inspect the actual figure rather than assuming the request is exact. -
Add normal and kernel curves when they answer an analysis question. SAS’s official example combines a histogram with two
DENSITYstatements. The statement without a type requests the normal density estimate;TYPE=KERNELrequests the kernel estimate. Start with this documented combination before introducing other styling changes.proc sgplot data=sashelp.heart; histogram cholesterol; density cholesterol; density cholesterol / type=kernel; run;Use the curves to examine how alternative descriptions follow the observed distribution. They are fitted or smoothed representations, rather than additional observations. Describe which curve is shown; a reader should not have to infer whether a smooth line represents a normal model or a kernel estimate.
-
Use UNIVARIATE for the distribution-analysis route. Name the variable explicitly in both statements so the requested histogram is unambiguous. The
NORMALoption adds a fitted normal curve.proc univariate data=sashelp.heart; var cholesterol; histogram cholesterol / normal; run;For custom UNIVARIATE intervals, use
MIDPOINTS=orENDPOINTS=. SAS’s binning examples distinguish these directly: one specifies centers and the other boundaries. Do not copy an SGPLOT option into this procedure unchanged. If a VAR statement is present, the histogram variable must also appear in its variable list.
How to read the output
Read the horizontal intervals first, then the vertical scale, then the overall distribution. Look for where observations concentrate, how far they spread, and whether the shape changes across the range. A peak belongs to an interval: the graph does not identify every individual measurement inside it. Record possible unusual values for follow-up rather than assuming their cause from the picture.
Check counts, percentages and density with arithmetic
Illustrative composite: suppose a numeric dataset contains 20 included observations, with 5 in a particular interval of width 10. The count is 5. The proportion is 5/20 = 0.25, and the percentage is 100 × 0.25 = 25%. Under the area-density normalization described by NIST, the height is 5/(20 × 10) = 0.025. Multiplying that height by the interval width gives 0.25.
These invented demonstration numbers are not results from SASHELP.HEART. They provide a check on interpretation: a proportion is the fraction assigned to a bin, whereas density also accounts for its width. Do not read a density height as a percentage. NIST explains that density normalization makes the total area one; proportional normalization makes the bar heights sum to one.
Keep visual shape separate from model checking
A fitted normal curve is a comparison with a distributional model, not proof that the model fits. NIST points to probability plots and goodness-of-fit methods for checking a proposed distribution. In a research report, state the visible pattern separately from any formal model-checking result. Avoid turning “the histogram looks roughly symmetric” into a stronger statistical conclusion.
Inspect more than one sensible bin configuration when the interpretation depends on an apparent gap or second peak. Retain the settings used for the reported figure and explain their relationship to the measurement’s units or bounds. SAS’s binning discussion illustrates why time intervals and bounded proportions can need more deliberate placement than the automatically chosen bins.
Troubleshooting SAS
The bin locations do not match the specification
First distinguish centers from endpoints. A first center of 0 with width 20 places the first lower boundary at −10; it does not begin the interval at 0. Recalculate the center from the intended lower boundary. Then remove conflicting options and read the SAS log for bin adjustments before changing labels or colors.
SAS documents that NBINS= can change the bin width and that BINWIDTH= is ignored when both are specified. Its custom-bin article also explains warnings when the requested placement does not accommodate the data. In UNIVARIATE, an endpoint list can be extended to include observations beyond the requested list. An extension warning calls for inspecting the range, not automatically removing those observations.
The program creates unexpected histograms
In UNIVARIATE, omitting variable names can request histograms for every numeric variable in the dataset, or every variable in a VAR statement. Name the intended measurement rather than relying on that default. This matters when a dataset contains numeric identifiers alongside measurements: their storage type alone does not make them useful analysis variables.
Frequency data and weights are being confused
The documented SGPLOT FREQ= option represents repetitions. Noninteger frequencies are truncated; frequencies below one or missing are excluded from that calculation. Therefore, do not substitute arbitrary fractional weights for frequency counts. Separately, the UNIVARIATE HISTOGRAM documentation says that its WEIGHT statement cannot be used with that statement. Review the specific procedure before transferring a weighted workflow.
For a reproducibility review, retain the dataset reference, measurement name, bin specification, scale and any frequency treatment together. Compare those items before deciding that a changed plot reflects changed data. This checklist follows from the documented controls: two runs can select the same measurement while requesting different representations of it.
Free alternatives to SAS
R and Python with Matplotlib provide histogram functions when a project uses those environments. Their documented interfaces are compared below. Moving a figure between tools requires preserving its intervals and scale explicitly; matching the requested number of bins alone does not establish matching plots.
| Alternative | Documented histogram route | Check when moving a SAS figure |
|---|---|---|
| R graphics | hist() accepts a vector of breakpoints; plot=FALSE returns the histogram information without drawing it. |
Check right, include.lowest and freq. The default interval convention is right-closed. |
| Python Matplotlib | pyplot.hist() accepts an explicit sequence of bin edges and returns bin values, edges and plotting objects. |
Intervals include their left boundary and exclude the right, except the final interval. density=True selects area-density normalization. |
For R code and the interpretation of its output, see histograms with base R and ggplot2. Use the original numeric measurements when reconstructing a histogram. If only a picture survives, it cannot supply the original within-bin values. Save the analysis data and plotting specification alongside the exported figure so future changes can be traced.
Sources
- SAS: SGPLOT HISTOGRAM Statement, ODS Graphics Procedures Guide, checked 2026-11-16.
- SAS: Choosing bins for histograms in SAS, checked 2026-11-16.
- SAS: UNIVARIATE HISTOGRAM Statement, Base SAS 9.4 Procedures Guide, checked 2026-11-16.
- SAS: Combining Histograms with Density Plots, checked 2026-11-16.
- SAS: OnDemand for Academics, checked 2026-11-16.
- NIST/SEMATECH e-Handbook of Statistical Methods: Histogram, checked 2026-11-16.
- R graphics documentation: Histograms, checked 2026-11-16.
- Matplotlib documentation: matplotlib.pyplot.hist, checked 2026-11-16.
Data: Histogram bin specifications across SAS, R and Matplotlib
Original comparison of documented bin-location and scale controls compiled from SAS procedure documentation and binning guidance, the R graphics manual and the Matplotlib API reference. Each row names its source keys; sources were checked on 2026-11-16.
| Method | Bin-location controls | Scale control | Documented caveat | Source keys |
|---|---|---|---|---|
| SAS PROC SGPLOT | BINSTART= first center; BINWIDTH= width | SCALE=COUNT, PERCENT or PROPORTION; default PERCENT | NBINS= overrides BINWIDTH=; the displayed bin count may be adjusted | sgplot; bins |
| SAS PROC UNIVARIATE | HISTOGRAM MIDPOINTS= centers or ENDPOINTS= boundaries | HISTOGRAM VSCALE= controls vertical scale | An ENDPOINTS= list may be extended to accommodate the data | univariate; bins |
| R graphics hist() | breaks= vector specifies bin boundaries | freq=TRUE gives counts; freq=FALSE gives area density | Default right=TRUE makes intervals right-closed; include.lowest governs an extreme boundary | r |
| Matplotlib pyplot.hist() | bins= sequence specifies bin edges | density=False gives counts; density=True gives area density | Bins are left-closed and right-open except the final bin, which includes its right edge | mpl |
Sources
- sgplot: SAS: SGPLOT HISTOGRAM Statement (accessed 2026-11-16)
- bins: SAS: Choosing bins for histograms in SAS (accessed 2026-11-16)
- univariate: SAS: UNIVARIATE HISTOGRAM Statement (accessed 2026-11-16)
- r: R: Histograms (accessed 2026-11-16)
- mpl: Matplotlib: matplotlib.pyplot.hist (accessed 2026-11-16)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







