Extract parts from alfanumerical values (text format)

The function substr() can be used to extract parts from alfanumerical values. The first input parameter declares name of variable, the next one the start position of the reading, while the last parameter assigns the number of positions to be read from the starting position. For example: substr(bosted,1,2) will read and assign the first two letters from all values for the variable bosted.

create-dataset demografidata
import BOSATTEFDT_BOSTED 2000-01-01 as bosted

//Recode residency from municipality to county level 

generate fylke = substr(bosted, 1, 2)

//Add valuelables to name counties (gives nicer output) 

define-labels fylkerstring '01' 'Østfold' '02' 'Akershus' '03' 'Oslo' '04' 'Hedmark' '05' 'Oppland' '06' 'Buskerud' '07' 'Vestfold' '08' 'Telemark' '09' 'Aust-Agder' '10' 'Vest-Agder' '11' 'Rogaland' '12' 'Hordaland' '14' 'Sogn og Fjordane' '15' 'Møre og Romsdal' '16' 'Sør-Trøndelag' '17' 'Nord-Trøndelag' '18' 'Nordland' '19' 'Troms' '20' 'Finnmark' '99' 'Uoppgitt'

assign-labels fylke fylkerstring

tabulate fylke