set sashelp.class;
height=height*2.54;
weight=weight*.45;
label height="Height in CM" weight="Weight in KG";
file print ods=(template="mygraphs.scatter");
put _ods_;
run;
Initializing Template Dynamic Variables and
Macro Variables
A useful technique for generalizing templates is to define dynamic variables or macro
variables that resolve when the template is executed.
You can create new macro variables or use the automatic macro variables that are
defined in SAS, such as the system date and time value (SYSDATE). Both types of
macro variables must be declared before they can be referenced. Whereas automatic
macro variables do not require initialization, you must initialize any macro variables that
you create with the variable declarations. The macro variable values are obtained from
the current symbol table (local or global), so SAS resolves their values according to the
context in which they are used.
The following template declares the dynamic variables XVAR and YVAR, and the
macro variables STUDY and SYSDATE:
Example Code 25.2 REGFIT Template
proc template;
define statgraph mygraphs.regfit;
dynamic XVAR YVAR;
mvar STUDY SYSDATE;
begingraph;
entrytitle "Regression fit for Model " YVAR " = " XVAR;
entryfootnote halign=left STUDY halign=right SYSDATE;
layout overlay;
scatterplot X=XVAR Y=YVAR;
regressionplot X=XVAR Y=YVAR;
endlayout;
endgraph;
end;
run;
Note the following:
The DYNAMIC statement declares dynamic variables XVAR and YVAR. On the
statements that later execute this template, you must initialize these dynamic
variables by assigning them to variables from the input data source so that they have
values at run time.
The ENTRYTITLE statement concatenates dynamic variables XVAR and YVAR
into a string that will be displayed as the graph title. At run time, the dynamic
variables are replaced by the names of the variables that are assigned to the dynamic
variables when they are initialized.
The SCATTERPLOT and REGRESSIONPLOT statements each reference the
dynamic variables on their X= and Y= arguments. At run time for both plots, the
522 Chapter 25 Executing Graph Templates
variable that has been assigned to XVAR will provide X values for the plot, and the
variable that has been assigned to YVAR will provide Y values.
The MVAR statement declares the macro variable STUDY. Because STUDY is not
a SAS automatic macro variable, it will be created for use in this template. On the
statements that later execute this template, you must initialize a value for STUDY.
The MVAR statement also declares the automatic macro variable SYSDATE. At run
time, the current system date and time will be substituted for this variable.
The ENTRYFOOTNOTE statement references both of the macro variables STUDY
and SYSDATE. The value that you assign to STUDY will be displayed as a left-
justified footnote, and the run-time value of SYSDATE will be displayed as a right-
justified footnote.
As with all GTL templates, the MYGRAPHS.REGFIT template can be executed with
either a PROC SGRENDER statement or a DATA step. Either way, any dynamic
variables and new macro variables that are declared in the template must be initialized to
provide run-time values for them. The following example executes the template with
PROC SGRENDER:
%let study=CLASS dataset;
proc sgrender data=sashelp.class template=mygraphs.regfit;
dynamic xvar="height" yvar="weight";
run;
Note the following:
The %LET statement assigns string value "CLASS data set" to the STUDY macro
variable.
PROC SGRENDER uses the DYNAMIC statement to initialize the dynamic
variables XVAR and YVAR. XVAR is assigned to the input variable HEIGHT, and
YVAR is assigned to the input variable WEIGHT.
Here is the output.
The DATA step uses the DYNAMIC= suboption of the ODS= option to initialize
dynamic variables. Macro variables can be initialized from the existing symbol table.
You can update the symbol table during DATA step execution with a CALL SYMPUT
or CALL SYMPUTX routine. The following DATA step executes the
MYGRAPHS.REGFIT template.
data _null_;
if _n_=1 then call symput("study","CLASS data set");
set sashelp.class;
Initializing Template Dynamic Variables and Macro Variables 523

Get SAS 9.4 Graph Template Language, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.