Using conditional logic,
you can create templates that have multiple visual results or output
representations, depending on existing conditions. The evaluation
of a logical expression must generate one or more complete statements
(not portions of statements). All conditional logic uses one of the
following constructs:
if (condition)
statement(s);
endif;
|
if (condition)
statement(s);
else
statement(s);
endif;
|
In the IF statement,
condition must be enclosed in parentheses. The
condition can be any standard SAS expression
involving arithmetic, logical operators, comparison operators, Boolean
operators, or concatenation operators. The expression can also use
SAS DATA step functions. The expression resolves to a single numeric
value, which is
true or
false.
In the following example,
a histogram is conditionally overlaid with a normal distribution curve,
a Kernel Density Estimate distribution curve, both, or neither:
proc template;
define statgraph conditional;
dynamic VAR VARLABEL BINS CURVE;
begingraph;
entrytitle "Histogram of " VAR;
layout overlay / xaxisopts=(label=VARLABEL);
histogram VAR / nbins=BINS;
if (upcase(CURVE) in ("ALL" "KERNEL"))
densityplot VAR / kernel() name="k"
legendlabel="Kernel"
lineattrs=(pattern=dash);
endif;
if (upcase(CURVE) in ("ALL" "NORMAL"))
densityplot VAR / normal() name="n"
legendlabel="Normal";
endif;
discretelegend "n" "k";
endlayout;
endgraph;
end;
run;
Note that the legend
syntax does not have to be made conditional. At run time, each plot
name in the legend is checked. If the plot does not exist, its name
is removed from the legend name list. If no names appear in the DISCRETELEGEND
statement, the legend “drops out” and the histogram
size is adjusted to fill the remaining space.