Initializing Template Dynamics and Macro Variables

A useful technique for generalizing templates is to define dynamics and/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:
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;
  • The DYNAMIC statement declares dynamic variables XVAR and YVAR. On the statements that later execute this template, you must initialize these dynamics by assigning them to variables from the input data source so that they have values at runtime.
  • The ENTRYTITLE statement concatenates dynamic variables XVAR and YVAR into a string that will be displayed as the graph title. At runtime, the dynamics will be replaced by the names of the variables that are assigned to the dynamics when they are initialized.
  • The SCATTERPLOT and REGRESSIONPLOT statements each reference the dynamics on their X= and Y= arguments. At runtime for both plots, the 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 runtime, 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 runtime 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 dynamics and/or new macro variables that are declared in the template must be initialized to provide runtime 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;
  • 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.
Initializing Dynamics and Macro Variables
The DATA step uses the DYNAMIC= suboption of the ODS= option to initialize dynamics. 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 example executes the MYGRAPHS.REGFIT template with a DATA step:
data _null_;
  if _n_=1 then call symput("study","CLASS dataset") ;
  set sashelp.class;
  file print ods=( template="mygraphs.regfit"
                  dynamic=( xvar="height" yvar="weight" ) );
  put _ods_;
run;
  • The CALL SYMPUT routine initializes the macro variable STUDY with the string value "CLASS dataset." The macro variable only needs to be initialized once, so the IF statement limits the initialization to the first observation (_N_ = 1).
  • The DYNAMIC= suboption initializes the dynamic variables XVAR and YVAR. XVAR is assigned to the input variable HEIGHT, and YVAR is assigned to the input variable WEIGHT.
For a more complete discussion of this topic and additional examples, see Using Dynamics and Macro Variables to Make Flexible Templates.