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 9 Data Tables 251<br />

Messages for Data Tables<br />

:age; //returns .<br />

Why? This default protects you from getting a result that might look reasonable for the whole data table but<br />

is actually based on only one row. It also protects you from accidentally overwriting data values when<br />

making assignments to ambiguous names under most circumstances. (You can have even more complete<br />

protection by using the prefix or infix : operator to refer unambiguously to a data column and the prefix ::<br />

operator to refer unambiguously to a global script variable. See “Stored expressions” on page 183 in the<br />

“Programming Methods” chapter.)<br />

You can use the Row() operator to get or set the current row number. Row( ) is an example of an L-value<br />

expression in JSL: an operator that returns its value unless you place it before an assignment operator (=, +=,<br />

and so on.) to set its value.<br />

Row(); //returns the number of the current row (0 by default)<br />

x=Row(); //stores the current row number in x<br />

Row()=7; //makes the 7th row current<br />

Row()=7; :age; //returns 12<br />

Note that the current row setting only lasts for the portion of a script that you select and submit. After the<br />

script executes, the current row setting resets to the default (row 0, or no row). Therefore, a script submitted<br />

all at once can produce different results from the same script submitted a few lines at a time.<br />

How Many Rows and Columns?<br />

For Each Row<br />

The NRow and NCol operators count the rows and columns in a data table (or matrix; see the “Data<br />

Structures” chapter on page 137).<br />

NRow(dt); // number of rows<br />

NCol(dt); // number of columns<br />

To iterate a script on each row of the current data table, put For Each Row around the script.<br />

For Each Row( if(:age>15, show(age)) );<br />

A typical use is for setting row states without creating a new formula column in the data table. The scripts<br />

below are similar except that the first one creates a row state column and the For Each Row version simply<br />

sets the row state without creating a column.<br />

new column("My Rowstate", rowstate, formula( color state(age-9)) );<br />

for each row(color of(rowstate())=age-9);<br />

To iterate a script on each row meeting a specified condition, combine For Each Row and If.<br />

for each row(marker of(rowstate())=if(sex=="F",2,6));<br />

Break and Continue can be used to control execution of a For Each Row loop. For details about using these<br />

two functions, see “Break and Continue” on page 85 in the “JSL Building Blocks” chapter.

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

Saved successfully!

Ooh no, something went wrong!