You can use the Match() function to make several equality comparisons without needing to rewrite the value to be compared. The syntax is:
Match( x, value1, result1, value2, result2, ..., resultElse );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
For Each Row( sex =
Match(
sex,
"F", "Female",
"M", "Male",
"Unknown");
);
For Each Row( sex = |
For each row in the table, sex is the column that is recoded.
|
Match( |
Begins the Match() loop.
|
sex, |
Specifies sex as the match argument.
|
"F", "Female", |
|
"M", "Male", |
|
"Unknown" ); |
|
); |
This Match() example is a simplified version of the example in If. The advantage of Match() is that you define the comparison value once rather than repeat it in each condition. The disadvantage is that you cannot use expressions with operators as you can with If; the argument sex == "F" returns an error in a Match() expression.
With more groups of conditions and results, the value of Match() becomes more apparent. The following script would require many additional lines of code with If().
dt = Open( "$SAMPLE_DATA/Travel Costs.jmp" );
For Each Row(
Booking Day of Week = Match( Booking Day of Week,
"Sunday", "SUN",
"Monday", "MON",
"Tuesday", "TUE",
"Wednesday", "WED",
"Thursday", "THU",
"Friday", "FRI",
"Saturday", "SAT",
"Not Specified"
)
);
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
For Each Row(
age = Match( age, 12, "Twelve", 13, "Thirteen", 14, "Fourteen", 15, "Fifteen", 16, "Sixteen", "Other" )
);
When data consists of integers such as 1, 2, and 3, you can save even more typing by using Choose(). See Choose for more information.