Creating a Graph Template

Quick Look at a GTL Graph Definition

To illustrate the steps needed to create a graph, assume that we want to produce a graph showing a linear regression fit for a set of data where HEIGHT is an independent variable and WEIGHT is a dependent variable.
Regression Fit Plot
proc template;
 define statgraph modelfit;
  begingraph;
   entrytitle "Regression Fit Plot";
   layout overlay;
     scatterplot x=height y=weight;
     regressionplot x=height y=weight;
   endlayout;
  endgraph;
 end;
run;

proc sgrender data=sashelp.class 
              template=modelfit;
run;
You can submit this program to produce the graph. Let us now look in more detail into what this program does.

More Detailed Look at a GTL Graph Definition

The TEMPLATE procedure can produce different kinds of templates, like STYLE, TABLE, COLUMN, and STATGRAPH. The type of template to be created is specified with a DEFINE statement.
PROC TEMPLATE DEFINE Statement
The DEFINE STATGRAPH statement and its matching END statement indicate that a graphics template named MODELFIT is to be created. The template name can be a simple one-level name or a multi-level name such as GRAPHS.MODELFIT or PROJECT.STUDY3.MODELFIT indicating a folders where the MODELFIT template is to be stored.
Graph Block in a Template
The BEGINGRAPH statement and its matching ENDGRAPH statement define the outermost container for the graph. It supports options for sizing the graph. Within this block you can use various statements that define the content of the graph.
ENTRYTITLE and ENTRYFOOTNOTE statements can be used to specify graph title lines and graph footnote lines, if needed.
Layout Block in a Template
The LAYOUT OVERLAY statement and its matching ENDLAYOUT statement define the type of graphical layout to be used. The OVERLAY layout allows the contained plots to be overlaid. It manages the plot layers and queries all contained plots to decide the axis types, axis labels, and axis ranges.
Graph Content in a Template
Both the SCATTERPLOT and REGRESSIONPLOT statements specify HEIGHT for the X variable and WEIGHT for the Y variable. For the regression, X is always used for the independent variable and Y for the dependent variable. By default, a linear regression is used.
For more information on the types of layouts and plots in GTL, see Overview of Basic Statements and Options.

Compiling the Template

When you submit your PROC TEMPLATE statements, the template syntax is checked.
Template Syntax is Checked
If no syntax error is detected, a compiled template named MODELFIT is created and stored physically in the SASUSER.TEMPLAT item store by default. This item store is chosen by default because it is the first item store that can be updated in the current ODS path.
It should be noted that STATGRAPH template syntax requires that any necessary arguments be specified (X= and Y= arguments are required for both the SCATTERPLOT and REGRESSIONPLOT statements), but no checking for the existence of the assigned variables is performed at compile time. Also note that no reference to an input data set appears in the template.
Compiling the template does not produce a graph—it only creates a compiled template that can be executed to produce a graph.
Templates Window Provides Access to a Template
To verify that the template was created, you can issue the ODSTEMPLATE command (ODST, for short). This opens the Templates window where you view all item stores and their contents. All STATGRAPH templates can be identified by the common icon shown above.
You can also browse the source for any compiled template by double-clicking on its name.
Template Browser
For more information about item stores and PROC TEMPLATE in general, see the SAS Output Delivery System: User's Guide in the Base documentation.