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 barchartparm;
    begingraph;
      entrytitle "Average Mileage by Vehicle Type";
      entryfootnote halign=left
        "Errorbars show +/- 1 Standard Error";
      layout overlay;
        barchartparm x=type y=mean / 
          errorlower=eval(mean-stderr)
          errorupper=eval(mean+stderr) ;
      endlayout;
    endgraph;
  end;
run;

/* create summarized data for barchartparm */
proc summary data=sashelp.cars nway;
  class type;
  var mpg_highway;
  output out=mileage mean=mean stderr=stderr ;
run;

proc sgrender data=mileage template=barchartparm;
run;

Statement Summary

The input data for the BARCHARTPARM statement is expected to be pre-summarized, with appropriate summarization statistics (sum, mean, and so on) computed for the Y column. When the X values are not unique, the display is not meaningful.
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.

Required Arguments

X=column | expression
specifies the column for the unique category values. All values are treated as discrete.
Y=numeric-column | expression
specifies the column for the response values.

Options

Statement Option
Description
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 attributes of the error bars.
Specifies the values of the lower endpoints on the error bars.
Specifies the values of the upper endpoints on the error bars.
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 roles for information defined by data columns.
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.
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 BARCHARTPARM statement in the overlay:
layout overlay / cycleattrs=true
    xaxisopts=(display=(tickvalues))
    yaxisopts=(label="Revenue" offsetmax=0.2);

  barchartparm x=year y=A_revenue / name="A"
    legendlabel="A" barwidth=0.8 ;
  barchartparm x=year y=B_revenue / name="B"
    legendlabel="B" barwidth=0.6 ;
  barchartparm x=year y=C_revenue / 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 BARCHARTPARM 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);

  barchartparm x=year y=A_revenue / name="A"
    legendlabel="A"
    discreteoffset=-0.3 barwidth=0.3 ;
  barchartparm x=year y=B_revenue / name="B"
    legendlabel="B"
    discreteoffset=0    barwidth=0.3 ;
  barchartparm x=year y=C_revenue / 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);

  barchartparm x=year y=A_revenue / name="A"
    legendlabel="A" datatransparency=0.2
    discreteoffset=-0.2 barwidth=0.5 ;
  barchartparm x=year y=B_revenue / name="B"
    legendlabel="B" datatransparency=0.2
    discreteoffset=0   barwidth=0.5 ;
  barchartparm x=year y=C_revenue / 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 also connect lines
(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
Interaction: Connect lines are not drawn for grouped data.
Error bars are automatically displayed whenever the ERRORUPPER= or ERRORLOWER= options are specified.
Use the OUTLINEATTRS= and FILLATTRS= options to control the appearance of the bars. Use CONNECTATTRS= to control the appearance of the connect lines.
ERRORBARATTRS=style-element | style-element (line-options) | (line-options)
specifies the attributes of the error bars associated with the bars. 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 GraphError style element.
  • For grouped data, the LineStyle and LineThickness attributes of the GraphError style element and the ContrastColor attribute of the GraphData1 - GraphDataN style elements.
ERRORLOWER=numeric-column | expression
specifies the values of the lower endpoints on the Y error bars.
Default: The lower segment of the error bars is not drawn.
Interaction: If the GROUP= option is specified, this option is ignored.
The error bar values must be absolute data values, not data values relative to the value of the bar.
The appearance of the error bars is controlled by the ERRORBARATTRS= option.
ERRORUPPER=numeric-column | expression
specifies the values of the upper endpoints on the Y error bars.
Default: The upper segment of the error bars is not drawn.
Interaction: If the GROUP= option is specified, this option is ignored.
The error bar values must be absolute data values, not data values relative to the value of the bar.
The appearance of the error bars is controlled by the ERRORBARATTRS= option.
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 GraphData1:Color - GraphDataN:Color style references.
GROUP=column | expression
creates a separate bar segment for each unique group value of the specified column. For example, for a vertical bar chart, the segments would be stacked to form the bar, and the height of each segment would represent that group value’s proportional contribution to the response value.
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 chart 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
ROLENAME=( role-name-list )
specifies user-defined roles for information contained in data columns.
Default: no default
(role-name-list)
a blank-separated list of role-name = column pairs.
For example, ROLENAME= ( TIP1=PCT ) assigns the column PCT to the user-defined role TIP1.
Requirement: The role names that you choose must be unique and different from the pre-defined roles X, Y, ERRORLOWER, ERRORUPPER, GROUP, and INDEX.
This option provides a way to add to the data columns that appear in tooltips specified by the TIP= option.
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. Roles for columns that do not contribute to the bar chart can be specified along with roles that do.
Default: The columns assigned to these roles are automatically included in the tooltip information: X, Y, ERRORLOWER, ERRORUPPER, and GROUP.
(role-list)
an ordered, blank-separated list of unique BARCHARTPARM and user-defined roles. BARCHARTPARM roles include X, Y, ERRORUPPER, ERRORLOWER, INDEX, and GROUP.
User-defined roles are defined with the ROLENAME= option.
The following example displays tooltips for the columns assigned to the roles X and Y as well as the column PCT, which is not assigned to any pre-defined bar chart role. The PCT column must first be assigned a role.
  ROLENAME=(TIP1=PCT)
  TIP= (TIP1 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.
  ROLENAME=(TIP1=PCT)
  TIP=(TIP1 X Y)
  TIPFORMAT=( TIP1=PERCENT7.2) 
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. See the ROLENAME= option.
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.
   ROLENAME=(TIP1=PCT) 
   TIP=(TIP1 X Y)
   TIPLABEL=( TIP1="Percent")
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. See the ROLENAME= option.
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, only the first URL value for a given X value is used. 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: The overall plot specification and the layout type determine the axis display. For more information, see How Axis Features are Determined.