Skip to main content

Facilitating variables - dummy and multicategory variables, functions and labels

The example below shows different ways of organizing variables, including the use of labels.

Variables that are used as they are, without any kind of processing, already have labels that are displayed when creating statistics. But when organizing and creating new variables and categories, it is useful to create labels to get a better overview of the meaning of the various codes. Labels are created through the define-labels command and linked to the relevant variables with the assign-labels command. If you create several variables with an identical code list, it is fine to use assign-labels where you refer to already created label set names.

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

// Import variables 
create-dataset demography
import db/BEFOLKNING_KJOENN as gender
import db/BEFOLKNING_FOEDSELS_AAR_MND as birthdate
import db/INNTEKT_BRUTTOFORM 2020-01-01 as wealth
import db/BOSATTEFDT_BOSTED 2020-01-01 as municipality

// Calculate age in 2020 from birthyear 
generate age = 2020 - int( birthdate / 100 )

// Create a dummy variable measuring male from a gender variable
generate male = 0
replace male = 1 if gender == '1'

// Group wealth into four intervals
generate wealthint = 1
replace wealthint = 2 if wealth > 150000
replace wealthint = 3 if wealth > 250000
replace wealthint = 4 if wealth > 400000

// Measure wealth in 1000 kr
generate wealth1000 = wealth / 1000

// Recode from municipality to county level 
generate county = substr(municipality,1,2)

// Add value labels to name counties (=> nicer descriptive output) 
define-labels countystring '03' Oslo '11' Rogaland '15' 'Møre og Romsdal' '18' Nordland '30' Viken '34' Innlandet '38' 'Vestfold og Telemark' '42' Agder '46' Vestland '50' Trøndelag '54' 'Troms og Finnmark' '21' Spitsbergen '25' 'Education abroad' '99' Unknown
assign-labels county countystring

tabulate county