## BEGIN BY SETTING THE WORKING DIRECTORY...
setwd("/Users/Myname/Desktop/Statahelp/")
library("foreign")
## VARSK ARE THE VARIABLES TO KEEP, IN THIS CASE, X1 AND X2.
varsk <- c("x1","x2")
## BEGIN A LOOP. FOR THE EXAMPLE, I AM ASSUMING THAT THERE ARE 500 FILES
for (i in 1:500){
strd <- paste('file',as.character(i),'.dta', sep='')
strk <- paste('file',as.character(i),'.csv', sep='')
data <- read.dta(strd)
datak <- data[varsk]
write.table(datak, file = strk, sep = ",", append = T)
}
If your files are in a different directory (other than the one specified in wd), then change the string in strd.
I also strongly suggest adding a variable in datak that is equal to [i]. This is solely for purposes of identifying the dataset that that particular observation came from. This is done by simply adding a line:
datak$fileid <- i
Done! Hope this was helpful!
I also strongly suggest adding a variable in datak that is equal to [i]. This is solely for purposes of identifying the dataset that that particular observation came from. This is done by simply adding a line:
datak$fileid <- i
Done! Hope this was helpful!