Search code examples
mergestatadata-cleaning

Merging 2 datasets when I observe 2 variables over multiple time periods


I'm trying to merge two datasets that look like this:

Dataset 1

unique_id    client_id    year   var1    var2  
  0001         10001      2000    .        .
  0001         10001      2001    .        .
  0002         10002      2000    .        .
  0002         10002      2001    .        .

Dataset 2

client_id       year    var3    var4  
  10001         2000     .        .
  10001         2001     .        . 
  10001         2002     .        .             
  10002         2000     .        .               
  10003         2001     .        .              

I observe each unique ID over multiple time periods and each client over multiple time periods.

I want to merge the dataset so that for each unique_id at time year, I have the corresponding client_id with variables 3 and 4 also at time year. Hence I want the final dataset to look the following way:

unique_id    client_id    year   var1    var2  var3(of client i in year t) .......
  0001         10001      2000    .        .            .     
  0001         10001      2001    .        .            .     
  0002         10001      2000    .        .            .     

Please let me know if I need to provide more information.


Solution

  • use "dataset1", clear
    merge 1:1 client_id year using "dataset2"