*Coincidence Matrix*
capture program drop coincid
program define coincid, rclass
version 9.0
syntax [anything] [fweight pweight iweight] [, STANDardized USEREDuction LAST]
local nument: word count `anything'
local namelist `anything'
if `nument'>2{
capture unab namelist: `anything'
}
marksample touse
unab vlist: _all


if "`last'"=="last" & "`r(colsig)'"=="" & "`r(comm)'"=="" {
	di as err `"Error: No "Last" Configuration Set Present"'
	di as err "Using Sets"
	local last
}

if "`usereduction'"=="usereduction" & "`r(reducsols)'"=="" {
	di as err "Error: No Reduction Previously Performed"
	di as err "Can Not Use Reduced Solutions"
	capture keep `vlist'
	exit 119
}


if "`namelist'" ~= "" {
	local v `namelist'
	local size: word count `v'
	matrix coincid = J(`size',`size',0)
	local new "`v'"
	tokenize `new'

	local nonvar: list v - vlist
        foreach con of local nonvar {
		cnfgen `con'
	  }
    unab setscm: `v'
}

foreach var of local setscm {
	qui su `var'
	if `r(max)' > 1 | `r(min)'<0 {
		di as err "Error: `var' Not Fuzzified"
		capture keep `vlist'
		exit 119
	}
}

if "`namelist'" == "" {
	local start = r(start)
	local sets "`r(sets)'"
	if "`usereduction'"=="usereduction" {
		local start `r(reducsols)'
		local sets `r(reducsols)'
		}
	local commsets `r(comm)'
	if "`last'" == "last"{
		if "`commsets'"=="" {
	 		local start `r(colsig)'
			local sets `r(colsig)'
			}
			else {
			local start `r(comm)'
	 		local sets `r(comm)'
			}
	}
	local y = r(y)
	if "`start'"=="." | "`start'"=="`sets'" {
		local start `sets'
		foreach con of local sets {
			cnfgen `con'
			}
	}
	local setcm: list y | start
	unab setscm: `setcm'
	local size: word count `setcm'
	matrix coincid= J(`size',`size',0)
	local new "`setcm'"
	tokenize `new'
}

qui foreach var of local setscm {
	su `var'
	if `r(sum)' == 0 {
		local toex `toex' `var'
	}
}

local numexl: word count `toex'
	if `numexl'>0 {
		di
		di in green "Note: `toex' Have No Information and Excluded from Matrix"
		local setscm: list setscm - toex
		local size: word count `setscm'
		matrix coincid = J(`size',`size',0)
		local new "`setscm'"
		tokenize `new'
	}

di
di in white "{ul:Coincidence Matrix}"
local z = 1
qui foreach var of local setscm {
	forvalues i = 1/ `size' {
		gen minvar = min(`var',``i'')
		gen maxvar = max(`var', ``i'')
		ratio (C: minvar / maxvar) if `touse' [`weight'`exp']
		if "`standardized'" == "" {
				matrix coincid[`z', `i'] = _b[C]
		}
		if "`standardized'" == "standardized" {
			su minvar
			local numer = r(sum)
			su `var'
			local sum`var' = r(sum)
			su ``i''
			local sum``i'' = r(sum)
				if `sum`var'' > `sum``i''' {
					qui gen stdz = `numer' / `sum``i'''
				}
				else {
					qui gen stdz = `numer' / `sum`var''
					}
			su stdz
			local stdval = r(mean)
			matrix coincid[`z', `i'] = `stdval'
		}
		drop minvar maxvar
		capture drop stdz
	}
   local z = `z' +1
}
capture matrix colnames coincid = "`setscm'"
capture matrix rownames coincid = "`setscm'"
matlist coincid, format(%9.3f)


return local sets `sets'
return local y `y'
return local start `start'
return matrix coincid=coincid
capture keep `vlist'
end