Create dummy variable

Dummy variables are often used in regression analysis. Such variables need to be numerical and take the values 0 and 1.

This example shows how to make the dummy variable male. First, all values are set to 0. The commando replace then replaces the value 0 with 1 where the variable sex takes the value ‘1’.

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

create-dataset demografidata
import fdb1/BEFOLKNING_KJOENN as sex

//Make a dummy variable for male derived from sex
generate male = 0
replace male = 1 if sex == '1'