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

Advanced Scoping and Namespaces<br />

Note: Problems using this function are generally due to the mixing of unqualified and qualified references<br />

to global variables. Always explicitly scoping a name prevents accessing an unintended variable.<br />

Scoped Names<br />

Specify where a name is to be resolved by using a scope in the form scope:name where scope indicates<br />

how to resolve the name. For example, here:name indicates that the name should be resolved locally. Using<br />

the Names Default To Here mode, here:name is equivalent to name. The scope instructs how to look up the<br />

name.<br />

The syntax is to use the colon scope operator:<br />

scope:name<br />

There are several types of scopes:<br />

• Scope can be a resolution rule. For example, here:x means that x should be resolved to a name that is<br />

local to the script. Global:x means that x should be resolved to a global name.<br />

• Scope can be a namespace reference variable. For example. ref:a means that a should be resolved<br />

within the namespace that ref refers to.<br />

• Scope can be a data table reference to look up names as column names. For example, dt:height means<br />

that height should be resolved as a column in the data table that dt refers to.<br />

• Scope can be the name of a namespace that you created. For example, myNamespace:b where<br />

myNamespace is a namespace that you created. "myNamespace":b is equivalent. See “Namespaces” on<br />

page 204.<br />

Examples of Scoping Column Formulas<br />

The following examples demonstrate how to scope columns that contain formulas. In both scripts, x is a<br />

global variable, local variable, and column name.<br />

In the first script, the column name x is unscoped. the formula in the second column multiplies the value in<br />

column x by 100. In this case, The result is a column with the values 100, 200, and 300.<br />

::x=5;<br />

New Table( "Test",<br />

New Column( "x", Values( [1,2,3] ) ),<br />

New Column( "y", Formula( 100*x ) ),<br />

);<br />

In the following script, the formula in column y assigns 500 to x and then adds 50 to x. Each cell in the<br />

column contains the value 550.<br />

::x=5;<br />

New Table( "Test",<br />

New Column( "x", Values( [1,2,3] ) ),<br />

New Column( "y", Formula(Local( {x=500}, x+50 ) ) ),<br />

);

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

Saved successfully!

Ooh no, something went wrong!