In GTL, as in Base SAS,
an expression is an arithmetic or logical expression that consists
of a sequence of operators, operands, and functions. An operand is
a dynamic, a macro variable, a column, a function, or a constant.
An operator is a symbol that requests a comparison, logical operation,
arithmetic calculation, or character concatenation.
Expressions can be used
to set an option value that is any one of the following:
-
a constant (character or numeric)
-
-
part of the text for ENTRYTITLE,
ENTRYFOOTNOTE, and ENTRY statements.
In GTL, an expression
must be enclosed in an EVAL function.
The following examples show how to specify an expression.
This first example uses the MEAN function to compute several constants:
/* create reference lines at computed positions */
referenceline y=eval(mean(weight)+2*std(weight)) / curvelabel="+2 STD";
referenceline y=eval(mean(weight)) / curvelabel="Mean";
referenceline y=eval(mean(weight)-2*std(weight)) / curvelabel="-2 STD";
This next example creates a new column:
/* create a new column as a log transformation */
scatterplot x=date y=eval(log10(amount));
This final example builds a text string:
/* create a date and time stamp as a footnote */
entryfootnote eval(put(today(),date9.)||" : "||put(time(),timeampm8.));
Valid GTL expressions are identical to valid WHERE expressions.
See the WHERE statement documentation in Base SAS for a comprehensive
list of operators and operands. Unlike WHERE expressions, however,
GTL expressions do not perform operations that create subsets. For
example, the difference between the result of a WHERE expression and
that of a logical GTL expression on a column is that the GTL expression
returns a Boolean value for each observation, without changing the
number of observations.
For example, the expression
for the Y= argument below does not reduce the number of observations
that are plotted.
scatterplot x=name y=eval(height between 40 and 60);
Instead, the computed
numeric column for the Y= argument consists of 0s and 1s, based on
whether each observation's HEIGHT value is between 40 and 60.
Whenever expressions
are used to create new columns, a new column name is internally manufactured
so that it does not collide with other columns in use.
Expressions in Statement Syntax. Throughout GTL documentation, you see
expression used in statement documentation:
BOXPLOT X=
column |
expressionY= numeric-column | expression </ option(s)>;
For the X= argument
in this BOXPLOT syntax,
expression means any EVAL(
expression) that results in either a numeric or character column. An expression
that yields a constant is not valid.
For the Y= argument,
expression means any EVAL(
expression) that results in a numeric column. The
expression cannot result in a character column
or any constant.
REFERENCELINE X= x-axis-value | column | expression </ option(s)>;
For a single line in
this REFERENCELINE syntax, the X= argument can be a constant (
x-axis-value). For multiple lines, it can be
a column. In either case, the supplied value(s) must have the same
data type as the axis. Thus, EVAL(
expression) can result in a constant, or it can result in a numeric or character
column. In either case, the data type of the result must agree with
the axis type.
Type Conversion in
GTL Expressions. Although expressions that are used in
a DATA step perform automatic type conversion, GTL expression evaluation
does not. Thus, you must use one or more functions to perform required
type conversions in an expression. Otherwise, the expression generates
an error condition without warning when the template is executed.
For example, consider
the following GTL expression:
if(substr(value, 1, 2) = "11")
This expression uses
the SUBSTR function to determine whether the first two characters
from VALUE evaluate to the string value "11". If VALUE is a string,
the expression works fine. However, if VALUE is numeric, then the
expression generates an error condition. For a numeric, you must convert
the value to a string before passing it to the SUBSTR function. The
following modification uses the CATS function to perform the type
conversion when necessary:
if(substr(cats(value, 1, 2)) = "11")