The examples presented
so far have "hard coded" the statistic values in the compiled template.
Hardcoding the statistic values requires you to change and recompile
the template code whenever the column values change or you want to
use different columns for the analysis. A more flexible way to present
a statistics table is to compute its content as follows:
-
use GTL functions to calculate
any required statistics
-
use dynamic variables as placeholders
for column names in the template
-
at run time, initialize the dynamic
variables so that they resolve to the names of columns in the data
object that is used to provide data values for the graph.
GTL supplies several functions
that you can use to calculate the statistics, including functions
that match the statistic keywords used by PROC SUMMARY. GTL functions
are always specified within an EVAL function. To declare dynamic variables,
you use the DYNAMIC statement.
The following example uses the
DYNAMIC statement to declare a dynamic variable named VAR, which is
used in the functions N, MEAN, and STDDEV to calculate the statistics
that are displayed in the statistics table:
proc template;
define statgraph ginset4a;
dynamic VAR;
begingraph;
entrytitle "Two Column Inset with Computed Values";
layout overlay;
histogram VAR ;
layout gridded / rows=3 order=columnmajor border=true
autoalign=(topleft topright);
/* column 1 */
entry halign=left "N";
entry halign=left "Mean";
entry halign=left "Std Dev";
/* column 2 */
entry halign=left eval(strip(put(n(VAR),12.0))) ;
entry halign=left eval(strip(put(mean(VAR),12.2))) ;
entry halign=left eval(strip(put(stddev(VAR),12.2))) ;
endlayout;
endlayout;
endgraph;
end;
run;
proc sgrender data=sashelp.heart template=ginset4a;
dynamic VAR="mrw";
run;
-
Dynamic VAR is first referenced
in the HISTOGRAM statement, where it is used to represent the variable
that provides numeric values for the histogram.
-
Dynamic VAR is again referenced
on each of the ENTRY statements that specify the statistic values
to use in the statistics table. Each of the ENTRY statements uses
an EVAL function to specify functions to calculate the statistic.
-
On each of the ENTRY statements,
the STRIP function strips leading and trailing blanks from the returned
values. The PUT function on the first ENTRY statement returns the
statistics value with format 12.0, and the next two PUT statements
return values with format 12.2. The N, MEAN, and STDDEV functions
return the number of observations, mean, and standard deviation of
variable VAR.
-
In the SGRENDER procedure, the
DYNAMIC statement initializes dynamic VAR so that it resolves at run
time to column MRW from the SASHELP.HEART data set. Because the dynamic
resolves to a column name, the value that is assigned to it is enclosed
in quotation marks. (Values for dynamics that resolve to column names
or strings should be quoted. Numeric values should not be quoted.)
See Using Conditional Logic and Expressions for more information
about the functions that can be used in the EVAL function.
See Using Dynamics and Macro Variables to Make Flexible Templates for more information
about using dynamics and macro variables in GTL templates.