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.
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().
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.
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.
Notice that If() and Match() require more code to achieve the same results as the Choose function:
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:
As with Interpolate(), the data points must create a positive slope.