How do I convert a SAV. file to a CSV. file in R?

I am trying to convert SAV. to CSV. with R. However, I got an error. My data was imported as a SAV. file, but I wonder whether the problem may be that the data is in tbl_df data.frame. Could this be a problem? What is the error and the correct coding to continue with analyses? Below codes and output. Thank you!

library(haven)

HNIR62FL_data_2 <- read_sav("~/DHS/HNIR62SV/HNIR62FL_data_2.SAV")

View(HNIR62FL_data_2)

install.packages("foreign")

library("foreign")

class(HNIR62FL_data_2)

[1] "tbl_df"     "tbl"        "data.frame"

write.table(read.spss("HNIR62FL_data_2"), file = "from_sav_data.csv", quote = FALSE, sep = ",") 

Error in read.spss("HNIR62FL_data_2") : unable to open file: 'No such file or directory

Hi, after you read in the file using read_sav from the haven package, just use write_csv from the readr package to save.

library(haven)
library(readr)

HNIR62FL_data_2 <- read_sav("~/DHS/HNIR62SV/HNIR62FL_data_2.SAV")
getwd() # this is the folder it will save into unless you specify otherwise in the path below
write_csv(x=HNIR62FL_data_2, path="from_sav_data.csv")
2 Likes

Thank you very much @StatSteph. I was able to convert the file!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.