Announcement

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

  • Merging many data file

    Is there a way I can merge more than 10 data file into one?

    I'm trying to use:

    foreach file in d.dat d1.dat....... {
    sort id
    merge 1:1 id using d5.dat

    }

    It's telling me " no variables defined"

    Please, any suggestions?

  • #2
    You need to have at least one dataset already in memory, otherwise, you cannot merge them. Furthermore, your code is incorrect.
    Code:
    foreach file in d.dat d1.dat....... {
    merge 1:1 id using `file', nogen
    }
    You did not reference the local macro needed to loop over the files. I also added an option to prevent the creation of the _merge variable, which you would need to drop anyways after each merge.

    Comment


    • #3
      Thanks Bormann

      Comment


      • #4
        Use Users\user\Desktop\datafolder\d.dta

        While I referenced the local macro using:

        cd "C: \Users\user\Desktop\datafolder"
        local datasets: dir "Users\user\Desktop\datafolder" files "*.dta"

        Tempfile resource

        foreach dataset in local datasets {
        sort id save resource, replace
        merge 1:1 id using C:\Users\user\Desktop\datafolder\d1.dta.
        }
        It only merged d.dta and d1.dta. But I want stata to merge all other files in datafolder.

        Thanks.

        Comment


        • #5
          Just start with a simple loop.

          Code:
          use d, clear
          foreach file in d1 d2...dN{
              merge 1:1 id using `file', nogenerate
          }
          Assumes that each dataset has the variable "id" present.

          Comment


          • #6
            Thanks Andrew.

            ​​​​​​I've not still got my desired result.

            What this loop did was that it merged the 'use' dataset and the 'master' dataset only.

            I actually want stata to merge all the datasets in the datafolder.

            E.g. The data folder contains d1, d2, d3........dN

            Comment


            • #7
              What this loop did was that it merged the 'use' dataset and the 'master' dataset only.
              Does the loop terminate with an error? If not, the merges were successful. I cannot advise further absent a data example. You can randomly pick 3 datasets and provide the first 10 observations of each so I illustrate the procedure. Just copy and paste the result of the following for each of the datasets. Replace the names of the datasets accordingly.

              Code:
              use d, clear
              dataex in 1/10
              
              use d3, clear
              dataex in 1/10
              
              use d7, clear
              dataex in 1/10

              Comment

              Working...
              X