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.

// Kobler til databank
require no.ssb.fdb:23 as db

// Henter variablene en trenger 
create-dataset demografidata
import db/BEFOLKNING_KJOENN as kjonn
import db/BEFOLKNING_FOEDSELS_AAR_MND as faarmnd
import db/INNTEKT_BRUTTOFORM 2020-01-01 as formue
import db/BOSATTEFDT_BOSTED 2020-01-01 as bosted

// Beregner alder i 2020 ut i fra fødselsår 
generate alder = 2020 - int( faarmnd / 100 )

// Lager en dummyvariabel som angir mann ut i fra kjønn
generate mann = 0
replace mann = 1 if kjonn == '1'

// Gruppere formue i 4 intervaller
generate formueint = 1
replace formueint = 2 if formue > 150000
replace formueint = 3 if formue > 250000
replace formueint = 4 if formue > 400000

// Angi formue i 1000 kr
generate formue1000 = formue / 1000

// Koder om fra kommune- til fylkesnivå 
generate fylke = substr(bosted,1,2)

// Legger til verdilabler for å navngi fylker med navn (=> penere deskriptiv output) 
define-labels fylkerstring '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' 'Utdanning i utlandet' '99' Uoppgitt
assign-labels fylke fylkerstring

tabulate fylke