Announcement

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

  • Merging two data with two common variables and appending it to previous merged data

    I am trying to merge two data and appending it to the previous merged data.

    Below are my Stata codes

    ================================================== =========

    clear all
    set more off, permanently
    use "U:\state\ARUNACHAL PRADESH.dta"
    (A state file with lots of districts)

    levelsof distname, local(dis) (Trying to extract the district names)
    >"CHANGLANG"' `"DIBANG VALLEY"' `"EAST KAMENG"' `"EAST SIANG"' `"KURUNG KUMEY"' `"LOHIT"' `"LOWER DIBANG VALLEY"' `
    > "LOWER SUBANSIRI"' `"PAPUM PARE"' `"TAWANG"' `"TIRAP"' `"UPPER SIANG"' `"UPPER SUBANSIRI"' `"WEST KAMENG"' `"WEST
    > SIANG"'

    foreach s of local dis {
    use "U:\Excel file\Arunachal Pradesh _`s'.dta"

    merge 1:1 distname blkname using "U:\state\ARUNACHAL PRADESH.dta"
    tostring BlockDifference, replace

    append using "U:\merge\Arunachal Pradesh.dta"
    save "U:\merge\Arunachal Pradesh.dta", replace
    }


    >(note: variable distname was str9, now str25 to accommodate using data's values)
    >(note: variable blkname was str14, now str35 to accommodate using data's values)

    >variables distname blkname do not uniquely identify observations in the using data
    r(459);


    ================================================== ========

    Then the error message pops out and when I browse the data,
    there is only the first district (CHANGLANG)'s data.

    What could have I done wrong?



  • #2
    The error message suggests that your file "U:\state\ARUNACHAL PRADESH.dta" contains more than one observation with the same values on "distname blkname".
    Did you check this?

    An easy way of doing this is creating a variable that helps you identity combinations of distname blkname that occur more than once
    Code:
    bys distname blkname : gen count=_n
    
    list distname blkname if count>1


    Comment


    • #3
      There are bunch of same distnames but different blknames, and also other cases with same blknames but different distnames. So I wanted to actually merge them with same distname&blknames.. But I am not sure how...

      Comment


      • #4
        Obviously you have duplicates with the same distname&blkname. Would it be valid to do a 1:m merge? What is "them" in:
        So I wanted to actually merge them with same distname&blknames.

        Comment

        Working...
        X