recode
Alters the coding of a category variable or creates a category variable from a continuous one. It cannot be abbreviated.
Typical Use:
recode varx (1 = 1) (2 = 0) (else = .)   - or -
recode vary (0/5 = 1 "low") (6/10 = 2 "mid") (10/20 = 3 "high"), gen(ylev)
Example
-
Create a new category variable from a continuous one:
. recode age (18/35 = 1 "youth") (36/99 = 2 "non-youth") (else = .),
> gen(age_group2);
(1097 differences between age and age_group2)
This would also work if age took on real values (instead of just integer ones).
- Change the coding of an existing variable. For example, on the survey forms, yes/no questions
are coded as 1=yes, 2=no and often 9=don't know. It's more convenient to have a standard binary with
0=no and things like "don't know" set to a missing value.
. recode b15raw b16raw b17raw (1 = 1 "yes") (2 = 0 "no")
> (8 = .o "not applicable") (9 = .p "don't know")
> (nonmiss = .a "invalid code") (miss = .),
> gen(raised_goats raised_pigs raised_cattle)
Options
- If the rules you provide do not cover all of the values of the original variable, these observations will be left unchanged if the generate option was not specified. It the generate observation was specified they will be set to . in the
new variable unless the option copyrest is specified (in which case these observations will be copied from the original variable.)
Notes
- A rule is generally of the form (numlist = x). Instead of a numlist you can also use
| nonmiss | all nonmissing values not covered by another rule |
| miss | all missing values not covered by another rule |
| else | all values not covered by another rule |
You can't use else with nonmiss or miss.
- recode can also be used to create an unrelated variable. The weights for this dataset were calculated in an Excel spreadsheet
using data on the number of households in each Local Council area from the census and the number of households covered in the survey and then
added to the dataset using a command like
recode localcouncil (711 = 123.711) (712 = 122.096) (713 = 125.934)
(721 = 122.948) (722 = 129.576) (723 = 123.545) (724 = 126.196)
(725 = 137.028) (731 = 127.918) (732 = 126.847) (733 = 124.939)
(734 = 122.387) (741 = 124.343) (742 = 140.150) (812 = 127.200)
(813 = 126.833) (821 = 125.380) (831 = 125.520) (832 = 30.900),
gen(inv_prob2);
Alternatively, I could have saved the columns from the Excel spreadsheet as a tab-delimited file, and used
insheet and merge but this was easier.
- You can't set the value labels if you are recoding an existing variable (not using the generate option).
- See the official Stata help for recode.