Announcement

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

  • Merge two datasets

    Good morning everyone.
    I need to merge two datasets but the do file doesn't work. The structure is this:

    use dataset2
    sort countryname year
    save dataset2, replace
    use maindataset
    sort countryname year
    merge countryname year using dataset2 --------------------> at this point appears this message: (you are using old merge syntax; see [D] merge for new syntax); variable _merge already defined r(110); and the Do-File end

    drop if _merge==2
    save maindataset, replace


    Someone could explain me why?

  • #2
    Welcome to Statalist.

    Try use this command to learn more about how the -merge- command is being put together:

    Code:
    help merge
    If the combinations of country-year are unique, then the command should be -merge 1:1- rather than just -merge-.

    In addition, putting a sort command before merge is not necessary, as it'd sort for you.
    Last edited by Ken Chui; 27 Nov 2021, 10:49.

    Comment


    • #3
      Adding to Ken's answer,
      Code:
      variable _merge already defined
      r(110)
      means that one or the other of your datasets was itself the result of a merge, and the _merge variable that merge creates (as per the documentation Ken referred you to) had not been dropped once it was no longer needed. You will want to drop _merge before the current merge, or follow the documentation and use the generate option to have merge give a different name to the variable it will now create, or use the keep(master match) and nogenerate options, perhaps.

      But Ken's advice is fundamental: before proceding, read the documentation for the merge command so you can understand what you need to do to have it to give you the results you need.

      The unneded sort and the old syntax makes me think you found sample code in a text or a paper or online and copied it into your program without trying to understand how it works by reading the documentation first.

      Comment

      Working...
      X