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.
newvarname
Name of the new variable.
More information help recode
Practical example 1
Dataset
StataData1.dta
Variable name
unemp_45
Variable label
Days in unemployment (Age 45, Year 2015)
Value labels
N/A
gen unemp_45dic=unemp_45
recode unemp_45dic (0=0) (1/365=1)
The new variable unemp_45dicis 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!
The new variable skipped_dicis 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”).