PDA

View Full Version : [Stata] Append files using loop



divishoot
08-11-2010, 09:21 AM
Hi,

I have about 20 files and I need to append 2 of them over and over.
Meaning append file0 to file1 and file1 to file2 and so on...
I wish to write a short loop code that will append each file to the next one.

I tried this-
forv y=0/10 {
use file`y', clear
append using file`y'+1
save file`y'new.dta
}
It didn't work and I don't really know how to fix it.

I thought about another code that will run but it's just wrong.

forv y=0/7 {
g x=`y'+1
forv x=1/8 {
use file`y', clear
append using file`x'
save file`y'new.dta, replace
}

Any ideas?
Help would be much appreciated

Dason
08-31-2010, 07:52 PM
I don't know how to do what you want in STATA (never used it). But if you still haven't figured this out I could offer up some code to do it in R.

Etienne
09-02-2010, 03:47 PM
Hey there,

I think that this might work:

forv y=0/10 {
local k = `y' + 1
use file`y', clear
append using file`k'
save file`y'new, replace
}

Hope this helps!

Etienne

divishoot
09-14-2010, 09:27 AM
Thanks. I'll try it, hope it works

divishoot
09-14-2010, 09:31 AM
I don't know how to do what you want in STATA (never used it). But if you still haven't figured this out I could offer up some code to do it in R.

I'm not very familiar with R and I think my work has some security issues with me downloading programs so I probably won't be able to use it.
Thank anyways

divishoot
09-15-2010, 04:57 AM
Hey there,

I think that this might work:

forv y=0/10 {
local k = `y' + 1
use file`y', clear
append using file`k'
save file`y'new, replace
}

Hope this helps!

Etienne

It Worked :) thanks.