Format:
recode var-1 [:var-2 [:var-n]] => var-out;
[range-1] [:range-2 [:range-n]] => exp;
[range-1] [:range-2 [:range-n]] => exp;
: : :
[: [:]] => other-exp;
endrecode;
[ ] indicates that this part is optional.
Description:
The recode statement assigns a value to a variable based on the value of one or more other variables. It is used to rescale variables, to assign values to variables, and to create new variables from existing ones. It works like a multiple if statement but is easier to use. The recode statement evaluates each line within it sequentially, beginning with the first line.
If the values of variables "var-1" to "var-n" lie within the ranges "range-1" to "range-n", respectively, then "var-out" is assigned the value given by the expression on the first line and the recode statement is ended. If the values of the variables "var-1" to "var-n" do not all lie within their specified ranges, then the next line of the recode statement is evaluated. This process continues until either a value is assigned to "var-out" or the end of the recode statement is reached.
A variable in a multiple record or group cannot be used in the recode statement except in data entry applications (where it may be specified without an index and the current occurrence of a variable is assumed). Use working variables to refer to or to assign values to variables in multiple sections or groups.
Variables "var-1" through "var-n" are referred to as independent variables and must be separated by colons [:]. "Var-out", the variable whose value is assigned by the recode statement, is referred to as the dependent variable. A recode statement can have any number of independent variables, but only one dependent variable. The dependent variable can also be included among the independent variables. The dependent variable is separated from the independent variables by =>.
The ranges specified in the recode statement (i.e., "range-1" through "range-n") can take the following formats:
• A range between two values, e.g., 12-15
• An individual value, e.g., 9
• A comparison with another value, using >, <, >=, <=, or <>, e.g., < 5
• A special value , e.g., NOTAPPL
• Some combination of these formats separated by a comma, e.g., < 5, 9, 12-15, missing
A blank range for an independent variable includes all values. A blank range for all independent variables on the last line of a recode statement acts as a catch-all condition. It ensures that a value is always assigned to "var-out" by the recode statement. If a value is not assigned by the recode statement, the value of "var-out" will not change. The number of ranges on each line must equal the number of independent variables.
The expression for the dependent variable must result in a numeric value (if "var-out" is a numeric variable) or a string (if "var-out" is an alphanumeric variable).
(Note to ISSA users: The Recode and Box statements are identical.)
Example 1:
recode AGE => AGE_GROUP;
0-19 => 1;
20-29 => 2;
30-39 => 3;
40-49 => 4;
>= 50 => 5;
=> 9;
endrecode;
is equivalent to the following if statements:
if AGE in 0:19 then
AGE_GROUP = 1;
elseif AGE in 20:29 then
AGE_GROUP = 2;
elseif AGE in 30:39 then
AGE_GROUP = 3;
elseif AGE in 40:49 then
AGE_GROUP = 4;
elseif AGE >= 50 then
AGE_GROUP = 5;
else
AGE_GROUP = 9;
endif;
Example 2:
recode ATTEND : ED_LEVEL => EDUC;
2,notappl : => 1;
1 : 1 => 2;
1 : 2,3 => 3;
: => 9;
endrecode;
is equivalent to the following if statements:
if (ATTEND = 2 or ATTEND = notappl) then
EDUC = 1;
elseif ATTEND = 1 then
if ED_LEVEL = 1 then
EDUC = 2;
elseif ED_LEVEL in 2:3 then
EDUC = 3;
endif;
else
EDUC = 9;
endif;
Example 3:
recode UNITS : NUMBER => DAYS;
: notappl => notappl;
: missing => missing;
1 : => NUMBER;
2 : => NUMBER*7;
3 : => NUMBER*30;
4 : => NUMBER*365;
: => missing;
endrecode;
is equivalent to the following if statements:
if NUMBER = notappl then DAYS = notappl;
elseif NUMBER = missing then DAYS = missing;
elseif UNITS = 1 then DAYS = NUMBER;
elseif UNITS = 2 then DAYS = NUMBER*7;
elseif UNITS = 3 then DAYS = NUMBER*30;
elseif UNITS = 4 then DAYS = NUMBER*365;
else DAYS = missing;
endif;
See also: If Statement