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 185<br />

Lists and Expressions<br />

Note, however, that in column formulas, eval only works if it is outermost in the formula. So, for example,<br />

Formula(log(eval(column name(i))))<br />

would generate an error. Instead, use<br />

Formula(Eval(Substitute(expr(log(xxx)),expr(xxx), column name(i))))<br />

As another example,<br />

Formula(eval(column name(i))+10)<br />

generates an error, since eval is actually under the Add function. Instead, use<br />

Formula(Eval(Substitute(expr(xxx+10), expr(xxx), column name(i))))<br />

Retrieve a stored expression, not its result<br />

What if you wanted the symbolic value of a global (such as the expression<br />

Distribution(Column(height)) stored in dist above), rather than the evaluation of it (the actual<br />

launched platform)? The Name Expr function does this. Name Expr retrieves its argument as an expression<br />

without evaluating it, but if the argument is a name, it looks up the name’s expression and uses that,<br />

unevaluated.<br />

Expr returns its argument exactly, whereas Name Expr looks up the expression stored in its argument. Name<br />

Expr “unpacks” just one layer to get the expression, but does not keep unpacking to get the result.<br />

For example, you would need to use this if you had an expression stored in a name and you wanted to edit<br />

the expression:<br />

popVar = Expr( Summation( i = 1, N Row(), (y[i] - Col Mean( y )) ^ 2 / N Row()<br />

) );<br />

Summation( i = 1, N Row(), (y[i] - Col Mean( y )) ^ 2 / N Row() )<br />

unbiasedPopVar = substitute( name expr( popVar ), expr( wild()/nrow() ), expr(<br />

(y[i] - Col Mean( y )) ^ 2 / ( N Row() - 1 ) ) );<br />

Summation( i = 1, N Row(), (y[i] - Col Mean( y )) ^ 2 / (N Row() - 1) )<br />

Compare x, Expr(x), NameExpr(x), and Eval(x) after submitting this script:<br />

a=1; b=2; c=3;<br />

x = Expr(a+b+c);<br />

Table 8.1 Compare Eval, Name Expr, and Expr<br />

Command and result<br />

x;<br />

6<br />

Eval(x);<br />

6<br />

Explanation<br />

Evaluates x to the expression a+b+c, and then evaluates the expression,<br />

returning the result, 6 (unpacks all layers).<br />

Equivalent to simply calling x.<br />

Evaluates x to the expression a+b+c, and then evaluates the expression,<br />

returning the result, 6 (unpacks all layers).

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

Saved successfully!

Ooh no, something went wrong!