Skip to content

Instantly share code, notes, and snippets.

@jmclawson
Created July 12, 2016 18:44
Show Gist options
  • Save jmclawson/2037d0b548338071b928847757f483aa to your computer and use it in GitHub Desktop.
Save jmclawson/2037d0b548338071b928847757f483aa to your computer and use it in GitHub Desktop.
Convert SPSS data files to CSV and Stata using R
library(foreign)
mytest.alldata <- list()
mytest.csvfile <- c()
mytest.dtafile <- c()
mytest.filelist <- list.files(path="data",pattern="*sav",recursive=FALSE,full.names=T)
for (number in 1:length(mytest.filelist)) {
mytest.alldata[[number]] <- read.spss(mytest.filelist[number],to.data.frame = TRUE)
mytest.csvfile[number] <- gsub("data/|.sav","",mytest.filelist[number])
mytest.csvfile[number] <- paste(mytest.csvfile[number],".csv",sep="")
mytest.dtafile[number] <- gsub("csv","dta",mytest.csvfile[number])
write.csv(mytest.alldata[[number]],mytest.csvfile[number])
write.dta(mytest.alldata[[number]],mytest.dtafile[number])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment