The example below demonstrates how to recode a variable that contains values on country level into world region level. Country level is in many cases too detailed, and it can then be useful to group countries according to Statistics Norway’s standard for world regions (https://www.ssb.no/en/klass/klassifikasjoner/91/varianter/1466).
By using the command
destring to make the values numeric, the
recode command may then be used to recode the values. Recode makes it possible, among other things, to recode all values within an interval into a given value. The recoding takes place sequentially from left to right in the recode expression, which can be used to simplify the coding. In the example below, e.g. the value 1 is assigned to all countries with codes in the intervals 101 – 141 and 144 – 158, except those which have already been given the code 2 in the previous parentheses.
//Connect to datastore require no.ssb.fdb:23 as db create-dataset population import db/BEFOLKNING_STATUSKODE 2021-01-01 as regstatus keep if regstatus == '1' import db/BEFOLKNING_FODELAND as birthcountry tabulate birthcountry //Create a copy of the birthcountry variable for controle purposes clone-variables birthcountry -> birthcountry_orig //Recode birth country into world region - first use destring to turn the value format into numeric (required when using recode command) destring birthcountry recode birthcountry (111 120 138 139 140 148 155 156 159/164 = 2) (101/141 144/158 = 1) (203/393 = 3) (143 404/578 = 4) (612 684 = 5) (601/775 = 6) (802/840 = 7) (980 = 8) (990 = 9) define-labels wregion 0 'Norway' 1 'EU/EEA' 2 'European countries outside the EU' 3 'Africa' 4 'Asia with Turkey' 5 'North America' 6 'South and Central America' 7 'Oceania' 8 'Stateless' 9 'Unknown' assign-labels birthcountry wregion tabulate birthcountry tabulate birthcountry_orig birthcountry