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

Advanced Programming Concepts<br />

The use of Default Local localizes all the names that:<br />

• Are not scoped as globals (for example, ::name)<br />

• Are not scoped as data table column names (for example, :name)<br />

• Occur without parentheses after them (for example, are not of the form name(...))<br />

For example, the following function sums three numbers.<br />

add3 = Function({a, b, c}, {temp}, temp=a+b; temp+c);<br />

X=add3(1, 5, 9);<br />

The following function does the same thing, automatically finding locals.<br />

add3 = Function({a, b, c}, {Default Local}, temp=a+b; temp+c);<br />

X=add3(1, 5, 9);<br />

In both cases, the variable temp is not a global, or, if it is already a global, remains untouched by evaluating<br />

the functions.<br />

Note: If you use an expression initially as local, then use it as a global, JSL changes the context. However,<br />

an expression used globally stays resolved globally regardless of its future use.<br />

Recursion<br />

Includes<br />

The Recurse function makes a recursive call of the defining function. For example, you can make a<br />

function to calculate factorials. A factorial is the product of a number, the number minus 1, the number<br />

minus 2, and so on, down to 1.<br />

myfactorial=function({a},if (a==1, 1, a*recurse(a-1)));<br />

myfactorial(5);<br />

120<br />

You can define recursive calculations without using Recurse. For example, you could replace Recurse by<br />

myfactorial, and the script would still work. However, Recurse offers these advantages:<br />

• It avoids name conflicts when a local variable has the same name as the function.<br />

• You can recurse even if the function itself has not been named (for example, assigned to a global variable,<br />

such as myfactorial above).<br />

The Include function opens a script file, parses the script in it, and executes the JSL in the specified file.<br />

include("pathname");<br />

For example,<br />

include("$SAMPLE_SCRIPTS/myStartupScript.jsl");<br />

There is an option to obtain the parsed expression from the file, rather than evaluating the file.<br />

include("pathname",

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

Saved successfully!

Ooh no, something went wrong!