Announcement

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

  • Merging two panel datasets

    Hello everyone,
    I have searched the forum but did not manage to find help for my problem. Im new to stata and still trying to learn so i apologize if my questions are inappropriate.

    Im trying to merge two panel data sets like the ones in the examples below but i get the fallowing error msg: "variables Country year do not uniquely identify observations in the master data"

    i have tried to use the datasets the other way around and i get this error: variables Country year do not uniquely identify observations in the using data

    the code i type is : merge 1:1 Country year using "\\Client\C$\Users\1\Desktop\xxxx.dta nogenerate force

    I would appreciate very much any answers and again im very sorry if my question are out of place or inappropriate.



    First data set is:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte ID str1 Country int year byte(var1 var2 var3)
    1 "A" 2013  6  6  7
    1 "A" 2013  8  1  3
    2 "A" 2013  4  8  9
    2 "A" 2013 10 14 15
    1 "A" 2014  1  2  3
    1 "A" 2014  1  5  4
    2 "A" 2014  6  8  5
    2 "A" 2014  5  7  5
    1 "B" 2013  7  3  7
    1 "B" 2013  8  5  8
    end
    The second one looks like this:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str1 Country int year byte(var1 var2 var3)
    "A" 2013 1 4 6
    "A" 2014 2 4 6
    "A" 2015 2 4 6
    "B" 2013 3 4 6
    "B" 2014 1 2 3
    end

  • #2
    George:
    are you sure that you do not need -append- instead of -merge-?
    Last edited by Carlo Lazzaro; 29 Feb 2020, 11:10.
    Kind regards,
    Carlo
    (StataNow 19.0)

    Comment


    • #3
      George, I think your made-up example confuses things by using the same variable names in both datasets (var1 var2 var3).

      It appears to me that your master dataset (the one you call the first one) contains observations for individuals within countries and years, so for each combination of country and year, there will be multiple observations - that is why you are told that country and year does not uniquely identify observations.

      Your second dataset appears to have just one observation for each country and year. I am going to assume that the variables in your second dataset are actually different variables than those in your first dataset - perhaps country statistics for each year - and you wish to add them to your individual data in the master dataset. If you really want to replace the existing values of var1 var2 var3 with new values of var1 var2 var3 the code will need to be a little different, I think.

      Because you have multiple observations in your master dataset you do not want merge 1:1 - you want merge m:1 where the m:1 indicates that the master dataset (the one in memory) has multiple observations for each 1 observation in the using dataset. Something like
      Code:
      use first
      merge m:1 country year using second

      Comment


      • #4
        I now see that this question was previously asked and answered within 24 minutes at

        https://www.statalist.org/forums/for...e-long-to-wide

        When you get an answer to your question, it is requested in the FAQ that you close out your topic so that readers who see your question learn that their help is no longer needed. And, when you post the same question in two different places, they should have links to each other so members can see what may have been accomplished at the other location before they repeat the work someone else has already done.
        Last edited by William Lisowski; 29 Feb 2020, 12:05.

        Comment


        • #5
          Thank you William for your replies notes taken and will try not to repeat these mistakes for the future.

          I got the answer to my question and want to thank everybody that gave their answers, time and help.

          Comment

          Working...
          X