Example Program and Statement Details

Example Graph

The following graph was generated by the Example Program:
Results of a Graph Defined within a BEGINGRAPH Block

Example Program

The BEGINGRAPH statement block is a required outermost container for any graph template. One of its purposes is to support options that apply to the entire graph. For example, the default graph size that a template produces is typically 640x480 pixels. If you need a different size, you can declare the alternative size on this statement. To do so, use the DESIGNWIDTH= option, or the DESIGNHEIGHT= option, or both. This program shows one way to set the width and height of two graph cells to be equal.
proc template;
  define statgraph begingraph;
    dynamic XVAR YVAR;
    begingraph / designwidth=640px designheight=320px;
    layout lattice / columns=2;
      layout overlayequated / equatetype=square;
        entry "Linear Regression Fit" /
          valign=top textattrs=(weight=bold);
        scatterplot x=XVAR y=YVAR / datatransparency=.5;
        regressionplot x=XVAR y=YVAR;
      endlayout;
      layout overlayequated / equatetype=square;
        entry "Loess Fit" /
          valign=top textattrs=(weight=bold);
        scatterplot x=XVAR y=YVAR / datatransparency=.5;
        loessplot x=XVAR y=YVAR;
      endlayout;
    endlayout;
    endgraph;
  end;
run;

proc sgrender data=sashelp.iris template=begingraph;
  dynamic title="Square Plot"
    xvar="SepalLength" yvar="SepalWidth";
run;

Statement Summary

All template definitions in the Graphics Template Language must start with a BEGINGRAPH statement and conclude with an ENDGRAPH statement.
Within a BEGINGRAPH block, one and only one GTL layout block is required. It can be a LATTICE, GRIDDED, OVERLAY, OVERLAYEQUATED, OVERLAY3D, REGION, DATALATTICE, or DATAPANEL layout block. This layout block can contain other nested layout blocks and should contain at least one plot statement.
The GTL global statements apply to the entire and can include such statements as title and footnote statements, attribute maps, draw statements, conditional statements, and so on. Any of these global statements can precede or follow the GTL layout block.
By default, graphs are rendered at 640px by 480px (4:3 aspect ratio). To change the output size for a single graph, use the DESIGNWIDTH= and DESIGNHEIGHT= options in the BEGINGRAPH statement for that graph. For example, the template in the Example Program uses DESIGNHEIGHT= to change the graph height to 320px. To prevent the graph width from automatically scaling to preserve the 4:3 aspect ratio, it uses DESIGNWIDTH= to maintain the 640px width. In this instance, the setting renders each graph cell as a 320px by 320px square. (The cells are square in this case, but the resulting cell size depends on the graph definition and would not be the same for all graphs.)
Note: To change the graph sizes for all templates in the current SAS session, you can use the WIDTH= and HEIGHT= options in the ODS GRAPHICS statement. Size settings in the ODS GRAPHICS statement override size settings in the BEGINGRAPH statement and remain in effect unless they are changed in another ODS GRAPHICS statement. You can also use WIDTH= and HEIGHT= settings in the graph style to modify the graphs sizes across template definitions. Be aware, however, that if you explicitly manage the graph output size, the graph elements might be scaled so that the size specification is honored.
The following template defines a square graph (equal height and width, 1:1 aspect ratio) by setting the design width equal to the internal default height (480px). The setting is made with DESIGNWIDTH=DEFAULTDESIGNHEIGHT:
Note: A “square graph” means that the output graph’s width and height are equal. That does not imply that the X and Y axis lengths are equal if the graph contains only one cell.
proc template;
  define statgraph squareplot;
    dynamic title xvar yvar;
    begingraph / designwidth=defaultDesignHeight;
      entrytitle title;
      layout overlayequated / equatetype=square;
        scatterplot x=xvar y=yvar;
        regressionplot x=xvar y=yvar;
      endlayout;
    endgraph;
  end;
run;
If this template were executed with the following GRENDER procedure statement, a 480px by 480px graph would be created:
proc sgrender data=mydata template="squareplot" ;
dynamic title="Square Plot" xvar="time1" yvar="time2";
run;
If the ODS GRAPHICS statement’s WIDTH= or HEIGHT= options change the render width or render height, the squareplot template’s 1:1 aspect ratio would still be honored. Thus, both of the following GRENDER procedure statements would create a 550px by 550px graph:
ods graphics / width=550px;
proc sgrender data=mydata template="squareplot" ;
dynamic title="Square Plot" xvar="time1" yvar="time2";
run;

ods graphics / height=550px;
proc sgrender data=mydata template="squareplot" ;
dynamic title="Square Plot" xvar="time1" yvar="time2";
run;

Options

Statement Option
Description
Specifies the color of the graph background.
Specifies whether a border is drawn around the graph.
Specifies the properties of the border line around the graph.
Specifies the design height of the graph.
Specifies the design width of the graph.
Specifies a global drawing space and drawing units for all of the draw statements within this BEGINGRAPH block.
Specifies whether missing values are displayed on a discrete axis.
Specifies the amount of extra space that is added inside the graph border.
BACKGROUNDCOLOR=style-reference | color
specifies the color of the graph background.
Default: The GraphBackground:Color style reference.
style-reference
A reference of the form style-element:style-attribute. Only the style attribute named COLOR is used.
BORDER=boolean
specifies whether a border is drawn around the graph.
Default: The ODS GRAPHICS statement BORDER= option setting, which is TRUE by default.
Interaction: If this option is set to FALSE, the BORDERATTRS= option is ignored.
BORDERATTRS=style-element | style-element (line-options) | (line-options)
specifies the attributes of the border line around the graph. See General Syntax for Attribute Options for the syntax on using a style-element and Line Options for available line-options.
Default: The GraphBorderLines style element.
Interaction: BORDER=TRUE must be set for this option to have any effect.
DESIGNHEIGHT=DEFAULTDESIGNHEIGHT | dimension
specifies the design height of the graph.
Default: DEFAULTDESIGNHEIGHT. This value is obtained from the SAS Registry key Productsthen selectGraphthen selectODSthen selectStatGraphthen selectDefaultDesignHeight when the graph is rendered. The initial value of this registry key is 640px.
Restriction: The minimum dimension value that you can set is 2 pixels. If a smaller setting is specified, the default design height is used.
Interaction: This height can be overridden at run time with a render height that is specified with the HEIGHT= option in the ODS GRAPHICS statement (external to the template). Also, the ODS destination statement’s IMAGE_DPI= option can affect the height.
The value of the DefaultDesignHeight registry key can be changed. Doing so would affect the design height of all templates that do not include an explicit dimension for the design height. Also, changing the height setting in the graph style would affect the height of all templates that use that style.
DESIGNWIDTH=DEFAULTDESIGNWIDTH | dimension
specifies the design width of the graph.
Default: DEFAULTDESIGNWIDTH. This value is obtained from the SAS Registry key Productsthen selectGraphthen selectODSthen selectStatGraphthen selectDefaultDesignWidth when the graph is rendered. The initial value of this registry key is 480px.
Restriction: The minimum dimension value that you can set is 2 pixels. If a smaller setting is specified, the default design width is used.
Interaction: This width can be overridden at run time with a render width that is specified with the WIDTH= option in the ODS GRAPHICS statement (external to the template). Also, the ODS destination statement’s IMAGE_DPI= option can affect the width.
The value of the DefaultDesignWidth registry key can be changed. Doing so would affect the design width of all templates that do not include an explicit dimension for the design width. Also, changing the width setting in the graph style would affect the width of all templates that use that style.
DRAWSPACE= GRAPHPERCENT | GRAPHPIXEL | LAYOUTPERCENT | LAYOUTPIXEL | WALLPERCENT | WALLPIXEL | DATAPERCENT | DATAPIXEL | DATAVALUE
specifies a global drawing space and drawing units for all of the draw statements within this BEGINGRAPH block.
Default: LAYOUTPERCENT
Tip: Individual draw statements within this BEGINGRAPH block can override this global setting.
INCLUDEMISSINGDISCRETE=boolean
specifies whether missing values are displayed on a discrete axis.
Default: FALSE
PAD=dimension | (pad-options)
specifies the amount of extra space that is added inside the graph border.
Default: The default padding for all sides is 10. Values without units are in pixels (px). A unit must be provided if other than pixels.
dimension
Specifies a dimension to use for the extra space at the left, right, top, and bottom of the layout border.
(pad-options)
Enables separate settings for the left, right, top, and bottom padding dimensions. Use the pad-options to create non-uniform padding. These options must be enclosed in parentheses. Each option is specified as a name = value pair. Sides not assigned padding are padded with the default amount.
Values without units are in pixels (px). A unit must be provided if other than pixels.
LEFT=dimension specifies the amount of extra space added to the left side.
RIGHT=dimension specifies the amount of extra space added to the right side.
TOP=dimension specifies the amount of extra space added to the top.
BOTTOM=dimension specifies the amount of extra space added to the bottom.