
This function also allows the user to convert version numbers to arbitrary values (default is 12). I haven't tested this on Windows but in theory it should work fine.Įdit: here is a more complete solution with read_dta and write_dta wrapped into a single function dtavconv.

sta files from here, and the script ran without throwing up errors. Version = 12, label = attr(data, "label")) Write_dta(data, path = paste0(mypath, "/", new_fname),

New_fname <- paste0(unlist(strsplit(basename(all_files), "\\.")), #(Above) iterations need the length of the vector to be specifiedĭata <- read_dta(all_files, #You want to read the ith element in all_files library(haven)Īll_files <- list.files(path = mypath, pattern = "*.dta", full.names = TRUE) I've indicated the changes (along with comments explaining them) in the snippet below. Your code only needs some very minor modifications. I know the commands inside the loop are working for a single file but not really being able to automate for all files. Write_dta(data,"c:/directory/filename.dta", version = 12, label = attr(data, "label")) #Load file to be converted into STATA12 version #vector with name of files to be convertedĪll_files <- list.files(pattern="*.dta",full.names = TRUE) This is what I have got so far: setwd("C:/FilesLocation")

Ideally, I want to keep the names of the original folders and files but save the converted versions into a new location. dta files into Stata 12 format (keeping all labels) to then analyze. I want to create an automated process of converting all Stata 16. dta files (and other types of files not relevant to this question). I am using RStudio (running R 4.0.1) and Stata 12 for Windows and have got a large number of folders with Stata 16.
