import all SAS files in folder and convert labels to column names in R

175 Views Asked by At

Following on from post (How to write efficient code for importing multiple SAS files) I have used the below to load all SAS files into an R dataframe/tibble, however the label names are lost in unnest() step. How can one perform operations on the df$data list object to convert the SAS labels to column names? Is there a way to add in an sjlabelled::label_to_colnames() or haven::as_factor() prior to unnesting that acts on the df$data list?

library(tidyverse)
library(foreign)
library(haven)

df <- list.files(path = "E:/Cohortdata/Raw cohort/Nationalscreeningcohort",
                 full.names = TRUE,
                 recursive = TRUE,
                 pattern = "*.sas7bdat") %>% 
  tbl_df() %>%
  mutate(data = map(value, read_sas)) %>%
  unnest(data) 
``
1

There are 1 best solutions below

1
On

Edit found a solution incase helps anyone else

data.table::rbindlist() rowbinds the list column (df$data) of SAS files

as.data.frame() %>% label_to_colnames() converts to a dataframe and swaps the SAS labels to column names

library(tidyverse)
library(haven)
library(sjlabelled)
library(janitor)

df <- list.files(path = "R:/James/path",
                 full.names = TRUE,
                 recursive = TRUE,
                 pattern = "*.sas7bdat") %>% as_tibble() %>%
  mutate(data = map(value, read_sas)) 

df1<- data.table::rbindlist(df$data, fill=T) %>%
  as.data.frame() %>%  label_to_colnames() %>% clean_names()