Use commandos `destring` and `recode` to make dummy variables

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

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

The variable sex 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 commando destring. However, this is not necessary if the variable is numerical to begin with.

//Connect to databank
require no.ssb.fdb:1 as fdb1

create-dataset demografidata
import fdb1/BEFOLKNING_KJOENN as sex

destring sex
tabulate sex, nolabels
recode sex (2 = 0)
rename sex male

//Check whether the recoding has been successful
tabulate male, nolabels