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;
run;

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

Statement Summary

The input data for the BARCHART statement is raw, unsummarized input data, and the statement calculates appropriate summarization statistics (sum, mean, and so on).
By default, if the data values are character, the bars in the chart appear in the order in which the X values are present in the input data. If the data values are character, the values are presented in ascending order. For non-grouped data, duplicated values of X are summarized into a unique value. All values are treated as discrete. For grouped data, the values of X are summarized as needed (see the GROUP= option).
The BARCHART statement only performs discrete binning for a numeric X. Use HISTOGRAM to create a chart with interval binning for X.

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 properties of the line connecting bars.
Specifies the degree of the transparency of the bar fill, bar outline, error bars, 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.
Creates a separate segment within each bar for each unique group value of the specified column.
Specifies the sorting order of grouped bars.
Specifies indices for mapping line attributes (color and pattern) to one of the GraphData1 - GranphDataN style elements.
Specifies the label of 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 information to display when the cursor is positioned over a bar.
Specifies display formats for information defined by roles.
Specifies display labels for information defined by 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.2 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 (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.
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.
DATATRANSPARENCY=number
specifies the degree of the transparency of the bar fill, bar outline, error bars, and connect line, if displayed.
Default: 0
Range: 0 (opaque) to 1 (entirely transparent)
DISCRETEOFFSET=number
specifies an amount to offset all bars from the category midpoints.
Note: This feature is for the third maintenance release of SAS 9.2 and later.
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, and error bars
(display-options)
a list of options, enclosed in parentheses, that must include one of the following:
OUTLINE—displays outlined bars
FILL—displays filled bars
CONNECT—Displays line segments connecting adjacent midpoints at the end of each bar
Restriction: Connect lines are not drawn for grouped data.
Use the OUTLINEATTRS= and FILLATTRS= options to control the appearance of the bars. Use CONNECTATTRS= to control the appearance of the connect lines.
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.
GROUP=column | expression
creates a separate bar segment for each unique group value of the specified column. For example, the sashelp.cars data used in the Example Program contains a column named ORIGIN, which identifies the region that produces each car. If this option were used on the example program’s BARCHART statement, as shown in the following code, the bars in the chart would display a separate bar segment to represent the regions that manufactured each type of car:
      layout overlay;
        barchart x=type y=mpg_highway /
          stat=mean orient=horizontal
          group=origin name="grouped";
        discretelegend "grouped";
      endlayout;
Bar Chart with Grouped Bars
Default:
  • If bar fills are enabled by the ODS style or the DISPLAY= option, each distinct group value is represented in the graph by a different fill color defined by the GraphData1:Color - GraphDataN:Color 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 outlined color defined by the GraphData1:ContrastColor - GraphDataN:ContrastColor style references.
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.
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.
GROUPORDER=DATA | ASCENDING | DESCENDING
specifies the ordering of bars within a group and within 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.
DESCENDING
shows each group of bars in descending order.
Interaction: This option is ignored unless GROUP= is specified.
INDEX=positive-integer-column | expression
specifies indices for mapping line attributes (color and pattern) to one of the GraphData1 - GranphDataN style elements.
Default: no default
Restriction: 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 use in a legend.
Default: The Y-variable label. If a label is not defined, the Y-variable name.
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
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.
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.
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 tip columns.
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 tip columns.
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.