Announcement

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

  • Confused on how to merge multiple panel datasets

    Hi! I am trying to merge multiple panel datasets, but I don't know which is the appropriate way and my online search hasn't lead me anywhere.

    Here's an illustration of my current (long) panel datasets.

    Panel A
    Country Year Variable1 Variable2
    A 2016 xx xx
    A 2015 xx xx
    A 2014 xx xx
    B 2016 xx xx
    Panel B
    Country Year Variable3 Variable4 Variable5
    A 2016 xx xx xx
    A 2015 xx xx xx
    B 2016 xx xx xx
    Panel C
    Country Year Variable6
    A 2016 xx
    A 2015 xx
    B 2016 xx
    And here's what I'm trying to achieve:
    Country Year Variable1 Variable2 Variable3 Variable4 Variable5 Variable6
    A 2016 xx xx xx xx xx xx
    A 2015 xx xx xx xx xx xx
    A 2014 xx xx xx xx xx xx
    B 2016 xx xx xx xx xx xx
    B 2015 xx xx xx xx xx xx
    *Notes

    The common variables that I have are Country and Year.
    I'm not trying to merge any variables or values among my Variables(1 to 6).
    There is no data that I want to be lost.
    xx represents any given value of the data (didn't put values so it wouldn't look messy)

    Thanks!

  • #2
    you don't show what you tried (if anything) but the help for -merge- seems pretty clear; try the following (I assume that "Panel A") is in memory; note that since you don't give filenames I have made some up:
    Code:
    sort country year
    merge 1:1 country year using PanelB
    drop _merge
    sort country year
    merge 1:1 country year using PanelC
    drop _merge
    note that, as in your example data (in the future please use -dataex-; see the FAQ), I have assumed there are no duplicates of "country year" in any file; also, I just -drop-ped _merge each time but you will see from Stata's report whether there is a problem

    Comment


    • #3
      Sorry for not posting enough information, I only tried merging them using the toolbar. However, there were duplicates on the merged data which is why I thought there's a specific process or code for merging them and the one I used was wrong. Hence, the post.
      Nevertheless, I tried using the code you provided and by inspecting the data through Stata, I finally found the problem. One dataset had a space after some names which caused the problem. (Screenshot attached) I was finally able to solved the problem after editing them manually.

      Thank you for your help! The code helped me save a lot of time and headache
      Attached Files

      Comment


      • #4
        you ignored my request to show data using -dataex- and instead posted (against FAQ advice) a screen shot which I cannot read

        maybe someone else can help you but based on your text you may have solved the problem? at any rate, there are easier ways to solve such issues than cleaning them up manually; in particular, see, under "help function" the help for "strtrim()"
        Last edited by Rich Goldstein; 27 Apr 2017, 08:48.

        Comment


        • #5
          Yes I was able to solve it (with the help of your code). Once again, thank your for another tip (strtrim). It helped a lot! Many thanks

          Comment


          • #6
            Also, I'll keep the dataex in mind and will use it whenever possible in posting here. Thanks!

            Comment

            Working...
            X