Announcement

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

  • Merge three datasets at the same time?

    I have three datasets (A,B, and C), which all datasets have the Year, State, Index variables. The Year and State are the same in three datasets, but the Index1 is in A, the Index2 in B, and the Index3 in C. I would like to merge three datasets.

    I was able to merge three datasets with this code:

    use A, clear

    merge 1:1 Year State using B
    drop _merge
    merge 1:1 Year State using C
    drop _merge

    save data_merged


    Is there any way I can merge multiple datasets together?

    Many thanks!







  • #2
    What you did is the correct approach to merging multiple datasets together. There is no single command in base Stata that will merge more than two datasets simultaneously. It is possible to imagine writing a command that would hide the sequential merges and just present the final result. I cannot imagine the circumstances under which I would find this helpful, since I generally want to check after each merge that my assumptions about matching are correct, and perhaps discard nonmatches from one or the other of the datasets.

    Comment


    • #3
      If you are familiar with merging in another statistical package, I'm quite sure that the underlying process is hiding the sequential merges before filtering out the requested combination of rows. Is it more typing? Certainly. But it is more explicit for an operation that can have quite serious implications to your dataset structure and integrity.

      Comment


      • #4
        https://www.stata.com/stb/stb29/dm37/xmerge.hlp

        Comment


        • #5
          I used xmerge before. It works.

          https://www.stata.com/stb/stb29/dm37/xmerge.hlp

          Comment


          • #6
            Originally posted by shem shen View Post
            I used xmerge before. It works.

            https://www.stata.com/stb/stb29/dm37/xmerge.hlp
            Having taken a quick read of the source code (from version 4, even) this is precisely what's happening. A better and potentially faster way would be to manage the merges with frames or using joinby or merge.

            Comment


            • #7
              Originally posted by shem shen View Post
              I used xmerge before. It works.

              https://www.stata.com/stb/stb29/dm37/xmerge.hlp
              Thank you, Shem.
              Last edited by Jaeyong Yoo; 25 Jun 2020, 00:18.

              Comment


              • #8
                Originally posted by William Lisowski View Post
                What you did is the correct approach to merging multiple datasets together. There is no single command in base Stata that will merge more than two datasets simultaneously. It is possible to imagine writing a command that would hide the sequential merges and just present the final result. I cannot imagine the circumstances under which I would find this helpful, since I generally want to check after each merge that my assumptions about matching are correct, and perhaps discard nonmatches from one or the other of the datasets.
                You're right. I might need to check each time if the merge has been correct. Thanks, William and Leonardo.

                Comment

                Working...
                X