Example Program and Statement Details

Example Graph

The following graph was generated by the Example Program:
Example Density Plot

Example Program

When used as a stand-alone plot or overlaid with other DENSITYPLOTs, the dependent axis shows the computed density values.
proc template;
  define statgraph densityplot1;
    begingraph;
      entrytitle "Fitted Density Curves";
      entrytitle "of Patient Weight";
      entryfootnote halign=left "Framingham Heart Study";
      layout overlay; 
        densityplot weight / normal()
         lineattrs=graphfit name="n" legendlabel="Normal";
        densityplot weight / kernel()
         lineattrs=graphfit2 name="k" legendlabel="Kernel";
        discretelegend "n" "k"; 
      endlayout;
    endgraph;
  end;
run;
proc sgrender data=sashelp.heart template=densityplot1;
  label weight="Patient Weight";
run;
When one or more DENSITYPLOTs are overlaid on a HISTOGRAM, the dependent axis shows the statistic indicated by the HISTOGRAM’s SCALE= option. The area under each density curve is equal to the area of the HISTOGRAM.
Density Plot Overlaid on a Histogram
proc template;
  define statgraph densityplot2;
    begingraph;
	  entrytitle "Patient Weight Distribution";
      entrytitle "with Fitted Normal Curve";
      entryfootnote halign=left "Framingham Heart Study";
      layout overlay; 
        histogram weight / primary=true scale=count; 
        densityplot weight / normal() lineattrs=graphfit;
      endlayout;
    endgraph;
  end;
run;
proc sgrender data=sashelp.heart template=densityplot2;
  label weight="Patient Weight";
run;

Statement Summary

A typical DENSITYPLOT statement specifies either the NORMAL or the KERNEL distribution option. If no distribution option is specified, NORMAL() is used. The following syntax explicitly shows the default case:
DENSITYPLOT numeric-column / NORMAL()
To specify a KERNEL distribution, use the following plot syntax:
DENSITYPLOT numeric-column / KERNEL()
If more than one distribution option is specified, then the last distribution option specified is used.

Required Arguments

numeric-column
specifies a numeric column of data values that are used to calculate the parameters for the probability distribution.
expression
specifies an expression that calculates values when those values are not stored in the data.

Distribution Options

KERNEL (C=number WEIGHT=NORMAL | QUADRATIC | TRIANGULAR MAXPOINTS=number )
specifies a nonparametric kernel density estimate. The general form of the kernel density estimator is as follows.
kernel distribution estimator
In the equation, is the weight function, is the bandwidth, is the sample size, and is the ith observation. You can use the C= suboption to specify the bandwidth and the WEIGHT= suboption to specify the weight function .
For more information, see the discussion of Kernel Density Estimates for the UNIVARIATE procedure in the Base SAS Procedures Guide , Volume 3.
C=
specifies a positive number (0 < number <= 100) that represents the standardized bandwidth.
Default: Calculated from the data as the bandwidth that minimizes the approximate mean integrated square error (MISE).
The value of , referred to as the bandwidth parameter, determines the degree of smoothness in the estimated density function. You specify indirectly by specifying a standardized bandwidth c with the C= kernel-option. If Q is the interquartile range and n is the sample size, then c is related to by the following formula:
WEIGHT=
specifies one of the weight functions NORMAL, QUADRATIC, or TRIANGULAR.
Default: NORMAL
The formulas for the weight functions are
kernel weight functions
MAXPOINTS=
specifies the maximum number of points generated for the curve.
Default: 512
NORMAL ( MU=number SIGMA=number MAXPOINTS=number )
specifies a normal density estimate, with mean and standard deviation. The fitted density function equation is as follows:
fitted density function equation
In the equation, is the mean and is the standard deviation (). You can specify with the MU= suboption and with the SIGMA= suboption. By default, ODS estimates with the sample mean and with the sample standard deviation.
For more information, see the discussion of Kernel Density Estimates for the UNIVARIATE procedure in the Base SAS Procedures Guide , Volume 3.
MU=
specifies the mean. If not specified, it is calculated from the data.
SIGMA=
specifies the standard deviation. If not specified, it is calculated from the data.
MAXPOINTS=
specifies the maximum number of points generated for the curve.
Default: 200

Options

Statement Option
Description
Specifies a label for the density curve.
Specifies the color and font attributes of the density curve label.
Specifies the location of the curve label relative to the plot area.
Specifies the position of the density curve label relative to the curve line.
Specifies the degree of the transparency of the density curve.
Specifies a numeric column that provides frequencies for each observation read.
Specifies the label of the legend.
Specifies the properties of the density curve.
Assigns a name to a plot statement for reference in other template statements.
Specifies the orientation of the Y axis.
Specifies that the data columns for this plot and the plot type be used for determining default axis features.
Specifies display formats for the tooltips that are displayed when the cursor is positioned over the density curve.
Specifies display labels for the tooltips that are displayed when the cursor is positioned over the density curve.
Specifies whether data are mapped to the primary X (bottom) axis or the secondary X2 (top) axis.
Specifies whether data are mapped to the primary Y (left) axis or the secondary Y2 (right) axis.
CURVELABEL="string"
specifies a label for the density curve.
Default: no default
The font and color attributes for the label are specified by the CURVELABELATTRS= option.
CURVELABELATTRS=style-element | style-element (text-options) | (text-options)
specifies the color and font attributes of the density curve label. See General Syntax for Attribute Options for the syntax on using a style-element and Text Options for available text-options.
Default: The GraphValueText style element.
Interaction: For this option to take effect, the CURVELABEL=curvelabel option must also be used.
CURVELABELLOCATION=INSIDE | OUTSIDE
specifies the location of the density curve label relative to the plot area.
Default: INSIDE
INSIDE
inside the plot area
OUTSIDE
outside the plot area
Restriction: OUTSIDE cannot be used when the DENSITYPLOT is used in multi-cell layouts such as LATTICE, DATAPANEL, or DATALATTICE, where axes can be external to the grid.
Interaction: This option is used in conjunction with the CURVELABELPOSITION= option to determine where the curve label appears. For more information, see Location and Position of Curve Labels.
CURVELABELPOSITION=AUTO | MAX | MIN | START | END
Specifies the position of the density curve label relative to the curve line.
Default: AUTO when CURVELABELLOCATION=OUTSIDE. END when CURVELABELLOCATION=INSIDE.
AUTO
Only used when CURVELABELPOSITION=OUTSIDE. The density label is positioned automatically near the end of the density curve along unused axes whenever possible (typically Y2 or X2) to avoid collision with tick values.
MAX
Forces the density label to appear near maximum density X- values (typically, to the right).
MIN
Forces the density label to appear near minimum density X- values (typically, to the left).
START
Only used when CURVELABELLOCATION=INSIDE. Forces density label to appear near the beginning of the curve. Particularly useful when the curve line has a spiral shape.
END
Only used when CURVELABELLOCATION=INSIDE. Forces density label to appear near the end of the curve. Particularly useful when the curve line has a spiral shape.
Restriction: The AUTO setting is not supported if CURVELABELLOCATION=INSIDE is specified. The START and END settings are not supported if CURVELABELLOCATION=OUTSIDE is specified.
Interaction: For this option to take effect, the CURVELABEL= option must also be specified.
Interaction: This option is used in conjunction with the CURVELABELLOCATION= option to determine where the density label appears. For more information, see Location and Position of Curve Labels.
DATATRANSPARENCY=number
specifies the degree of the transparency of the density curve.
Default: 0
Range: 0 (opaque) to 1 (entirely transparent)
FREQ=numeric-column | expression
specifies a numeric column that provides frequencies for each observation read. If n is the value of the numeric-column for a given observation, then that observation is used n times in computing the density curve.
Default: Each observation is counted once.
Restriction: If the value of the numeric-column is missing or is less than 1, the observation is not used in the analysis. If the value is not an integer, only the integer portion is used.
LEGENDLABEL= "string"
specifies a label to be used in a legend.
Default: The string specified on the NAME= option.
LINEATTRS=style-element | style-element (line-options) | (line-options)
specifies the attributes of the density curve. See General Syntax for Attribute Options for the syntax on using a style-element and Line Options for available line-options.
Default: The GraphFitLine style element.
NAME="string"
assigns a name to a plot statement for reference in other template statements.
Default: no default
Restriction: The string is case sensitive, cannot contain spaces, and must define a unique name within the template.
Interaction: The string is used as the default legend label if the LEGENDLABEL= option is not used.
The specified name is used primarily in legend statements to coordinate the use of colors and line patterns between the graph and the legend.
ORIENT=VERTICAL | HORIZONTAL
specifies the orientation of the Y axis.
Default: VERTICAL
PRIMARY=boolean
specifies that the data columns for this plot and the plot type be used for determining default axis features.
Default: FALSE
Restriction: This option is ignored if the plot is placed under a GRIDDED or LATTICE layout block.
Details: This option is needed only when two or more plots within an overlay-type layout contribute to a common axis. For more information, see When Plots Share Data and a Common Axis
TIPFORMAT=(role-format-list)
specifies display formats for the tooltips that are displayed when the cursor is positioned over the density curve.
Default: The column format of the variable assigned to the role or BEST6. if no format is assigned to a numeric column.
(role-format-list)
a list of role-name = format pairs separated by blanks.
Use role-name X to format the tooltip values that map to the values along the X axis, and role-name Y to format the tooltip values that map to the values along the Y axis.
  TIPFORMAT=(Y=6.2) 
Tip: This option provides a way to control the formats of columns that appear in tooltips.
TIPLABEL=(role-label-list)
specifies display labels for the tooltips that are displayed when the cursor is positioned over the density curve.
Default: The column label or column name of the variable assigned to the role.
(role-label-list)
a list of role-name = "string" pairs separated by blanks.
Use role-name X to label the tooltip values that map to the values along the X axis, and role-name Y to label the tooltip values that map to the values along the Y axis.
   TIPLABEL=(Y="Normal Approximation")
Tip: This option provides a way to control the labels of columns that appear in tooltips.
XAXIS=X | X2
specifies whether data are mapped to the primary X (bottom) axis or to the secondary X2 (top) axis.
Default: X
Interaction: The overall plot specification and the layout type determine the axis display for the specified axis. For more information, see How Axis Features are Determined.
YAXIS=Y | Y2
specifies whether data are mapped to the primary Y (left) axis or to the secondary Y2 (right) axis.
Default: Y
Interaction: The overall plot specification and the layout type determine the axis display for the specified axis. For more information, see How Axis Features are Determined.