Announcement

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

  • merge sequentially?

    Dear All, J found this question here (in Chinese). Suppose that I have two datasets. Data A is:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id1 id2 sells)
    1 11 4
    1 11 5
    1 11 6
    2 22 3
    2 22 3
    3 33 4
    3 33 2
    end
    and Data B is:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id1 id2 costs)
    1 11 .2
    2  . .3
    . 33 .4
    end
    The desired result is
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id1 id2 sells costs)
    1 11 4 .2
    1 11 5 .2
    1 11 6 .2
    2 22 3 .3
    2 22 3 .3
    3 33 4 .4
    3 33 2 .4
    end
    The rule is that (1) first merge A with B using id1 (1 and 2), and then (2) the remaining ones are merged using id2 (33). Any suggestions are appreciated.
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear*
    input float(id1 id2 sells)
    1 11 4
    1 11 5
    1 11 6
    2 22 3
    2 22 3
    3 33 4
    3 33 2
    end
    tempfile A
    save `A'
    
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id1 id2 costs)
    1 11 .2
    2  . .3
    . 33 .4
    end
    tempfile B
    save `B'
    
    use `A', clear
    merge m:1 id1 using `B', keep(master match)
    frame put _all if _merge == 3, into(matched)
    drop if _merge == 3
    drop _merge
    merge m:1 id2 using `B', update keep(master match match_update) nogenerate
    
    frame change matched
    frameappend default
    -frameappend- is by Jeremy Freese, available from SSC.

    Comment


    • #3
      Dear Clyde, Thanks for this interesting suggestion (especially, the use of frame).
      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment


      • #4
        This exact question was also asked yesterday here.

        Comment


        • #5
          Dear Leonardo, Thanks for the helpful link.

          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment

          Working...
          X