Check what your project received
Six checks to run on your project’s registers before you build a cohort
Statistics Denmark’s (DST’s) variable lists describe a register. Your project gets a delivery: a copy of the registers that someone ordered, converted and placed on your project’s drive. The two can differ. Columns can be missing, keys renamed or empty, years cut off, and registers left out of the parquet folder. None of this causes an error. Your code runs, and the result is quietly wrong.
Use this page when you start working with your project’s registers on DST, once you can open a register (Your project folder and its file types and Read a register), and before you build a cohort: spend an hour running the six checks against your own delivery. When you are done you know which registers, columns, keys and years you can rely on, and what to ask your data manager about. Each check comes with an example of what it catches.
The examples are examples. Each one is a problem that turned up in one real project’s delivery. They show what can happen, not what is in yours. Another project, or a newer delivery for the same project, may look completely different. That is the reason to run the checks yourself rather than rely on anyone’s list of known issues, this one included.
All the code reads registers with read_register() from fastreg. Without fastreg, use open_dataset("path/to/<register>/") instead; the rest is the same. To list the columns, the code fetches zero rows and reads their names, because colnames() on a lazy arrow table after rename_with() can come back empty (see Inspect your data).
library(fastreg) # read_register(): opens a parquet register by name
library(dplyr) # summarise, count, filter, collect and the pipe1. Does each register reach the end of your follow-up?
A register in your delivery does not necessarily run as far as DST says the register does. It may have been converted a year ago, ordered for fewer years, or be a register that has closed for good (dodsaars stops in 2001, see pitfall 1). Nothing errors when your follow-up runs past the end of the data: people simply look as if nothing happened to them, which quietly skews censoring, outcomes and matching.
So for every register you use, check its first and last year or date against your study period before you build anything on it:
library(fastreg) # read_register()
library(dplyr) # summarise, collect
# Yearly registers (BEF, AKM, UDDA, FAIK ...): look at the year column
read_register("akm") %>%
rename_with(tolower) %>%
summarise(first_year = min(year), last_year = max(year)) %>%
collect()
# Event registers (LMDB, LPR, DOD ...): look at the event date itself
read_register("lmdb") %>%
rename_with(tolower) %>%
summarise(first_date = min(eksd, na.rm = TRUE), last_date = max(eksd, na.rm = TRUE)) %>%
collect()For event registers, use the date column rather than year: year is the folder the row was filed under, not when the event happened.
The first and last date show only the two ends. One wrong date far in the future is enough to make a register look complete, so count the rows per year as well: a register that stops early has a last year with far fewer rows than the years before it.
# Rows per year of the event date: look for a year that is missing or much smaller
read_register("lmdb") %>%
rename_with(tolower) %>%
count(eksd) %>% # rows per date, counted in DuckDB: one row per day comes back
collect() %>%
mutate(event_year = as.integer(format(eksd, "%Y"))) %>% # the year the event happened, now in R
count(event_year, wt = n) %>% # add up the daily counts per year
arrange(event_year) # oldest year firstThe year is taken in R, after collect(), because the DuckDB backend that newer fastreg versions use cannot translate year() and stops with an error on a large register.
It also helps to know when the parquet copy was made. If the folder has not changed in a long time, newer years are probably missing:
file.info("path/to/parquet-registers/bef/")$mtime # replace with your project's pathIf a register stops before your follow-up ends, either end follow-up at the register’s last date, or ask your data manager whether a newer delivery or the raw SAS (a statistics program) files cover the missing years.
In one delivery, the LPR2 (the patient register up to 2019) contact register lpr_adm stopped on 31 December 2018, a full quarter before DST’s own LPR2 files end (31 March 2019; the regions switched their reporting to the patient register from 2019, LPR3 on 2-3 February or 2-3 March 2019). Contacts from those three months existed only in the LPR3 tables, so the standard LPR3 filter quietly removed them. See Extract from LPR (the National Patient Register) for how to check where your LPR2 stops.
2. Does each register have the columns DST documents?
Compare the column names in your delivery with DST’s variable list for that register (Registers and variables links to each one).
read_register("lpr_sksube") %>% # replace with the register you need
rename_with(tolower) %>%
head(0) %>% # zero rows: only the structure
collect() %>%
names() # the columns you actually haveIf a column you need is not there, no amount of filtering will bring it back. Ask your data manager whether it can be added to the delivery.
In one delivery, lpr_sksube (examinations and treatments) had only three columns: recnum, d_odto and year. DST’s variable list for it has 11 variables, among them the procedure code c_opr. The table could say that a procedure happened and when, but not which procedure.
3. Are the join keys named as documented?
Joins fail, or join nothing, when a key has a different name than the code expects. Look for the key columns before you write a join.
read_register("t_psyk_adm") %>%
rename_with(tolower) %>%
head(0) %>% # zero rows: only the structure
collect() %>%
names() # is the person key pnr? is the contact key recnum?In one delivery, the psychiatric LPR2 tables used v_cpr instead of pnr and k_recnum / v_recnum instead of recnum. See LPR2 - Psychiatric in Registers and variables for how to rename them only when your delivery needs it.
4. Are the join keys actually filled in?
A key column can exist and still be empty. Count the missing values in every key you plan to join on.
read_register("lpr_a_kontakt") %>% # replace with your table
rename_with(tolower) %>%
summarise(
rows = n(),
missing_key = sum(is.na(dw_ek_kontakt)) # replace with your join key
) %>%
collect()Some missing keys can be by design: in LPR3, a procedure attaches either to a contact or to a whole course, and the other key is left empty. A key that is missing on every row is not by design.
In one delivery, a project-specific LPR3 procedure table had the contact key dw_ek_kontakt empty on every single row, so joining it to the contacts returned nothing. The general LPR3 procedure register, lpr_a_procregistrering, had the key filled in and was the way out.
5. Is there one row per key, as documented?
Registers that are documented as one row per person per year (or per family per year) do not always arrive that way. Duplicates make every join grow without an error.
read_register("faik") %>%
rename_with(tolower) %>%
count(familie_id, year) %>% # replace with the key DST documents
filter(n > 1) %>%
count() %>% # how many keys have more than one row - expect 0
collect()In one delivery, FAIK (family income) from 2022 onward also carried pnr and repeated each family’s row once per family member. See Socioeconomic variables for the full check and the fix. BEF (the population register) can do the same with quarterly snapshots.
6. Is every register in the parquet folder?
A register you applied for is not necessarily converted. Some may only exist as raw SAS files, and the converted one with a similar name may be a different register.
list.files("path/to/parquet-registers/") # replace with your project's pathIf a register is missing, ask your data manager where it is. Reading a raw SAS file and converting it are covered in Convert SAS files to Parquet.
In one delivery, the death register dod was only a raw SAS file, while dodsaars was converted and easy to load. dodsaars stops in 2001 (see pitfall 1), so anyone who took the convenient one treated everyone who died after 2001 as alive.
When you find something
Write it down where your project keeps its notes, with the date and the check that found it, and tell your data manager. The next person on the project will otherwise find it again the hard way. Re-run the checks when a new delivery arrives: problems can be fixed, and new ones can appear.
Next steps
That was the last of the Start here steps. From here, go to what you need:
- Registers and variables: what each register holds, its join key and its most used columns
- Inspect your data: the commands for checking each extract you pull from the registers
- General DST pitfalls: problems that apply to every delivery