Special Dynamic Variables

Several special predefined dynamic variables are available that you can use with your templates. These special variables are listed in Special Dynamic Variables.
Special Dynamic Variables
_LIBNAME_
Represents the name of the library that contains the data set.
_MEMNAME_
Represents the name of the library member that contains the data set.
_BYLINE_
Represents the complete BY line, when you specify a BY statement.
_BYVAR_
Represents the name of the first BY variable, when you specify a BY statement.
_BYVARn_
Represents the name of the nth BY variable, when you specify a BY statement with multiple variables.
_BYVAL_
Represents the first BY value, when you specify a BY statement.
_BYVALn_
Represents the value of the nth BY variable, when you specify a BY statement with multiple variables.
To use a special variable in your template, you must define it in the DYNAMIC statement in your template. However, you do not have to define and initialize the special variable in your SGRENDER procedure statement. The special variables are defined and initialized automatically at run time for the SGRENDER procedure. Here is an example that uses the _BYVAL_ variable to substitute the BY variable value in a graph title. This example generates a graph of the monthly stock closing price for each year for the years 2000 through 2005.
/* Add a YEAR column to the data set. */
data stocks;
   set sashelp.stocks;
   year=year(date);
run;

/* Sort the data by YEAR. */
proc sort data=stocks;
   by year;
run;

/* Create the template for the graph. */
proc template;
  define statgraph seriesgroup;
    begingraph;
    dynamic _BYVAL_;
    entrytitle "Monthly Closing Price in " _BYVAL_;
      layout overlay / xaxisopts=(label="Month" type=discrete
                          griddisplay=on gridattrs=(pattern=dot))
                       yaxisopts=(griddisplay=on gridattrs=(pattern=dot));            
        seriesplot x=eval(month(date)) y=close / group=stock name="s";
        discretelegend "s";
      endlayout;
    endgraph;
  end;
run;
   
/* Suppress the BY-line. */
options nobyline;

/* Generate the graph. */
proc sgrender data=stocks template=seriesgroup;
   by year;
   where year between 2000 and 2005;
run;
Notice that _BYVAL_ is included in the DYNAMIC statement of the template definition, but it is not declared or initialized in the SGRENDER statement. The following figure shows the first graph. As shown in the figure, the first year value (2000) is substituted for _BYVAL_ in the graph title.
Monthly Closing Price Graph for the Year 2000