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

Lists and Expressions<br />

• Arg(name,i) obtains the expression held in name (it is not evaluated) and finds the ith argument<br />

• Arg(expression,i) evaluates expression and finds the i th argument<br />

• Arg(Expr(expression),i) finds the i th argument of expression<br />

As another example, if aExpr = Expr(12+13*sqrt(14-15));<br />

• Arg(aExpr,1) yields 12<br />

• Arg(aExpr,2) yields 13*sqrt(14-15)<br />

• Arg(Expr(12+13*sqrt(14-15)),2) yields 13*sqrt(14-15)<br />

To extract an argument of an argument inside an expression, you can nest Arg commands:<br />

• Arg(Arg(aExpr,2),1) yields the first argument within the second argument of aExpr, or 13.<br />

• Arg(Arg(aExpr,2),2) yields Sqrt( 14 - 15 )<br />

• Arg(Arg(Arg(aExpr,2),2),1) yields 14 - 15<br />

• Arg(Arg(Arg(aExpr,2),2),3) yields Empty()<br />

Here is a description of how the last example line unwraps itself:<br />

1. The inner Arg statement is evaluated.<br />

Arg(aExpr,2)<br />

13 * Sqrt( 14 - 15 )<br />

2. Then the next one is evaluated.<br />

Arg(Arg(aExpr,2),2)<br />

// this is equivalent to Arg(Expr (13 * Sqrt( 14 - 15 ) ), 2)<br />

Sqrt( 14 - 15 )<br />

3. Finally, the outer Arg is evaluated.<br />

Arg(Arg(Arg(aExpr,2),2),3)<br />

// this is equivalent to Arg (Expr (Sqrt( 14 - 15 ) ), 3)<br />

Empty()<br />

There is only one element to the Sqrt expression, so a request for the third argument yields Empty(). To<br />

access the two arguments inside the Sqrt expression, try this:<br />

Arg(Arg(Arg(Arg(aExpr,2),2),1),2);<br />

15<br />

HeadName(expressionArg) returns the name of the head of the expression as a string. If the expression<br />

is an infix, prefix, postfix, or other special character operator, then it is returned as the functional equivalent.<br />

The expressionArg can be a name holding an expression, an expression evaluated to an expression, or a<br />

literal expression quoted by Expr().<br />

For example, if aExpr = expr(a+b);<br />

• r = HeadName (aExpr) results in "Add".<br />

• r = HeadName (Expr(sqrt(r))) results in "Sqrt".<br />

• r = HeadName ({1,2,3}) results in "List".

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

Saved successfully!

Ooh no, something went wrong!