14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 Programming Methods 189<br />

Lists and Expressions<br />

Evaluate expressions inside expressions<br />

Eval Expr evaluates expressions inside other expressions but returns an expression that includes the results.<br />

By comparison, Eval evaluates any expressions inside the expression, and then evaluates the expression.<br />

Here is an example where you would need to evaluate the inner expression first:<br />

// assumes a data table with column named X3<br />

x = expr( distribution(column( expr("X"||char(i)) )) );<br />

i = 3;<br />

y=Eval Expr(x); // returns Distribution(Column("X3"))<br />

However, Eval Expr only unpacks the inside layer then stops, returning the result as an expression. To<br />

evaluate further, you need to either call the result in a subsequent step, or else put Eval() around the<br />

EvalExpr().<br />

// two-step method<br />

y=Eval Expr(x);<br />

y;<br />

// one-step method<br />

eval(eval expr(x));<br />

See Table 8.3 “Compare all the operators for controlling evaluation,” p. 191, to learn what would happen if<br />

you tried to use Eval directly on x without first doing Eval Expr.<br />

Parsing strings into expressions, and vice versa<br />

Parsing is the syntactic scanning of character strings into language expressions. Suppose that you have read<br />

in a valid JSL expression into a character string, and now want to evaluate it. The Parse function returns<br />

the expression. To evaluate it, use the Eval function.<br />

x = parse("a=1") ; // x now has the expression a=1<br />

eval(parse("a=1")); // a now has the value 1<br />

To go in the reverse, use the Char function, which converts an expression into a character string. Usually the<br />

argument to a Char function is an Expr function (or a NameExpr of a global variable), since Char evaluates<br />

its argument before deparsing it.<br />

y = char(expr(a=1)); // results in y having the character value "a=1"<br />

z = char(42);<br />

The Char function allows arguments for field width and decimal parameter if the argument is a number.<br />

The default is 18 for width and 99 for decimal (Best format).<br />

char(42,5,2);<br />

// results in the character value "42.00"<br />

To preserve locale-specific formatting in the numeric value, include the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!