Hi all,

Working with two different event datasets: event1 and event2. If the dates overlapped I would like the dummy variable (Var1) to be merged. Minimum working example below using rangejoin.

Code:
ssc install dataex
ssc install rangejoin
ssc install rangestat

clear
input byte id int(StartYear1 EndYear1 Var1) 
1 1950        1960    1  
2 1950        1960    1
3 1950        1960    1
4 1950        1960    1
5 1950        1960    1
end
save "events1.dta", replace

clear
input byte id int(StartYear2 EndYear2) 
1 1955        1965            
2 1945        1955    
3 1951        1959
4 1949        1961
5 1940        1950    
end
save "events2.dta", replace

rangejoin StartYear1 StartYear2 EndYear2 using "events1.dta", by(id)
    drop StartYear1 EndYear1
rangejoin EndYear1 StartYear2 EndYear2 using "events1.dta", by(id)
    drop StartYear1 EndYear1    
gen Var = (Var1==1 | Var1_U==1) 
drop Var1 Var1_U

. list, sepby(id) 

     +--------------------------------+
     | id   StartY~2   EndYear2   Var |
     |--------------------------------|
  1. |  1       1955       1965     1 |
     |--------------------------------|
  2. |  2       1945       1955     1 |
     |--------------------------------|
  3. |  3       1951       1959     0 |
     |--------------------------------|
  4. |  4       1949       1961     1 |
     |--------------------------------|
  5. |  5       1940       1950     1 |
     +--------------------------------+
Unfortunately, you'll note that ID #3 does not include Var and the desired output should be as follows,

Code:
. list, sepby(id) 

     +--------------------------------+
     | id   StartY~2   EndYear2   Var |
     |--------------------------------|
  1. |  1       1955       1965     1 |
     |--------------------------------|
  2. |  2       1945       1955     1 |
     |--------------------------------|
  3. |  3       1951       1959     1 |
     |--------------------------------|
  4. |  4       1949       1961     1 |
     |--------------------------------|
  5. |  5       1940       1950     1 |
     +--------------------------------+
I fear that using rangejoin was not the correct approach to my issue. I could supplement the above code using joinby but I'm guessing there is a far more elegant solution? Any thoughts or suggestions would be greatly appreciated!