+ Reply to Thread
Results 1 to 6 of 6

Thread: [Stata] Append files using loop

  1. #1
    Points: 1,763, Level: 24
    Level completed: 63%, Points required for next Level: 37

    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [Stata] Append files using loop



    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

  2. #2
    RotParaTon
    Points: 46,221, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,074
    Thanks
    211
    Thanked 1,607 Times in 1,377 Posts
    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.

  3. #3
    Points: 2,352, Level: 29
    Level completed: 35%, Points required for next Level: 98

    Location
    Paris
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  4. #4
    Points: 1,763, Level: 24
    Level completed: 63%, Points required for next Level: 37

    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: [Stata] Append files using loop

    Thanks. I'll try it, hope it works

  5. #5
    Points: 1,763, Level: 24
    Level completed: 63%, Points required for next Level: 37

    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: [Stata] Append files using loop

    Quote Originally Posted by Dason View Post
    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

  6. #6
    Points: 1,763, Level: 24
    Level completed: 63%, Points required for next Level: 37

    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: [Stata] Append files using loop


    Quote Originally Posted by Etienne View Post
    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.

+ Reply to Thread

Similar Threads

  1. Replies: 4
    Last Post: 04-16-2011, 01:39 PM
  2. Replies: 12
    Last Post: 04-14-2011, 07:56 AM
  3. Merging Two Files
    By plasmatronix in forum SPSS
    Replies: 0
    Last Post: 12-30-2010, 09:00 AM
  4. importing csv files into R
    By niyinks in forum Statistics
    Replies: 12
    Last Post: 12-01-2010, 03:10 AM
  5. R and text files
    By freebird2008 in forum R
    Replies: 5
    Last Post: 06-03-2008, 08:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats