-1

I have cross-sectional data for a number of years. There's a unique identifier which denotes a specific person common in each cross-section. However, new people are added each year (and some people die and are therefore not in later years)

I'd like to know how to merge these datasets in Stata to create a kind of panel.

Example:

    ID X_2005 Y_2005
    1    700    850
    2    400    311
    3    475    250


   ID  X_2006 Y_2006
   2     425    325
   3     500    250
   4     465    200

I want this:

  ID X_2005 Y_2005 X_2006 Y_2006
  1    700    850        
  2    400    311    425   325
  3    475    250    500   250
  4                  465   200

I've tried looking at Stata's merge command but I don't understand the 1:m, m:m, etc. It also seems like the number of identifiers has to be the same in each dataset so I'm confused.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Andrew01
  • 9
  • 1
  • 1
    Your command starts `merge 1:1 ID` as at most each distinct ID corresponds single observation by single observation in both datasets. It's the job of `merge` to worry about IDs present in only one dataset. Disappointing that you didn't even try to implement this simplest case. – Nick Cox Apr 06 '18 at 14:37
  • If you found my answer helpful, please consider accepting it using the check-mark. –  Mar 13 '19 at 18:25

1 Answers1

1

As @Nick Cox has already pointed out, your example is a simple case of a 1:1 merge, using the variable ID to match observations in both cross-sections:

clear
input ID X_2005 Y_2005
1    700    850
2    400    311
3    475    250
end

save year2005, replace

clear
input ID X_2006 Y_2006
2     425    325
3     500    250
4     465    200
end

save year2006, replace

use year2005
merge 1:1 ID using year2006

Result                           # of obs.
-----------------------------------------
not matched                             2
    from master                         1  (_merge==1)
    from using                          1  (_merge==2)

matched                                 2  (_merge==3)
-----------------------------------------


list ID  X_2005 Y_2005 X_2006  Y_2006

   +----------------------------------------+
   | ID   X_2005   Y_2005   X_2006   Y_2006 |
   |----------------------------------------|
1. |  1      700      850        .        . |
2. |  2      400      311      425      325 |
3. |  3      475      250      500      250 |
4. |  4        .        .      465      200 |
   +----------------------------------------+