Example Program and Statement Details

Example Graph

The following graph was generated by the Example Program:
Example Bar Chart

Example Program

proc template;
  define statgraph barchart;
    begingraph;
      entrytitle "Average Mileage by Vehicle Type";
      layout overlay;
        barchart x=type  y=mpg_highway /
          stat=mean orient=horizontal;
      endlayout;
    endgraph;
  end;

proc sgrender data=sashelp.cars template=barchart;
run;

Statement Summary

The BARCHART statement takes raw, unsummarized data as input and calculates appropriate summarization statistics (sum, mean, and so on).
By default, if the category column is character, the bars in the chart appear in the order in which the category values are present in the input data. If the category column is numeric, the values are presented in ascending order. For non-grouped data, duplicated category values are summarized into a unique value. All values are treated as discrete. For grouped data, the category values are summarized as needed. (See the GROUP= option.)
The BARCHART statement creates a bar chart with bars that represent summarized response values that are categorized by the unique category values. To create a chart that represents response values along an interval axis, use the HISTOGRAM statement.

Arguments

X=column | expression
specifies the column or expression for the category values. This argument is required.
Y=numeric-column | expression
specifies the numeric column or expression for the response values. This argument is optional.

Options

Statement Option
Description
Specifies whether the bar statistic value is displayed at the end of the bar.
Specifies the text properties of the bar label text.
Specifies the text format used to display the bar label.
Specifies the bar width as a ratio of the maximum width.
Specifies the response axis intercept for the baseline.
Specifies the width of the group clusters as a fraction of the midpoint spacing.
Specifies the properties of the line connecting bars.
Enhances the visual appearance of the filled bars.
Specifies the degree of the transparency of the bar fill, bar outline, and connect line, if displayed.
Specifies an amount to offset all bars from the category midpoints.
Specifies which bar features are displayed.
Specifies the appearance of the filled bar area.
Specifies the appearance of the pattern-filled bar area
Creates a separate segment or bar for each unique group value of the specified column.
Specifies how to display grouped bars.
Specifies the order of the grouped bars for each category value.
Specifies whether missing values of the group variable are included in the plot.
Specifies indices for mapping line attributes (color and pattern) to one of the GraphData1–GranphDataN style elements.
Specifies the label for the legends.
Assigns a name to a plot statement for reference in other template statements.
Specifies the orientation of the Y axis and the bars.
Specifies the properties of the bar outlines.
Specifies that the data columns for this chart and the chart type be used for determining default axis features.
Specifies the statistic to be computed for the Y-axis.
Specifies the target value for each bar.
Specifies the information to display when the cursor is positioned over a bar.
Specifies display formats for the information that is defined by the tooltip roles.
Specifies display labels for the information that is defined by the tooltip roles.
Specifies an HTML page to display when a bar is selected.
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.
BARLABEL=boolean
specifies whether the bar statistic value is displayed at the end of the bar.
Default: FALSE
Tip: The font and color attributes for the label are specified by the BARLABELATTRS= option. The text format is specified by the BARLABELFORMAT= option.
BARLABELATTRS=style-element | style-element (text-options) | (text-options)
specifies the text properties of the bar label text. See General Syntax for Attribute Options for the syntax on using a style-element and Text Options for available text-options.
Default: The GraphDataText style element.
Interaction: For this option to take effect, BARLABEL=TRUE must be specified.
BARLABELFORMAT=format
specifies the text format used to display the bar label.
Default: The column format assigned to the Y= variable or BEST6. if no format is assigned.
Interaction: For this option to take effect, BARLABEL=TRUE must be specified.
BARWIDTH=number
specifies the width of a bar as a ratio of the maximum possible width.
Default: .85. By default, the bar width automatically adjusts based on the number of bars to be displayed and the wall width.
Range: 0.1 (narrowest) to 1 (widest)
This option is needed only to change the default behavior. For example, to remove any inter-bar gap, set BARWIDTH=1.
BASELINEINTERCEPT=number
specifies the response axis intercept for the baseline.
Default: 0
Interaction: Label positions are adjusted to prevent the labels from overlapping.
Interaction: If GROUPDISPLAY=STACKED is specified, this option is ignored and the baseline is not displayed.
Tip: The baseline does not add a tick or a tick value to the axis. To label the baseline, you can use a REFERENCELINE statement to overlay a line with the same X or Y value and use the REFERENCELINE statement’s CURVELABEL= option.
Details: When this option is used, the axis range is adjusted to include the baseline, and the baseline is placed at the specified value on the response axis.
Graph with BASLINEINTERCEPT=0
CLUSTERWIDTH= number
specifies the width of the group clusters as a fraction of the midpoint spacing on a discrete axis or a fraction of the minimum interval between adjacent data values on an interval axis.
Default: 0.85
Range: 0.1 (narrowest) to 1 (widest)
Interaction: For this option to take effect, the GROUP= option must also be specified, and the GROUPDISPLAY= option must be set to CLUSTER.
Note: When GROUPDISPLAY=CLUSTER, the default BARWIDTH is 1.0.
Clusters in a Bar Chart
CONNECTATTRS=style-element | style-element (line-options) | (line-options)
specifies the appearance of the bar connect lines. See General Syntax for Attribute Options for the syntax on using a style-element and Line Options for available line-options.
Default: The GraphConnectLine style element.
DATASKIN=NONE | PRESSED | SHEEN | CRISP | GLOSS | MATTE
enhances the visual appearance of the filled bars.
Bar Chart with Data Skins
Default: NONE
Requirement: For this option to have any effect, the fill must be enabled by the ODS style or the DISPLAY= option.
Interaction: The data skin appearance is based on the FILLATTRS= color.
Interaction: When a data skin is applied, all bar outlines are set by the skin, and the OUTLINEATTRS= option is ignored.
DATATRANSPARENCY=number
specifies the degree of the transparency of the bar fill, bar outline, and connect line, if displayed.
Default: 0
Range: 0 (opaque) to 1 (entirely transparent)
Tip: The FILLATTRS = option can be used to set transparency for just the filled bar area. You can combine this option with FILLATTRS= to set one transparency for the bar outlines and connect lines but a different transparency for the bar fills. Example:
datatransparency=0.2 fillattrs=(transparency=0.6)
DISCRETEOFFSET=number
specifies an amount to offset all bars from the category midpoints.
Default: 0 (no offset, all bars are centered on the category midpoints)
Range: -0.5 to +0.5, where .5 represents half the distance between category ticks. Normally, a positive offset is to the right when ORIENT=VERTICAL, and up when ORIENT=HORIZONTAL. (If the layout's axis options set REVERSE=TRUE, then the offset direction is also reversed.)
Details: This feature is useful for graphing multiple response variables side by side on a common axis. By default within an overlay-type layout, if multiple BARCHART statements are used with different response variables, the bars for matching category values are centered on the midpoints and the bars are superimposed. To make it easier to distinguish among superimposed bars, you can assign a different BARWIDTH= setting to each BARCHART statement in the overlay:
layout overlay / cycleattrs=true
    xaxisopts=(display=(tickvalues))
    yaxisopts=(label="Revenue" offsetmax=0.2);

  barchart x=year y=A_revenue / stat=sum name="A"
    legendlabel="A" barwidth=0.8 ;
  barchart x=year y=B_revenue / stat=sum name="B"
    legendlabel="B" barwidth=0.6 ;
  barchart x=year y=C_revenue / stat=sum name="C"
    legendlabel="C" barwidth=0.4 ;

  discretelegend "A" "B" "C" / title="Product:"
    location=inside halign=right valign=top;
endlayout;
Bar Chart with Bars Centered on Midpoints
To place the different response values side by side, you can assign a different offset to each BARCHART statement. If desired, the BARWIDTH= option can be used in conjunction with DISCRETEOFFSET= to create narrower bars that require less width within the plot area:
layout overlay / cycleattrs=true
    xaxisopts=(display=(tickvalues))
    yaxisopts=(label="Revenue" offsetmax=0.2);

  barchart x=year y=A_revenue / stat=sum name="A"
    legendlabel="A"
    discreteoffset=-0.3 barwidth=0.3 ;
  barchart x=year y=B_revenue / stat=sum name="B"
    legendlabel="B"
    discreteoffset=0    barwidth=0.3 ;
  barchart x=year y=C_revenue / stat=sum name="C"
    legendlabel="C"
    discreteoffset=+0.3 barwidth=0.3 ;

  discretelegend "A" "B" "C" / title="Product:"
    location=inside halign=right valign=top;
endlayout;
Bar Chart with Midpoint Offsets
Different combinations of DISCRETEOFFSET and BARWIDTH can be used to get the effect that you want. Gaps can be created between bars by providing a narrower bar width. Or, bars can be overlapped if the bar widths are increased in proportion to the discrete offset.
layout overlay / cycleattrs=true 
    xaxisopts=(display=(tickvalues))
    yaxisopts=(label="Revenue" offsetmax=0.2);

  barchart x=year y=A_revenue / stat=sum name="A"
    legendlabel="A" datatransparency=0.2
    discreteoffset=-0.2 barwidth=0.5 ;
  barchart x=year y=B_revenue / stat=sum name="B"
    legendlabel="B" datatransparency=0.2
    discreteoffset=0   barwidth=0.5 ;
  barchart x=year y=C_revenue / stat=sum name="C"
    legendlabel="C" datatransparency=0.2
    discreteoffset=+0.2 barwidth=0.5 ;

  discretelegend "A" "B" "C" / title="Product:"
    location=inside halign=right valign=top;
endlayout;
Bar Chart with Overlapping Bars
DISPLAY=STANDARD | ALL | (display-options)
specifies which bar features to display.
Default: STANDARD
STANDARD
displays outlined, filled bars
ALL
displays outlined, filled bars, and connect lines
(display-options)
a list of options, enclosed in parentheses, that must include one of the following:
OUTLINE Displays outlined bars.
FILL Displays bars with a solid fill.
CONNECT Displays line segments connecting adjacent midpoints at the end of each bar
FILLPATTERN Displays bars with a patterned fill. This setting is used primarily for grouped bar charts that must be rendered in monochrome for use in a journal article. The fill patterns make it easier to distinguish among groups when color is not available.
Restriction: Connect lines are not drawn for grouped data.
Use the OUTLINEATTRS=, FILLATTRS=, and FILLPATTERNATTRS= options to control the appearance of the bars. Use CONNECTATTRS= to control the appearance of the connect lines.
Tip: Both FILL and FILLPATTERN can be specified to combine solid fills and pattern fills in the bars.
FILLATTRS=style-element | style-element (fill-options) | (fill-options)
specifies the appearance of the filled bar area. See General Syntax for Attribute Options for the syntax on using a style-element and Fill Options for available fill-options.
Default:
  • For non-grouped data, the GraphDataDefault:Color style reference.
  • For grouped data, the Color attribute of GraphData1–GraphDataN style elements.
Tip: The DATATRANSPARENCY= option sets the transparency for the bar fills, bar outlines, and connect lines. You can combine this option with DATATRANSPARENCY= to set one transparency for the bar outlines and connect lines but a different transparency for the bar fills. Example:
datatransparency=0.2 fillattrs=(transparency=0.6)
FILLPATTERNATTRS=style-element | style-element (fill-options) | (fill-pattern-options)
specifies the appearance of the pattern-filled bar area.
Default: no default
To specify a style-element, only one of the elements GraphData1–GraphDataN can be used, subject to the Restriction listed in a moment.
The following fill-pattern-options are available:
COLOR = color | style-reference
specifies a color to use for the bar-fill-pattern lines. With grouped data, this setting has the effect of holding the pattern line color constant across all group values.
PATTERN = line-pattern
specifies a line pattern to use for the bar fill.
To specify a line-pattern, combine a line-direction prefix (R for right, L for left, and X for cross hatch) with a line-identification number:
Bar Chart Fill Patterns
With grouped data, this PATTERN= setting has the effect of holding the fill pattern constant across all group values.
Restriction: The only SAS-delivered styles that support fill patterns are JOURNAL2, JOURNAL3, and MONOCHROMEPRINTER. If any other SAS-delivered style is in effect and this option uses style-element in its specification, this option is ignored.
Interaction: For this option to take effect, the DISPLAY= option must include FILLPATTERN among the display options.
GROUP=column | discrete-attr-var | expression
creates a separate bar segment or bar for each unique group value of the specified column.
discrete-attr-var
specifies a discrete attribute variable that is defined in a DISCRETEATTRVAR statement.
Restriction: A discrete attribute variable specification must be a direct reference to the attribute variable. It cannot be set by a dynamic variable.
For example, the sashelp.cars data used in the Example Program contains a column named ORIGIN, which identifies the region that produces each car. This column could be used in the BARCHART statement to group the bars in the display (see the GROUPDISPLAY= option to see the output for the grouped bars):
layout overlay;
  barchart x=type y=mpg_highway /
    stat=mean group=origin name="b";
  discretelegend "b" / title="Regions:";
endlayout;
Default:
  • If bar fills or fill patterns are enabled by the ODS style or the DISPLAY= option, each distinct group value is represented in the graph by a different fill color or fill pattern. The fill colors are defined by the GraphData1:Color–GraphDataN:Color style references. The fill patterns are defined by the GraphData1:FillPattern–GraphDataN:FillPattern style references.
  • If bar outlines are enabled by the ODS style or the DISPLAY= option, each distinct group value is represented in the graph by a different outline. The outline colors are defined by the GraphData1:ContrastColor–GraphDataN:ContrastColor style references.
Interaction: The bar display depends on the setting for the GROUPDISPLAY= option.
Interaction: Connect lines are not drawn for grouped data.
Interaction: By default, the group values are mapped in the order of the data. The GROUPORDER= option can be used to control the sorting order of the grouped bar segments. The INDEX= option can be used to alter the default sequence of colors and line patterns.
Interaction: The INCLUDEMISSINGGROUP option controls whether missing group values are considered a distinct group value.
Tip: The representations that are used to identify the groups can be overridden individually. For example, each distinct group value is represented by a different line pattern for the bar outlines, but the PATTERN= setting on the OUTLINEATTRS= option could be used to assign the same line pattern to all bar outlines and connect lines.
GROUPDISPLAY=STACK | CLUSTER
specifies how to display grouped bars.
Default: STACK
STACK
Displays group values as stacked segments within the category bar.
Bar Chart with Stacked Bars
CLUSTER
Displays group values as separate adjacent bars that replace the single category bar. Each set of group values is centered at the category midpoint on the axis. This example illustrates the clusters and also how groups are displayed when they have an unequal number of unique values.
Bar Chart with Clustered Bars
GROUPORDER=DATA | ASCENDING | DESCENDING
specifies the order of the grouped bars for each category value. It also sets the default order of the groups in the legend.
Default: DATA
DATA
shows each group of bars in data order of the group column.
ASCENDING
shows each group of bars in ascending order of the group column.
DESCENDING
shows each group of bars in descending order of the group column.
Interaction: This option is ignored unless GROUP= is specified.
Note: Attributes such as color, symbol, and pattern are assigned to each group in the DATA order by default regardless of the GROUPORDER= option setting.
Note: The ASCENDING and DESCENDING settings linguistically sort the group values within each category (or X value) for display position purposes only. The data order of the observations and the visual attributes that are assigned to the group values remain unchanged.
INCLUDEMISSINGGROUP=boolean
specifies whether missing values of the group variable are included in the plot.
Default: TRUE
Interaction: For this option to take effect, the GROUP= option must also be specified.
Tip: Unless a discrete attribute map is in effect or the INDEX= option is used, the attributes of the missing group value are determined by the GraphMissing style element except when the MISSING= system option is used to specify a non-default missing character or when a user-defined format is applied to the missing group value. In those cases, the attributes of the missing group value are determined by a GraphData1–GraphDataN style element.
INDEX=numeric-column | expression
specifies indices for mapping line attributes (color and pattern) to one of the GraphData1 - GranphDataN style elements.
Default: no default
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.
Interaction: For this option to take effect, the GROUP= option must also be specified.
Interaction: All of the indexes for a specific group value must be the same. Otherwise, the results are unpredictable.
Interaction: The index values are 1-based indices. For the style definitions in GraphData1–GraphDataN, if the index value is greater than N, then a modulo operation remaps that index value to a number less than N to determine which style to use.
Interaction: If this option is not used, then the group values are mapped in the order of the data.
Discussion: Indexing can be used to collapse the number of groups that are represented in a graph. For more information, see Remapping Groups for Grouped Data
LEGENDLABEL= "string"
specifies a label for the legend item that is associated with this plot.
Default: The Y-variable label. If a label is not defined, the Y-variable name.
Restriction: This option applies only to an associated DISCRETELEGEND statement.
Interaction: If the GROUP= option is specified, this option is ignored.
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.
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 and the bars.
Default: VERTICAL
Tip: When this option is set to VERTICAL, the category variable appears on the X (or X2) axis and the response variable appears on the Y (or Y2) axis. To set the axis properties for this chart, you should use the appropriate axis options of the layout container.
When this option is set to HORIZONTAL, the category variable appears on the Y (or Y2) axis and the response variable appears on the X (or X2) axis. To set the axis properties for this chart, you should use the appropriate axis options of the layout container.
Tip: If you change the orientation of the bar chart, you should adjust the layout container’s axis options appropriately.
OUTLINEATTRS=style-element | style-element (line-options) | (line-options)
specifies the appearance of the bar outlines. See General Syntax for Attribute Options for the syntax on using a style-element and Line Options for available line-options.
Default:
  • For non-grouped data, the ContrastColor and LineThickness attributes of the GraphOutlines style element.
  • For grouped data, the ContrastColor and LineThickness attribute of the GraphData1–GraphDataN style elements.
Interaction: For this option to have any effect, outlines must be enabled by the ODS style or the DISPLAY= option.
Interaction: If the DATASKIN= option applies a data skin, this option is ignored.
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.
STAT= FREQ | PCT | SUM | MEAN
specifies the statistic to be computed for the Y-axis.
Default: FREQ for bar charts with no Y=column. SUM for bar charts with a Y= column.
For bar charts with no Y= column:
FREQ
Frequency count
PCT
Percent
For bar charts with a Y= column:
SUM
Sum
MEAN
Mean
When this option is used with the GROUP=group option, the specified statistic is computed for each segment that is created for the unique group values.
TARGET=numeric-column | expression
specifies the target value for each bar. The visual representation is a triangle with a line at the target value.
layout overlay;
  barchart x=type y=mpg_highway / barwidth=.8
    target=mpg_city group=origin groupdisplay=cluster
    name='bar';
  discretelegend 'bar';
endlayout;
Bar Chart with Target Markers
Default: No targets are displayed.
Interaction: For this option to take effect, the Y= argument must also be used.
Interaction: If the GROUP= option is used and GROUPDISPLAY=STACK, this option is ignored.
Tip: The statistic indicated by the STAT= option applies to the TARGET=column. If a constant value is desired for each target, specify it only once for repeated category (X) values (or category and GROUP combinations), and leave other target values missing.
Tip: The color of the target is that of the bar outline.
TIP=(role-list)
specifies the information to display when the cursor is positioned over a bar. If this option is used, it replaces all the information displayed by default.
Default: The columns assigned to these roles are automatically included in the tooltip information: X, Y, and GROUP.
(role-list)
an ordered, blank-separated list of unique BARCHART roles. BARCHART roles include X, Y, INDEX, and GROUP.
The following example displays tooltips for the columns assigned to the roles X and Y only:
  TIP= (X Y)
Requirement: To generate tooltips, you must include an ODS GRAPHICS ON statement that has the IMAGEMAP option specified, and write the graphs to the ODS HTML destination.
Interaction: The labels and formats for the TIP variables can be controlled with the TIPLABEL= and TIPFORMAT= options.
TIPFORMAT=(role-format-list)
specifies display formats for the information that is defined by the tooltip roles.
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.
  TIP=(Y)
  TIPFORMAT=(Y=DOLLAR12.)
Requirement: This option provides a way to control the formats of columns that appear in tooltips. Only the roles that appear in the TIP= option are used. Columns must be assigned to the roles for this option to have any effect.
TIPLABEL=(role-label-list)
specifies display labels for the information that is defined by the tooltip roles.
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.
   TIP=(Y)
   TIPLABEL=(Y="Average Sales")
Requirement: This option provides a way to control the labels of columns that appear in tooltips. Only the roles that appear in the TIP= option are used. Columns must be assigned to the roles for this option to have any effect.
URL=character-column
specifies an HTML page to display when the bar is selected.
Default: no default
character-column
each value of the column should be a valid HTML page reference (HREF). Example: http://www.sas.com/technologies/analytics/index.html.
Requirement: To generate selectable bars, you must include an ODS GRAPHICS ON statement that has the IMAGEMAP option specified, and write the graphs to the ODS HTML destination.
For non-grouped data, the values of the column are expected to be same for each unique X value. If they are not, the results might be unpredictable. The URL value can be blank for some X values, meaning that no action is taken when the bars for those X values are selected. The URL value can be the same for different X values, meaning that the same action is taken when the bars for those X values are selected.
For grouped data, the values of the column are expected to be the same for each unique X and GROUP combination.
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 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: This option is ignored if the Y= argument is not specified.
Interaction: The overall plot specification and the layout type determine the axis display. For more information, see How Axis Features Are Determined.