Announcement

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

  • Appending multiple files

    Hi all,

    I have 135 .dta files on a folder each for one month from 2004m1 to 2018m12. The files are called Emp200401.dta, Emp200402.dta ...

    I would like to append all of them into one master dataset. I tried doing this using:

    Code:
    local fnames: dir "U:\Data\EmpCounts" files "*"
    foreach file of local fnames {
    append using `r(files)', all
    }
    But I recieve the following message:

    Code:
    invalid file specification
    r(198);
    Any advice on how to append the file into one dataset would be greatly appreciated.

  • #2
    In your foreach line you set the values of your local to be referred to as file, so they can be called with `file'. Try this.
    Code:
    cd "U:/Data/EmpCounts"
    local fnames: dir "U:/Data/EmpCounts" files "*"
    foreach file of local fnames{
            append using "`file'"
            }

    Comment


    • #3
      That works, thank you very much for your help!

      Comment


      • #4
        Hi:

        I thought I would learn/practice this. So I created 3 dummy data sets called dummy1.dta, dummy2.dta and dummy3.dta, and adopted the code given by Wouter Walker.

        Code:
        cd "/Users/bala/Documents/dummies"
        local fnames: dir "/Users/bala/Documents/dummies" files "*"
        foreach file of local fnames{
        append using "`file'"
        }
        But I receive this error:

        Code:
        file .DS_Store not found
        r(601);
        The files are there and I am able to append them manually one by one. Can someone please point out where I am going wrong?

        Thank you.

        Comment


        • #5
          There's a hidden file named .DS_Store in that directory that is a macOS system file rather than a Stata dataset. The simplest tweak would be to change "*" to "*.dta".

          Comment


          • #6
            Hi Nils, it's working fine now, and I have learnt a new lesson. Thank you so very much for the help.

            Comment

            Working...
            X