0

I have many files I want to convert from sas7bdat to Rds using R, I am using the following code:

name <- read_sas("C:/Users/Desktop/Files/name.sas7bdat")
saveRDS(name, file = "C:/Users/Desktop/RDSfiles/name.Rds")

but there are too many files I would like to use a loop to optimize to process, any ideas?

Thank you very much!!

  • [This](https://stackoverflow.com/questions/40063507/how-to-loop-through-a-folder-of-csv-files-in-r) can help. – nghauran Nov 14 '17 at 20:48

1 Answers1

2

This is simple loop that I would use:

path_sas7bdat <- "C:/Users/Desktop/Files/"
path_RDS <- "C:/Users/Desktop/RDSfiles/"

files <- list.files(path_sas7bdat, pattern = "sas7bdat")

for(i in files) {
    saveRDS(read_sas(paste0(path_sas7bdat, i)),
            paste0(path_RDS, gsub("sas7bdat", "RDS", i)))
}

PS.: check saveRDS option collapse = FALSE for speed.

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • I'd do the same, but with `file.path` instead of `paste` (and no trailing `/` on folders). – Frank Nov 14 '17 at 21:50
  • It seems to work but gives me an error: Evaluation error: cannot allocate vector of size 11.7 Mb. –  Nov 15 '17 at 09:26
  • @Mee it should be your computer problem, what system is that, how much memory you have? – pogibas Nov 15 '17 at 09:27
  • 8GB. for big files gives me that error, any solution? –  Nov 15 '17 at 15:17