Publication date: 07/08/2024

Match Function

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 );

For example, the following script recodes gender abbreviations in Big Class.jmp:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
For Each Row( sex =
	Match(
		sex,
		"F", "Female",
		"M", "Male",
		"Unknown");
	);

The script works like this:

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",

If the value matches "F" replace it with “Female”.

"M", "Male",

If the value matches "M", replace it with "Male".

"Unknown" );

If F or M are not matched, replaces the value with "Unknown".

);

Ends the loop.

This Match() example is a simplified version of the example in If Function. 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"
	)
);

Be careful with the data type of the condition and result. In the preceding example, the conditions and results are both character data. If the data types do not match, JMP automatically changes the column’s data type. The results might not be what you want.

Note: The Match() function explicitly checks to see if the compare expression x is missing and if the value of value1 is missing, then it returns the value of result1; otherwise it continues to compare the expression x to each valueN value in each valueN/resultN pair, ignoring any missing values. If the expression x is equal to any of the valueN value, then the corresponding resultN value is returned. If no matching valueN value is found, then the resultElse value is returned.

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).