Epi Info™ User Guide

Command Reference


Analysis Commands: RECODE

Description
This command is used to change some or all the values of a field or variable.  New values can be stored back to the source or to a new field or variable.  It can also be used to convert data to another data type.  For example, RECODE can categorize text data to a set of numeric values, or it can convert numeric data to each value’s text equivalent.

Syntax
RECODE <SourceField> TO <TargetField>
LOVALUE – value1 = <recoded value>
value1 – value2 = <recoded value>
value2 – value3 = <recoded value>

valueN – HIVALUE = <recoded value>
ELSE = <recoded value>
END

  • The <SourceField> represents the donor field that supplies the data to be recoded.
  • The <TargetField> represents the field or variable to receive the recoded data.
  • LOVALUE key word is optional and represents the smallest or lowest value in the source field.
  • HIVALUE key word is optional and represents the largest or greatest value in the source field.
  • ELSE = <recoded value> is optional and is used to assign data to the target field when the data in the source field does not fit any of the other categories.

Comments
Text values must be enclosed in quotation marks; numeric, date; yes/no values must not. All recoded values must be of the same type. Numeric ranges are separated by a space, hyphen, and space, as in 1 – 5. Negative values are permitted (i.e.,-10, -9, and -8). The words LOVALUE and HIVALUE may be used to indicate the smallest and largest values representable in the database. The word ELSE may be used to indicate all values not falling in the preceding ranges. Recodes take place in the order stated; if two ranges overlap, the first in order will apply. Analysis cannot RECODE more than about 12 levels of values. If this is a problem, do as many recodes as possible, write a new table, READ it, and do more recodes.

Examples
Example 1: The RECODE command is used to generate an age range.

READ {.\Projects\Sample\Sample.prj}:Oswego 
DEFINE AgeRange TEXTINPUT 
RECODE Age TO AgeRange 
 LOVALUE - 0 = "<=0"
 0 - 10 = ">0 - 10"
 10 - 20 = ">10 - 20"
 20 - 30 = ">20 - 30"
 30 - 40 = ">30 - 40" 
 40 - 50 = ">40 - 50" 
 50 - 60 = ">50 - 60" 
 60 - 70 = ">60 - 70" 
 70 - 80 = ">70 - 80" 
 80 - 90 = ">80 - 90" 
 90 - 99 = ">90 - 99" 
 99 - HIVALUE = ">99" 
END
LIST Age AgeRange

Example 2: The RECODE command is used to generate an age range. The ELSE clause ensures that any values not captured in the recoding process are assigned a default value. In this case, any values greater than 60 are assigned “Senior.”

READ {.\Projects\Sample\Sample.prj}:Oswego 
DEFINE AgeRange TEXTINPUT 
RECODE Age TO AgeRange 
 LOVALUE - 0 = "<=0" 
 0 - 10 = ">0 - 10" 
 10 - 20 = ">10 - 20" 
 20 - 30 = ">20 - 30" 
 30 - 50 = ">30 - 50" 
 50 - 65 = ">50 - 65" 
 ELSE = "Senior" 
END 
LIST Age AgeRange

Example 3: The RECODE command is used to generate a detailed age range from 0 to 70 in increments of three. Note that a single RECODE command is limited to approximately 12 conditions because of query size limitations inherent in the Access database format. The desired age categories would require more than 12 recodes. To work around this problem, only 10 recodes are done at a time and are separated by a series of SELECT, WRITE, and READ commands. The first WRITE command creates a new temporary table (or overwrites an existing one) that stores only records that contain recoded values. The remaining records are not written out because of the SELECT command. Each subsequent block of recoded values is written to the same file using the APPEND parameter. By the time the code is done executing, the table T1 contains all of the recoded data.

READ {.\Projects\Sample\Sample.prj}:Oswego
DEFINE AgeRange TEXTINPUT
RECODE AGE TO AgeRange
LOVALUE - 0 = "<=0"
0 - 3 = ">0 - 3"
3 - 6 = ">3 - 6"
6 - 9 = ">6 - 9"
9 - 12 = ">9 - 12"
12 - 15 = ">12 - 15"
15 - 18 = ">15 - 18"
18 - 21 = ">18 - 21"
21 - 24 = ">21 - 24"
24 - 27 = ">24 - 27"
END
SELECT NOT AgeRange = (.)
WRITE REPLACE "Epi7" {Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".\Projects\Sample\Sample.mdb"} : T1 *

READ {.\Projects\Sample\Sample.prj}:Oswego
DEFINE AgeRange TEXTINPUT
RECODE Age TO AgeRange
27 - 30 = ">27 - 30"
30 - 33 = ">30 - 33"
33 - 36 = ">33 - 36"
36 - 39 = ">36 - 39"
39 - 42 = ">39 - 42"
42 - 45 = ">42 - 45"
45 - 48 = ">45 - 48"
48 - 51 = ">48 - 51"
51 - 54 = ">51 - 54"
54 - 57 = ">54 - 57"
END
SELECT NOT AgeRange = (.)
WRITE APPEND "Epi7" {Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".\Projects\Sample\Sample.mdb"} : T1 *

READ {.\Projects\Sample\Sample.prj}:Oswego
DEFINE AgeRange TEXTINPUt
RECODE Age TO AgeRange
57 - 60 = ">57 - 60"
60 - 63 = ">60 - 63"
63 - 66 = ">63 - 66"
66 - 69 = ">66 - 69"
69 - 70 = ">69 - 70"
70 - HIVALUE = ">70"
END
SELECT NOT AgeRange = (.)
WRITE APPEND "Epi7" {Provider=Microsoft.Jet.OLEDB.4.0;Data Source=".\Projects\Sample\Sample.mdb"} : T1 *

READ {.\Projects\Sample\Sample.prj}:T1
LIST Age AgeRange