Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loops for appending

    Hello,

    I need to append 81 files.

    The long code would look something like..

    use 1.dta , clear
    append using 2.dta
    append using 3.dta
    .
    .
    .
    append using 81.dta

    I know I can use the foreach command, and I have seen examples on the internet, but they do not work for me, because of the way I have named my files.

    Any suggestions?

    Thank you

  • #2
    Place all your files in a single directory, let's pretend it is named dir81. Then look at the help information for macro extended functions found by giving the Stata command help extended_fcn for the documentation on the dir macro extended function, and use it to get a macro variable containing a list of the files in the directory. And then use the appropriate version of the foreach command to loop across the names. I think your final code will look something like the following, which I have not tested.

    Code:
    clear
    local filelist : dir dir81 files "*"
    foreach file of local filelist {
    append using `file'
    }

    Comment


    • #3
      Originally posted by Kerri Agnew View Post
      I know I can use the foreach command, and I have seen examples on the internet, but they do not work for me, because of the way I have named my files.
      The loop driven by foreach will work regardless of how you named your files. For example: "17" "alpha" "bar" "foo" "zuzu".
      Of course there is always an option of renaming/renumbering the files outside with a batch rename commands appropriate for your environment. See for example #10 here for Windows (DOS).

      Best, Sergiy

      Comment


      • #4
        I think it may need to be
        Code:
        append using "`file'"
        if there are spaces in the filename? Untested, I may have things wrong, but I'm pretty sure you'd need to get quotation marks around it somehow if there are spaces.

        Comment


        • #5
          After I posted #2 above, the concern Ben expressed at post #4 came to mind - at 3AM in bed, and no, I don't keep a laptop on the nightstand, and VNC from the iPhone leaves much to be desired. His posting reminded me of this, and I did a test, and then checked help extended_fcn which had I done that first would have saved me the effort of testing.

          The returned string will contain each of the names, separated one from the other by spaces and each enclosed in double quotes.

          Comment


          • #6
            It's not working for me right now, but thanks for the help! I will keep trying with variations of the command you gave

            Comment


            • #7
              My example should have included the directory name on the append. Note the use of compound double quotes surrounding dir81/`file' .
              Code:
              append using '"dir81/`file'"'
              That likely isn't the only problem. A good approach to debugging would be to replace the current local command with something like
              Code:
              local filelist "1.dta" "2.dta"
              (using your actual filenames of course) and then get the foreach loop working. Once the foreach loop works, then replace the original local command, perhaps following it with
              Code:
              display `"`filelist'"'
              to confirm that it's getting the correct list of files.

              Comment


              • #8
                If you keep getting errors, please provide exactly what you ran, and the exact error you got. Use CODE tags around them to make them readable. To get code tags, click the "A" above where you type, then click the "#" sign.

                Comment

                Working...
                X