Create dummy variables by using the recode command

There is more than one way to make dummy variables (values 1 and 0). Perhaps the most basic way is by using the generate and replace commands (see one of the other examples).

An alternative way of making dummy variables is by using the destring and recode commands as illustrated in the example below.

The variable gender takes the values ‘1’ and ‘2’ representing the values “Male” and “Female” respectively. The result of the procedure is a variable containing the values 1 and 0 representing male and female (= not male) respectively.

Since recode only accepts numerical valueformats, one must first convert from alfanumerical through the destring command. However, this is not necessary if the variable is numerical to begin with.

//Connect to datastore
require no.ssb.fdb:23 as db

create-dataset demography
import db/BEFOLKNING_KJOENN as gender

destring gender
tabulate gender
recode gender (2 = 0)
rename gender male

//Check for correct recoding
tabulate male, nolabels