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 5 JSL Building Blocks 89<br />

Conditional Functions<br />

Match(<br />

sex,<br />

"F", "Female",<br />

"M", "Male",<br />

"Unknown");<br />

);<br />

The script works like this:<br />

For Each Row(sex =<br />

Match(<br />

sex,<br />

"F", "Female",<br />

"M", "Male",<br />

"Unknown");<br />

For each row in the table, sex is the column that is recoded.<br />

Begins the Match() loop.<br />

Specifies sex as the match argument.<br />

If the value matches "F" replace it with “Female”.<br />

If the value matches "M", replace it with "Male.<br />

If F or M are not matched, replaces the value with "Unknown".<br />

); Ends the loop.<br />

This Match() example is a simplified version of the example in “If” on page 87. The advantage of Match()<br />

is that you define the comparison value once rather than repeat it in each condition. The disadvantage is<br />

that you cannot use expressions with operators as you can with If; the argument sex == "F" returns an<br />

error in a Match() expression.<br />

With more groups of conditions and results, the value of Match() becomes more apparent. The following<br />

script would require many additional lines of code with If().<br />

dt=open("$SAMPLE_DATA/Travel Costs.jmp");<br />

For Each Row(Booking Day of Week =<br />

Match(Booking Day of Week, "Sunday", "SUN", "Monday", "MON", "Tuesday",<br />

"TUE", "Wednesday", "WED", "Thursday", "THU", "Friday", "FRI", "Saturday",<br />

"SAT", "Not Specified");<br />

);<br />

Be careful with the data type of the condition and result. In the preceding example, the conditions and<br />

results are both character data. If the data types do not match, JMP automatically changes the column’s data<br />

type. The results are not what you want.<br />

The following script changes the column’s data type from numeric to character based on the first cell’s data<br />

type. The first value, "12", is replaced with "Twelve", and the remaining cells are filled with "Other".<br />

dt=open("$SAMPLE_DATA/Big Class.jmp");<br />

For Each Row(age =<br />

Match(age, 12, "Twelve", 13, "Thirteen", 14, "Fourteen", 15, "Fifteen", 16,<br />

"Sixteen", "Other");<br />

);<br />

When data consists of integers such as 1, 2, and 3, you can save even more typing by using Choose(). See<br />

“Choose” on page 90 for more information.

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

Saved successfully!

Ooh no, something went wrong!