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

create-dataset demografidata
import BEFOLKNING_KJOENN as kjønn

destring kjønn
tabulate kjønn, nolabels

recode kjønn (2 = 0)


//Check whether the recoding has been successful

tabulate kjønn, nolabels