The
recode statement assigns a value to one or more output variables based on the value of one or more other input variables. It can be used to rescale variables, to assign values to variables, or to create a composite variable from existing variables. In many instances it is easier to use than writing multiple
if statements.
One or more expressions,
expression1 to
expressionN, are given, with each expression separated by two colons
::. These expressions must evaluate to either a number or string. Based on the values of each expression, CSPro evaluates each line between the
recode and
endrecode, trying to match a line's values,
value1 to
valueN, with the evaluated expressions. Once a line matches, one or more results,
result1 to
resultN, which are given after the arrow
->, are assigned to
destination_variable1 to
destination_variableN, which can be dictionary items, working variables, array values, list values, or the return values of a user-defined function. A destination variable can also be included among the expressions. It is possible to declare a new
numeric,
alpha, or
string destination variable as part of the recode statement (see Example 2 below).
Each value provided must evaluate to the same type (number or string) as its respective expression, and each result must be the same type as the destination variable. A value omitted is considered a match, and if no values are provided, then the optional else_result1 to else_resultN are used in the assignment (assuming no preceding line matched).