Johan Osterberg - Product Engineer

Recoding variables in Stata

April 15, 2019

Recoding variables in Stata is very simple. It is useful in many scenarios, for instance where you would want to merge a variable containing many values into a few distinct categories. Let’s use the nlsw88 dataset and the age variable:

sysuse nlsw88, clear
tab age

Tabulate age

This variable has quite a few values, let’s say we’d want to merge them into three distinct categories. To achieve this in Stata, we use the recode command and applying the ranges we want to merge.

recode age (30/37=1) (38/41=2) (42/46=3)
tab age

Tabulate age

And now all values are recoded into three new distinct categories.


Johan Osterberg

Written by Johan Osterberg who lives and works in Gothenburg, Sweden as a developer specialized in e-commerce. Connect with me on Linkedin