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 6 Types of Data 125<br />

Use Pattern Matching and Regular Expressions<br />

1 2 1 2 1 2,<br />

3 4 3 4 3 4]<br />

repeat(9,2,3)<br />

[ 9 9 9,<br />

9 9 9]<br />

The repeat function is compatible with the function of the same name in the <strong>SAS</strong>/IML language, but is<br />

incompatible with the <strong>SAS</strong> character DATA step function, which repeats one more time than this function.<br />

Use Pattern Matching and Regular Expressions<br />

Pattern matching in JSL is a flexible method for searching and manipulating strings.<br />

You define and use pattern variables just like any JMP variable:<br />

i = 3; // a numeric variable<br />

a = "Ralph"; // a character variable<br />

t = textbox("Madge"); // a display box variable<br />

p = ( "this" | "that" ) + patSpan(" ") + ( "car" | "bus" ); // a pattern<br />

variable<br />

When the above statement executes, p is assigned a pattern value. The pattern value can be used either to<br />

construct another pattern or to perform a pattern match. The patSpan function returns a pattern that<br />

matches a span of characters specified in the argument; patSpan("0123456789") matches runs of digits.<br />

p2 = "Take " + p + "."; // using p to build another pattern<br />

if( patMatch( "Take this bus.", p2 ), print("matches"), print("no match") ); /<br />

/ performing a match<br />

Sometime all you need to know is that the pattern matched the source text, as above. Other times, you<br />

might want to know what matched; for example, was it a bus or a car?<br />

p = ( "this" | "that" ) + patSpan(" ") + ( "car" | "bus" ) >? vehicleType; //<br />

conditional assignment ONLY if pattern matches<br />

if( patMatch( "Take this bus.", p ), show(vehicleType), print("no match") ); /<br />

/ do not use vehicleType in the ELSE because it is not set<br />

You could pre-load vehicleType with a default value if you do not want to check the outcome of the<br />

match with an if. The >? conditional assignment operator has two arguments, the first being a pattern and<br />

the second a JSL variable. >? constructs a pattern that matches the pattern (first argument) and stores the<br />

result of the match in the JSL variable (second argument) after the pattern succeeds. Similarly, >> does not<br />

wait for the pattern to succeed. As soon (and as often) as the >> pattern matches, the assignment is<br />

performed.<br />

findDelimString = patLen(3)>>beginDelim + patArb()>?middlePart +<br />

expr(beginDelim);<br />

testString = "SomeoneSawTheQuickBrownFoxJumpOverTheLazyDog'sBack";<br />

rc = PatMatch( testString, findDelimString, "" );<br />

show( rc, beginDelim, middlePart, testString );<br />

The above example shows a third argument in the patMatch function: the replacement string. In this case,<br />

the replacement is formed from a concatenation (|| operator) of three strings. One of the three strings,

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

Saved successfully!

Ooh no, something went wrong!