For the latest version of JMP Help, visit JMP.com/help.

Publication date: 09/28/2021

If

The If() function evaluates the first result expression when its condition evaluates as true (a nonzero or nonmissing value). Otherwise, it evaluates the second result expression.

The syntax is:

If ( condition, result1, result2 );

For example, the following script returns "Young" when the age is less than 12. Otherwise, the script returns "Young at Heart".

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
dt << New Column( "age group", "Character" );
For Each Row(
	:age group = If( :age <= 12,
		"Young",
		"Young at Heart"
	)
);

You can also string together multiple conditions and results. The syntax is:

If( condition1, result1,
	condition2, result2,
	...,
	resultElse );

In the preceding example, if condition1 is not true, the function continues evaluating until it finds a true condition. Then that condition’s result is returned.

The last result is returned when all conditions are false. And when a value is missing, the missing value is returned. For these reasons, it’s very important to include a default result at the end of the expression. Consider the following example, which recodes gender abbreviations in Big Class.jmp:

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
For Each Row( sex =
	If(
		sex == "F", "Female",
		sex == "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.

If(

Begins the If() loop.

sex == "F", "Female",

If the value of sex is F, replaces the value with Female.

sex == "M", "Male",

If the value of sex is M, replaces the value with Male.

"Unknown" );

If neither of the above conditions are true, replaces the value with Unknown. If this result were omitted and the value of sex were missing, the script would return a missing value.

);

Ends the loop.

You can also put actions and assignments in the result expression. The following example assigns 20 to x, because the first condition (y < 20) is false:

y = 25;
z = If( y < 20, x = y, x = 20 );

Note: Be careful to use two equal signs (==) for equality tests, not one equal sign (=). An If with an argument such as name=value assigns rather than tests the value.

Avoiding Memory Issues with Multiple If Statements

Running a script that contains dozens of If() Statements can cause memory problems. We recommend reducing the If() nesting. Suppose that your script contains 100 If() statements in this format:

If( val, If( val, If( val, If( val, If( val,
If( val, If( val, If( val, If( val, If( val,
...
) ) ) ) ) ) ) ) ) );

Rewrite the script:

If( val, val1, If(val, val2, If( val, val3,
If( val, val4, If( val, val5, If( val, val6, ...) ) ) ) ) );

You can also try setting the maximum call depth. The Maximum Call Depth preference sets the default for the maximum call depth (or stack size) in which JSL built-in functions, user-defined functions, or Recurse() function calls can be made. By default, the maximum call depth is set to 256.

In JMP, your JSL script has a limited amount of physical runtime stack memory in which to perform JSL function calls. By default, this size is set to 2MB. Increasing the maximum call depth can cause a physical runtime stack overflow, so incrementally increase this preference in small amounts until you find the best value that works for your JSL script.

Preferences[1] << Set( Maximum JMP call depth( 64 ) );
Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).