drop _all set obs 1 mac def dummy : tempvar gen byte $dummy=1 save ${temploc}dummy,replace use ${temploc}junk,clear keep if $y1>0 & $y2>0 & ($y1>1 | $y2>1) if _N==0 { use ${temploc}junk,clear drop $y1 $y2 save ${temploc}junk,replace exit } sort $sort year mac def guy : tempvar mac def guy1: tempvar mac def guy2: tempvar mac def group : tempvar qui by $sort year:gen byte $guy=_n qui by $sort:gen int $group=_n==1 replace $group=sum($group) save ${temploc}_junk,replace keep $sort $group qui by $sort:keep if _n==1 keep $group save ${temploc}_reflist,replace mac def HH=_N capture prog drop doit prog def doit mac def hold "`*'" mac def hh 1 while $hh<=$HH { use ${temploc}_reflist,clear keep if _n==$hh sort $group save ${temploc}__ref,replace use ${temploc}_junk sort $group merge $group using ${temploc}__ref keep if _merge==3 drop _merge sort $group save ${temploc}_fam,replace keep if year==$yr1 local obs1=_N save ${temploc}_junk1,replace use ${temploc}_fam keep if year==$yr2 local obs2=_N save ${temploc}_junk2,replace mac def marker 1 if `obs2'>`obs1' { $runcmnd this.cps `obs1' } if `obs2'<`obs1' { $runcmnd that.cps `obs2' } if `obs2'==`obs1' { mac def marker 0 $runcmnd other.cps `obs2' } mac def hh=$hh+1 } end doit `*' use ${temploc}junk,clear drop if $y1>0 & $y2>0 & ($y1>1 | $y2>1) if $marker { append using ${temploc}_junk drop $guy $group } drop $y1 $y2 save ${temploc}junk,replace use ${temploc}dummy drop if $dummy==1 drop $dummy $y1 $y2 $guy $group replace match_id=year==%yr1 replace match_id=sum(match_id) replace match_id=match_id + $leftoff replace level=2*$step mac def leftoff=match_id[_N] save ${temploc}dummy,replace use ${temploc}matched append using ${temploc}dummy save ${temploc}matched,replace exit 0