Create couples datasets including non-couples
The script below is an extension of [Create couples datasets](i18n\en\docusaurus-plugin-content-docs\current\eksempel\Tema - Familieanalyser\Lage datasett bestående av ektefeller og par.md).
The last part of the script creates an extended population that also includes people outside of a relationship: Singles and children in families with couples. This enables you to measure how many people are in a relationship vs non-relationship. Otherwise, the two scripts are identical.
// Connect to the database
require no.ssb.fdb:30 as ds
// Create a dataset consisting of the oldest person in each family
create-dataset oldest
import ds/BEFOLKNING_REGSTAT_FAMNR 2021-01-01 as famnr
import ds/BEFOLKNING_REGSTAT_PERSONKODE 2021-01-01 as personcode_oldest
import ds/BEFOLKNING_PARSTATUS 2021-01-01 as maritalstatus_oldest
import ds/REGSYS_ARB_YRKE_STYRK08 2020-11-16 as occupation_oldest
keep if personcode_oldest == '1'
// Create dataset with youngest person in each family, and who is not a child
create-dataset youngest
import ds/BEFOLKNING_REGSTAT_FAMNR 2021-01-01 as famnr
import ds/BEFOLKNING_REGSTAT_PERSONKODE 2021-01-01 as personcode_youngest
import ds/BEFOLKNING_PARSTATUS 2021-01-01 as maritalstatus_youngest
import ds/REGSYS_ARB_YRKE_STYRK08 2020-11-16 as occupation_youngest
keep if personcode_youngest == '2'
// Use the dataset with the oldest people and merge the corresponding variables into the dataset with the youngest people via family number (family number = oldest person's ID number)
use oldest
merge personcode_oldest maritalstatus_oldest occupation_oldest into youngest on famnr
// The dataset 'youngest' now contains data on both spouses/partners for all couples in the population. Running control tables
use youngest
tabulate personcode_youngest, missing
tabulate personcode_oldest, missing
tabulate maritalstatus_youngest, missing
tabulate maritalstatus_oldest, missing
tabulate occupation_oldest, missing
tabulate occupation_youngest, missing
// Check if both, one or none of the spouses/partners are in job
generate job_youngest = 1 if sysmiss(occupation_youngest) == 0
generate job_oldest = 1 if sysmiss(occupation_oldest) == 0
tabulate job_oldest job_youngest, missing
// Want to create statistics on the number of people in a relationship vs. not in a relationship. First create a new dataset that also includes singles and children in family with couple.
create-dataset youngest_and_no_couple
import ds/BEFOLKNING_REGSTAT_PERSONKODE 2021-01-01 as personcode
import ds/BEFOLKNING_PARSTATUS 2021-01-01 as maritalstatus
keep if personcode == '2' | inlist(maritalstatus,'0','8')
// Merge the information from the dataset with people in relationships onto the new expanded dataset. Those who have missing for the variable personcode_youngest after the merge are people who are not in a relationship
use youngest
merge famnr personcode_youngest maritalstatus_youngest occupation_youngest personcode_oldest maritalstatus_oldest occupation_oldest into youngest_and_no_couple
use youngest_and_no_couple
generate couple = 1
replace couple = 0 if sysmiss(personcode_youngest)
tabulate couple, missing