JSL provides five functions to evaluate an expression conditionally: If(), Match(), Choose(), Interpolate(), and Step().
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.
For each row in the table, sex is the column that is recoded.
|
|
Begins the If() loop.
|
|
If the value of sex is F, replaces the value with Female.
|
|
If the value of sex is M, replaces the value with Male.
|
|
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.
|
|
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:
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.
You can use the Match() function to make several equality comparisons without needing to rewrite the value to be compared. The syntax is:
For each row in the table, sex is the column that is recoded.
|
|
Begins the Match() loop.
|
|
Specifies sex as the match argument.
|
|
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().
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.
The Choose() function shortens scripts even more than Match(), provided the arguments are tested against integers. The syntax is:
Creates the x variable.
|
|
Begins the Choose() loop.
|
|
Evaluates the value of group.
|
|
If the value of group is 1, return "Low".
|
|
If the value of group is 2, return "Medium".
|
|
If the value of group is 3, return "High".
|
|
Closes the x variable.
|
|
Note: If the data types in the expression do not match, JMP automatically changes the column’s data type.
The Interpolate() function finds the y value corresponding to a given x value between two points (x1, y1 and x2, y2). A linear interpolation is applied to the values. You might use Interpolate() to calculate missing values between data points.
The value 62.5 is halfway between the y values 56 and 69, just as 23 is halfway between the x values 22 and 24.
•
|
The data points in each list or matrix must create a positive slope. For example, Interpolate(2,1,1,3,3) returns 2. However, Interpolate(2,3,3,1,1) returns a missing value (.).
|
•
|
Interpolate is best used for continuous data, but Step() is for discrete data. See Step for details.
|
The Step() is like Interpolate() except that it finds the corresponding y for a given x from a step-function fit rather than a linear fit. Use Step() with discrete y values (that is, when the y value’s corresponding x value can be only y1 or y2). However, when the y value’s corresponding x value can fall between y1 and y2, use Interpolate().
As with Interpolate, the data points can be specified as a list:
If the discounts were on a sliding scale (in this example, between 5 and 10, you would use Interpolate():
As with Interpolate(), the data points must create a positive slope.