Extract parts from alfanumerical values (text format)

The substr() function 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(municipality, 1, 2) will read and assign the first two letters from all values for the variable municipality.

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

create-dataset demography
import db/BEFOLKNING_KOMMNR_FAKTISK 2017-01-01 as municipality

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

//Adding value labels for nicer output
define-labels countystring '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 '21' Spitsbergen '25' 'Education abroad' '99' Unknown
assign-labels county countystring

tabulate county