Announcement

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

  • Merging matching Year & State

    Hello all,

    Suppose I have this main data set:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(ID wage Year State)
    3  600 2001 4
    1  100 2001 5
    2   40 2001 5
    2    . 2002 5
    3   90 2002 5
    1  200 2002 5
    3 1000 2003 4
    2  500 2003 6
    1  300 2003 6
    end
    Now I want to combine the information in the following data to the above one:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(Year State Macro)
    2001 4 100
    2002 4 200
    2003 4 300
    2001 5 400
    2002 5 500
    2003 5 600
    2001 6 100
    2002 6 200
    2003 6 300
    end

    I just want the "macro" data added to the first data set matching Year & State. I having really trouble with this when I use many-to-one merging, I think my data set is getting bigger!
    Can anyone help me?

  • #2
    Yes, because, at least in your example, there are combinations of Year and State that appear in the second data set but do not occur in the first. These get dragged in by -merge- as extra unmatched observations. But you can modify -merge-'s behavior with the -keep()- option:
    Code:
    use first_dataset, clear
    merge m:1 Year State using second_dataset, keep(master match)

    Comment


    • #3
      Thank you so much Clyde Schechter ! I get it now!

      Comment


      • #4
        Also, just checking, will I lose some observations from the master file (first data set) in this merging process? Because I don't want that either! I am ready to drop some macros from the second data set if they are not needed.

        Comment


        • #5
          No, you will lose any observations from the master file data set. There are three types of observations that can result from any merge: master only, using only, or matched. The -keep()- option allows you to specify which among these you want to keep (using the key words master, using, and match, respectively). So if you say -keep(master match) it will retain any master only and matched observations but omit using only observations. If you wanted to keep only matched observations, you could say -keep(match)-.

          Comment

          Working...
          X