14.03.2014 Views

Scripting Guide - SAS

Scripting Guide - SAS

Scripting Guide - SAS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 Programming Methods 195<br />

Lists and Expressions<br />

Substituting<br />

show(polynomial);<br />

polynomial = d * x ^ 3 + a * x ^ 2 + b * x + c<br />

For the not-in-place operators, you must either state the expression directly or else quote a name that<br />

evaluates to an expression using NameExpr. Such operators do not have From or Into in their names. They<br />

return manipulated lists or expressions without changing the original list or expression given in the first<br />

argument.<br />

cubic=insert(expr(a*x^2 + b*x + c),expr(d*x^3),1);<br />

d * x ^ 3 + a * x ^ 2 + b * x + c<br />

quadratic=expr(a*x^2 + b*x + c);<br />

cubic=insert(nameexpr(quadratic),expr(d*x^3),1);<br />

d * x ^ 3 + a * x ^ 2 + b * x + c<br />

Substituting is extremely powerful; please review the earlier discussion “Substituting,” p. 194. Here are a few<br />

notes regarding substituting for expressions.<br />

Substitute(pattern,name,replacement) substitutes for names in expressions<br />

NameExpr looks through the name but copies instead of evaluates:<br />

a = expr(distribution(column(x), normal quantile plot)); show(NameExpr(a));<br />

NameExpr(a) = Distribution(Column(x), normal quantile plot)<br />

Substitute evaluates all its arguments, so they must be quoted correctly:<br />

b = substitute(NameExpr(a),expr(x),expr(weight)); show(NameExpr(b));<br />

NameExpr(b) = Distribution(Column(weight), normal quantile plot)<br />

SubstituteInto needs an L-value, so the first argument is not quoted:<br />

SubstituteInto(a,expr(x),expr(weight)); show(NameExpr(a));<br />

NameExpr(a) = Distribution(Column(weight), normal quantile plot)<br />

Substitute is useful for changing parts of expressions, such as in the following example that tests the Is<br />

functions:<br />

data = {1, {1,2,3}, [1 2 3], "abc", x, x(y)};<br />

ops = {is number, is list, is matrix, is string, is name, is expr};<br />

m=J(n items(data),n items(ops),0);<br />

test = expr(m[r,c] = _op(data[r]));<br />

for (r=1,r

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

Saved successfully!

Ooh no, something went wrong!