Announcement

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

  • Loop merge

    Hi

    I have a large number of dta files in a folder I have to open each file and merge each file with the ONE SAME file and append them into one

    how I can do that, please

    1_36.44991823_72.57155787_out.dta
    2_35.89191425_71.72687311_out.dta

    soo on


    ONE SAME FILE
    loc.dta


    Thanks
    Attached Files

  • #2
    first, many of us will not open binary files from people we don't know (please read the FAQ)

    second, you mention both -merge- and -append - but these are different (think of -merge- as adding new variables to the already existing observations while -append- adds new observations with the same set of variables - so which do you want to do? if -append-, note that you can append more than one file in one command; see
    Code:
    h append

    Comment


    • #3
      Yes First I have to merge all the files in a folder with one SAME FILE.

      after all the files are merged , I have to append all the files please

      Comment


      • #4
        sorry but I am confused - maybe someone else can help you

        Comment


        • #5
          There are various ways to do this but here is a relatively straightforward way to make it work with two loops. This particular formulation requires you to put your file names into local macros. I've added a third file just to make it clearer how the second loop works.
          I am assuming a 1 to 1 merge and an identifier that appears in all data sets called idvar. If these assumptions don't hold you will need to adjust accordingly.
          You'll also want to verify that you want to keep all the observations from the merge and, if not, add code to drop undesired observations.
          You also will likely need to add the path for the directories your files are in.

          Code:
          local f1 = "1_36.44991823_72.57155787_out.dta"
          local f2="2_35.89191425_71.72687311_out.dta"
          local f2="3_numbers_out.dta"
          
          forv i=1/3 {
              use loc.dta, clear
              merge 1:1 idvar using f`i'
              save appended`i'
          }
          
          use appended1, clear
          forv i=2/3 {
              append using appended`i'
          }
          This can be generalized to any number of files.
          If you don't want to create the list of files there are ways to have Stata create a macro of filenames from a directory, but it's unclear how many files you have and how your directory structure is arranged so I have no idea whether that would be worth doing here.

          Comment


          • #6
            I'll add that, depending on the nature of your data and what you're retaining from the merge, it may make sense to append all you files first and THEN merge to the other file using a m:1 merge. That would not require looping.
            However, that strategy may not work depending on how many observations you have for each ID in the two types of files.

            Comment

            Working...
            X