log using matchid, text replace set memory 500m set more 1 program define matchid #delimit cr display "Merge previous year for `1'" local lastyear=2002 use /homes/data/morg/annual/morg`2' rename match match`2' rename minsamp minsamp`2' egen str2 filmage = concat(mage) replace filmage = "99" if mage==. if `1' == 1979 { gen matchp=0 } else if ( `1' <= 2000 & `1' > 1979 ) { local prevyear=`2'-1 if ( `1'==2000 ) { local prevyear=99 } sort matchid filmage age sex race id display "Merging `1' file using morg`prevyear'.dta (year<=2000)" merge matchid using /homes/data/morg/annual/morg`prevyear'.dta gen matchp =(_merge==3 & minsamp`2'==8) } else if `1' >2000 { local prevyear=`1'-2001 sort matchid filmage age sex race id display "Merging `1' file using morg0`prevyear'.dta (year>2000)" merge matchid using /homes/data/morg/annual/morg0`prevyear'.dta gen matchp =( _merge==3 & minsamp`2'==8) } if (`1' > 1979 ) { display "Dropping non-matches from the matchfile" drop if _merge==2 display "Dropping the _merge variable" drop _merge } if `1' <1999 { local nextyear = `2' + 1 sort matchid age filmage sex race id display "Merging `1' file using morg`nextyear'.dta (year<2000)" merge matchid using /homes/data/morg/annual/morg`nextyear'.dta gen matchn =(_merge==3 & minsamp`2'==4) } else if ( `1' < `lastyear' & `1' >= 1999 ) { local nextyear = `2' + 1 if ( `1' ==1999 ) { local nextyear=0 } sort matchid age filmage sex race id display "Merging `1' file using morg0`nextyear'.dta (year>=1999 & year < `lastyear')" merge matchid using /homes/data/morg/annual/morg0`nextyear'.dta gen matchn=( _merge==3 & minsamp`2'==4) } else if ( `1' == `lastyear' ) { gen matchn=0 } if ( `1' < `lastyear' ) { display "Dropping non-matches from the matchfile" drop if _merge==2 display "Dropping the _merge variable" drop _merge } gen byte match2=matchp+matchn display "Eliminating 'matches' that are too far apart in age" replace match2=0 if nragedif==0 replace match`2'=0 if nragedif==0 display "Eliminating 'matches' with different sex" list matchid sex if match2==1 & sexdif==0 replace match2=0 if sexdif==0 replace match`2'=0 if sexdif==0 display "Eliminating 'matches' with different race" replace match2=0 if racedif==0 replace match`2'=0 if racedif==0 *unab vlist: _all *sort `vlist' sort mym intmonth state hhid hhnum id lineno quietly by mym intmonth state hhid hhnum id lineno: gen dupm = cond(_N==1,0,_n) *quietly by `vlist': gen dupm = cond(_N==1,0,_n) replace match2=0 if dupm>0 replace match`2'=0 if dupm>0 sum match`2' match2 clear end *matchid 1979 79 79_83 *matchid 1980 80 79_83 *matchid 1981 81 79_83 *matchid 1982 82 79_83 *matchid 1983 83 79_83 *matchid 1984 84 84_88 *matchid 1985 85 84_88 *matchid 1986 86 84_88 *matchid 1987 87 84_88 *matchid 1988 88 84_88 *matchid 1989 89 89_93 *matchid 1990 90 89_93 *matchid 1991 91 89_93 *matchid 1992 92 89_93 *matchid 1993 93 89_93 *matchid 1994 94 94_97 *matchid 1995 95 94_97 *matchid 1996 96 94_97 *matchid 1997 97 94_97 matchid 1998 98 98 matchid 1999 99 98 matchid 2000 00 98 *matchid 2001 01 98 *matchid 2002 02 98