*MAVMB*
capture program drop mavmb
program define mavmb, rclass
version 9.0
syntax [anything] [if] [, group(varname) cluster(varname) SIGonly SLEVel(real .05) GREATer(namelist) USEREDuction LAST]
local nument: word count `anything'
local namelist `anything'
if `nument'>2{
capture unab namelist: `anything'
}
unab vlist: _all
marksample touse
local test "mavmb"
local oldgroup `r(oldgroup)'

if "`greater'" ~= "" {
	if "`greater'" ~= "col1" & "`greater'"~= "col2" {
		di as err `"Error: "`greater'" Not a Valid Option of Greater"'
		di as err "Greater Option Not Applied"
		local greater
	}
}

if "`oldgroup'"=="" & "`group'" == "" {
	di as error "ERROR: No Group Variable Defined"
	exit
	}

if "`oldgroup'"~="" {
	di in red "Using `oldgroup' as Group Variable"
	local group `oldgroup'
	}

if "`last'"=="last" & "`r(colsig)'"=="" & "`r(comm)'"=="" {
	di as err `"Error: No "Last" Configuration Set Present"'
	di as err "Using Full Configuration Set"
	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 y: word 1 of `v'
	local sets: list v - y
	  local nonvar: list v - vlist
        foreach con of local nonvar {
		cnfgen `con'
	  }
          unab sets: `sets'
}

foreach var of local sets {
	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 set "`r(sets)'"
	if "`usereduction'"=="usereduction" {
		local start `r(reducsols)'
		local set `r(reducsols)'
		}
	local commsets `r(comm)'
	if "`last'" == "last"{
		if "`commsets'"=="" {
	 		local set `r(colsig)'
			}
			else {
	 		local set `r(comm)'
			}
	}
	local y = r(y)
	capture myboolean `start'
		if _rc>0{
			local nonvar `set'
        		foreach con of local nonvar {
			cnfgen `con'
			}
		}
	unab sets: `set'
		mark nomis
		capture markout nomis `start'
		qui foreach var of local sets {
		capture replace `var'=. if nomis==0
		}
	qui drop nomis
}

qui tab `group'
	if `r(r)' ~=2 {
	di as err "Error: Group Variable Must Be 2 Categories"
	keep `vlist'
	exit
	}

qui ratio (`group'/`y'), over(`group')
local groups = e(over_labels)
local group1 : word 1 of `groups'
local group2 : word 2 of `groups'
local group1 : subinstr local group1 " " "", all
local group2 : subinstr local group2 " " "", all

qui foreach var of local sets {
	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 Test"
		local sets: list sets - toex
	}
di
di in white "{ul:Configuration Mean `group1' vs Configuration Mean `group2'}"
di as text %-6s "Set", %12s "CMean`group1'", %16s "CMean`group2'", %9s "T", %9s "P"

qui su `group'
local firstcat = r(min)
local seccat = r(max)
foreach var of local sets {
	qui su `y' [aw=`var'] if `group'==`firstcat' & `touse'
	local `var'mean1 = r(mean)
	local `var'sd1 = r(sd)
	local `var'obs1 = r(N)
	qui su `y' [aw=`var'] if `group'==`seccat' & `touse'
	local `var'mean2 = r(mean)
	local `var'sd2 = r(sd)
	local `var'obs2 = r(N)
   qui ttesti ``var'obs1' ``var'mean1' ``var'sd1' ``var'obs2' ``var'mean2' ``var'sd2'
	if "`sigonly'" == "sigonly" {
	  		local p = r(p)
	  		if `p' < `slevel' {
        			if "`greater'"== "col1" {
				if r(mu_1)> r(mu_2) {
			  		di as res %-6s "`var'", %10.3f r(mu_1), %15.3f r(mu_2), %14.2f r(t), %9.3f r(p)
						local `test'colsig "``test'colsig' `var'"
					}
			    		}
		  	if "`greater'" == "col2" {
				if r(mu_1) < r(mu_2) {
			  		di as res %-6s "`var'", %10.3f r(mu_1), %10.3f r(mu_2), %14.2f r(t), %9.3f r(p)
						local `test'colsig "``test'colsig' `var'"
					}
                    		}
                 if "`greater'" == "" {
				di as res %-6s "`var'", %10.3f r(mu_1), %15.3f r(mu_2), %14.2f r(t), %9.3f r(p)
					local `test'colsig "``test'colsig' `var'"
				}
				}
				}
 	else {
		  if "`greater'"== "col1" {
			if r(mu_1)> r(mu_2) {
			 di as res %-6s "`var'", %10.3f r(mu_1), %15.3f r(mu_2), %14.2f r(t), %9.3f r(p)
				local `test'colsig "``test'colsig' `var'"
			    }
			    }
		  if "`greater'" == "col2" {
			if r(mu_1) < r(mu_2) {
			  di as res %-6s "`var'", %10.3f r(mu_1), %15.3f r(mu_2), %14.2f r(t), %9.3f r(p)
				local `test'colsig "``test'colsig' `var'"
			    }
			    }
		     if "`greater'" == "" {
			 di as res %-6s "`var'", %10.3f r(mu_1), %15.3f r(mu_2), %14.2f r(t), %9.3f r(p)
				local `test'colsig "``test'colsig' `var'"
				}
			      }
	  }

return local colsig ``test'colsig'
return local group `oldgroup'
return local sets `sets'
return local start `start'
return local y `y'
keep `vlist'
end