*Coverage*
capture program drop coverage
program define coverage, rclass
version 9.0
syntax [anything] [if] [fweight pweight iweight] [, cluster(varname) ORIG(varlist) LABel(namelist) leftover(namelist)]
marksample touse
unab vlist : _all
local nument: word count `anything'
local namelist `anything'
if `nument'>2{
capture unab namelist: `anything'
}

if "`namelist'" ~= "" {
	local dv: word 1 of `namelist'
	local soluts: list namelist - dv

	foreach con of local soluts {
		capture cnfgen `con'
		local letters "`letters' `r(letters)'"
		}
	local letters: list uniq letters
	unab sets: `soluts'
	}

if "`namelist'" == "" {
	local start = r(start)
	local soluts "`r(sets)'"
	local dv = r(y)
	local lastset `r(colsig)'
		if "`r(comm)'" ~= "" {
		 local lastset `r(comm)'
		}
	capture myboolean `start'
		mark nomis
		capture markout nomis `start'
	local soluts `lastset'
   if "`soluts'" == "" {
	di
		di as err "Error: No Configurations Identified"
		capture keep `vlist'
		exit
    }
 local sets "`soluts'"
}

foreach co of local sets {
	local str `co'
	local `str' "`str'"
	local ln`str': length local `str'
	}

if "`orig'" == ""{
	if "`r(letters)'" == "" {
		foreach co of local sets {
			forvalues i = 1/`ln`co'' {
			local `co'prt= substr("`co'", `i', 1)
			local orig "`orig' ``co'prt'"
			}
		}
	local orig = upper("`orig'")
	local orig:list uniq orig
	local label `orig'
	}
  else{
	local orig `letters'
	local orig = upper("`orig'")
	local label `orig'
	}
}

tokenize `orig'
local ix 1
foreach lo of local label {
	local `lo' = upper("``ix''")
	local `lo'1 = lower("`lo'")
	local ``lo'1' = lower("``ix''")
	local ix = `ix'+1
	}
local toset 0
foreach co of local sets {
	forvalues iz = 1/`ln`co'' {
		local `co'prt= substr("`co'", `iz', 1)
		local truset`co' "`truset`co''*```co'prt''"
		}
	local truset`co':subinstr local truset`co' "*" ""
	local tset`co':subinstr local truset`co' "*" "", all
	capture local `tset`co'' `tset`co''
	capture local count: length local `tset`co''
		if _rc>0 {
			local count = 40
		}
	if `count' > `toset' {
		local toset `count'
		}
	 }
local toset=`toset'+5
di
di in white "{ul:Coverage}"
di as text %-`toset's "Set", %14s "Raw Coverage", %19s "Unique Coverage", %24s "Solution Consistency"
 	qui gen tempvar = 0
	local check: word count `sets'
	if `check' == 1 {
		local solset "`sets'"
		}
	     else {
		local solset: subinstr local sets " " ",", all
		}
	qui gen solsetcv = max(`solset', tempvar) if `touse'
	qui gen setcv = min(`dv', solsetcv) if `touse'
	qui ratio (T: setcv/`dv') if `touse' [`weight'`exp'], cluster(`cluster')
	qui local x = "_b[T]"
	qui local totcov = `x'
	qui capture drop solsetcv setcv

	qui gen solcon = max(`solset', tempvar) if `touse'
	qui gen numsolcon = min(`dv', solcon) if `touse'
	qui ratio (C: numsolcon / solcon)  if `touse' [`weight'`exp'], cluster(`cluster')
	qui local x = "_b[C]"
	qui local solconsis = `x'
	qui capture drop solcon numsolcon

	foreach var of local sets {
	 qui gen num`var' = min(`dv',`var') if `touse'
       qui ratio (Y:num`var'/`var')  if `touse' [`weight'`exp'], cluster(`cluster')
	 local ycons = "_b[Y]"
	 local ycons = `ycons'
	 qui gen rnum`var' = min(`dv', `var') if `touse'
	 qui ratio (R: rnum`var' / `dv')  if `touse' [`weight'`exp'], cluster(`cluster')
	 local raw = "_b[R]"
	 local raw = `raw'
	 drop rnum`var'
    if `check' == 1 {
	di as res %-`toset's "`truset`var''", %15.3f `raw' , %17.3f `raw', %21.3f `ycons'
	di
	di as res "Total Coverage =", %-9.3f `raw'
	di as res "Solution Consistency =", %-9.3f `ycons'
	di
	capture drop tempvar
	}
	else {
	 local others : list sets - var
       local solset: subinstr local others " " ",", all
	 qui gen solsetcv`var' = max(`solset', tempvar) if `touse'
 	 qui gen u`var'cv = min(`dv', solsetcv`var') if `touse'
       qui ratio (U: u`var'cv / `dv')  if `touse'  [`weight'`exp'], cluster(`cluster')
	 local x = "_b[U]"
	 local uniqcov = `x'
	 qui replace u`var'cv = `totcov' - `uniqcov'  if `touse'
	 qui su u`var'cv if `touse'
	 di as res %-`toset's "`truset`var''", %10.3f `raw' , %17.3f r(mean), %21.3f `ycons'
	 qui drop solsetcv`var' u`var'cv num`var'
      }
	}
if `check'~=1 {
	di
	di as res "Total Coverage =", %-9.3f `totcov'
	di as res "Solution Consistency =", %-9.3f `solconsis'
	di
}

if "`leftover'" ~= "" {
	di in green "{ul:And One of the Following}"
		di in white "`leftover'"
	}
capture drop tempvar
keep `vlist'
end