Recode numeric variables

Function

Basic command
recode varname (rule)
Useful options
recode varname (rule), gen(newvarname)
Explanations
varnameInsert the name of the variable.
(rule)Specify which values you want to recode and how you want them to change.
gen()Add this if you want to generate a new variable with the recoding.
newvarnameName of the new variable.
More information
help recode

Practical example 1

Dataset
StataData1.dta
Variable nameunemp_45
Variable labelDays in unemployment (Age 45, Year 2015)
Value labelsN/A
gen unemp_45dic=unemp_45
recode unemp_45dic (0=0) (1/365=1)

The new variable unemp_45dic is a binary version of the original variable unemp_45, where all the individuals who had 0 days of unemployment at age 45 are given the value 0, and everyone who had 1-365 days of unemployment are given the value 1.

tab unemp_45dic

browse unemp_45 unemp_45dic

An advantage with this way of recoding a variable is that we first create a copy of the variable and then recode the copy. This means that we can be sure that we do not recode the original variable by accident.

However, an important disadvantage is that we have to add the variable label, create the set of value labels, and apply the value labels in a second step.

The next practical example will show a way of doing all of this (gen, recode, label variable, label define, label values) within just one line of code!

Practical example 2

Dataset
StataData1.dta
Variable nameskipped
Variable labelSkipped class (Age 15, Year 1985)
Value labels1=Never
2=Sometimes
3=Often
recode skipped (1=0 "No") (2/3=1 "Yes"), gen(skipped_dic)

The new variable skipped_dic is a binary version of the original variable skipped, where all the individuals who responded “Never” are given the value 0 (“No”), and everyone who skipped class at some point (“Sometimes” or “Often”) are given the value 1 (“Yes”).

tab skipped_dic