SAS Institute. The Power to Know

SAS/GRAPH(R) 9.2: Graph Template Language Reference

Previous | Next
Overview

Dynamics and Macro Variables

An extremely useful technique for generalizing templates is to define dynamics and/or macro variables that resolve when the template is executed.

Template Statement Purpose Value supplied by ...
DYNAMIC defines dynamic(s) 1) DYNAMIC= suboption of ODS= option of FILE PRINT, or 2) DYNAMIC statement of PROC SGRENDER
MVAR defines macro variable(s) %LET or CALL SYMPUT( )
NMVAR defines macro variable(s) that resolves to a number(s) %LET or CALL SYMPUT( )

Template dynamics and macro variables can be used to supply required arguments and option values; they cannot be used to substitute keywords or statements. When used, dynamics and macro variables must appear after the DEFINE STATGRAPH statement and before the outermost LAYOUT block.

The following example defines a template named DYNAMICS that can create a histogram for any variable. It defines both macro variables and dynamics for runtime substitution. No data dependent information is hard coded in the template. You can execute templates with special ODS features of the DATA step.

  
 proc template; 
  define statgraph dynamics; 
   begingraph; 
    mvar SYSDATE9 SCALE; 
    nmvar BINS; 
    dynamic VAR VARLABEL; 
    entrytitle "Histogram of " VAR; 
    entrytitle "with Normal Distribution"; 
    layout overlay / xaxisopts=(label=VARLABEL); 
      histogram VAR /  scale=SCALE  nbins=BINS; 
      densityplot VAR / normal(); 
    endlayout; 
    entryfootnote halign=right "Created: " SYSDATE9 / 
      textattrs=GraphValueText; 
   endgraph; 
  end; 
 run; 
  
 %let bins=6; 
 %let scale=count; 
 proc sgrender data=sashelp.class 
               template=dynamics; 
   dynamic var="Height" varlabel="Height in Inches"; 
 run; 
  
 data _null_; 
   set sashelp.class; 
   if _n_=1 then do; 
      call symput("bins",""); 
      call symput("scale","percent"); 
   end; 
   file print ods=(template="dynamics" 
      dynamic=(var="Weight" varlabel="Weight in Pounds") ); 
   put _ods_; 
 run;
 


ovsgrender.gif (40490 bytes)

Figure 1.2: PROC SGRENDER Output


ovdatanull.gif (40490 bytes)

Figure 1.3: DATA Step Output


In general, you should avoid open code macro-variable references (with ampersands) unless you want the variables to resolve at template-compilation time. For example, if you had used &SYSDATE9 in the template definition, the reference would have resolved at template compilation, not when the template executed. You can think of the SYSDATE9 reference in the template as eval(symget('SYSDATE9')).

Another interesting language feature is that options will "drop out" if you do not supply values for them at run time. For example, in the preceding template, the only piece of information that is truly required is the name of the column (VAR) for the histogram statement. Consider the following SGRENDER specification:

  
 proc sgrender data=sashelp.class template=dynamics; 
   dynamic var="Weight"; 
 run;
 

If you were to execute the DYNAMICS template with this SGRENDER statement, the following template code would be executed:

  
 layout overlay / xaxisopts=( ); 
   histogram Weight /  ; 
   densityplot Weight / normal( ); 
 endlayout; 
 entryfootnote halign=right "Created: " 27DEC2005 / 
   textattrs=GraphValueText;
 

Note. GTL code is not sent directly to the renderer. At runtime, the graphics template communicates with a supplied TAGSET template. It translates GTL into markup language, such as XML, that a renderer acts upon. It is the tagset logic that is conditionally "dropping out" options when no option value is supplied.

Previous | Next | Top of Page