*!	version 1.0	July 24, 1997
program define missing
version 5.0
local varlist "required existing"
local options "ANalys MEthod(string) DAte(string)"
*local if "optional"
*local in "optional"
parse "`*'"
local k : word count `varlist'

if "`analys'"!="" {
parse "`varlist'", parse(" ")

tempfile misfile
tempname misproc
postfile `misproc' var1 var2 single probs jaccard probj using `misfile'
local var 1
while `var'<`k' {
	label define varia `var' "``var''", add
	local var2=`k'
	while `var2'>`var' {
		tempvar xx yy
		gen `xx' = cond(``var''==. & ``var2''==.,1,0)
		gen `yy' = cond(``var''!=. & ``var2''!=.,1,0)
		summarize `xx', meanonly
		local n1=_result(18)
		summarize `yy', meanonly
		local n4=_result(18)
		local sing=(`n1' + `n4')/_N
		local jacc=`n1'/(_N - `n4')
		local prob1=1-Binomial(_N,`n1'+`n4',0.5)
		local prob2=1-Binomial(_N-`n4',`n1',0.5)
		post `misproc' `var' `var2' `sing' `prob1' `jacc' `prob2'
		local var2=`var2'-1
	}
	macro drop `xx' `yy'
	local var=`var'+1
}
label define varia `var' "``var''", add
tempfile labfile
quietly label save varia using `labfile'
postclose `misproc'
preserve
use `misfile', replace
run `labfile'
label variable var1 "VARIABLES"
label variable var2 "VARIABLES"
label values var1 varia
label values var2 varia
#delimit ;
di _n(2) "Table of Association Measures for Missing Values" _n(2)
_col(15) "Simple Coefficient"
	_n _col(17) "Signif. Level" _n _col(15) "Jaccard Coefficient" _n
_col(17)
	"Signif. Level";
#delimit cr
tabdisp var1 var2, cellvar(single probs jaccard probj) format(%5.3f)
center
restore, preserve
}

if substr("`method'",1,2)=="dr" {
	for `varlist': drop if @==.
}
else if substr("`method'",1,2)=="in" {
	local ind1=index("`method'","[")+1
	local ind2=index("`method'","]")
	local group=substr("`method'",`ind1',`ind2'-`ind1')
	tempvar ndat
	gen `ndat' = -`date'
	sort `group' `ndat'
	parse "`varlist'", parse(" ")
	local var=1
	while `var'<=`k' {
		tempvar xx`var'
		gen `xx`var'' = (``var''==.)
		replace `xx`var'' = `xx`var''[_n-1]+1 if ``var''==. &
`group'==`group'[_n-1]
		local var = `var'+1
	}
	sort `group' `date'
	local var=1
	while `var'<=`k' {
		tempvar yy`var'
		gen `yy`var'' = (``var''==.)
		replace `yy`var'' = `yy`var''[_n-1]+1 if ``var''==. &
`group'==`group'[_n-1]
		local var = `var'+1
	}
	local var=1
	while `var'<=`k' {
		#delimit ;
		replace ``var''=``var''[_n+`xx`var''] if ``var''==.
			& `group'!=`group'[_n-`yy`var''];
		replace ``var''=``var''[_n-`yy`var''] if ``var''==.
			& `group'!=`group'[_n+`xx`var''];
		replace ``var''=(`xx`var''*``var''[_n-`yy`var'']+
			`yy`var''*``var''[_n+`xx`var''])/(`yy`var''+`xx`var'') if ``var''==.
			& `group'==`group'[_n-`yy`var''] & `group'==`group'[_n+`xx`var''];
		#delimit cr
		local var = `var'+1
	}
}
else if substr("`method'",1,2)=="im" {
	parse "`varlist'", parse(" ")
	local var=1
	while `var'<=`k' {
		if `var'<`k' {
			local lvar=`var'+1
			#delimit ;
			local rest=substr("`varlist'",1,index("`varlist'","``var''")-1)+
				substr("`varlist'",index("`varlist'","``lvar''"),.);
			#delimit cr
		}
		else if `var'==`k' {
			local rest=substr("`varlist'",1,index("`varlist'","``var''")-1)
		}
		tempvar xx
		impute ``var'' `rest', gen(`xx')
		if ``var''==. {
			replace ``var''=`xx'
		}
		local var=`var'+1
	}
}
else if substr("`method'",1,2)=="pr" {
	parse "`varlist'", parse(" ")
	local var=1
	while `var'<=`k' {
		if `var'<`k' {
			local lvar=`var'+1
			#delimit ;
			local rest=substr("`varlist'",1,index("`varlist'","``var''")-1)+
				substr("`varlist'",index("`varlist'","``lvar''"),.);
			#delimit cr
		}
		else if `var'==`k' {
			local rest=substr("`varlist'",1,index("`varlist'","``var''")-1)
		}
		tempvar xx
		quietly fit "``var''" "`rest'"
		quietly predict `xx'
		replace ``var''=`xx' if ``var''==.
		local var=`var'+1
	}
}
else if substr("`method'",1,2)=="me" {
	parse "`varlist'", parse(" ")
	local ind1=index("`method'","[")+1
	local ind2=index("`method'","]")
	local group=substr("`method'",`ind1',`ind2'-`ind1')
	sort `group'
	local var=1
	while `var'<=`k' {
		tempvar xx
		egen `xx'=mean(``var''), by(`group')
		replace ``var''=`xx' if ``var''==.
		local var=`var'+1
	}
}
end