*! mean2 1.0.1 26jan2010 by roywada@hotmail.com
*! Makes a table of estimate means with subpopulation differences
*! Variable over must be binary

prog def mean8
syntax varlist(min=1) using/ [if] [in] [pweight aweight iweight] , over(str asis)  /*
	*/ [svy * vce(str asis) CLuster(str asis)] [diffonly meanonly svy replace excel LABel labelvar(str asis)]
version 9

qui {

*create label
capture sum `labelvar'
if _rc~=0 {
local idname `labelvar'
}
else {
sum `labelvar' `if'
local labval=r(mean)
local labname: value label `labelvar'
local idname: label `labname' `labval'
}

versionSet

preserve

if "`svy'"=="svy" {
	local svy "svy:"
}

marksample touse

tab `over',matrow(post_vals)
local post_num=r(r)

local labval1=post_vals[1,1]
local labval2=post_vals[2,1]
local labname1: value label `over'
local 1: label `labname1' `labval1'
local 2: label `labname1' `labval2'


tempname coef0 se0 coef1 se1 coef se

foreach var in `varlist' {
	if `version'>=11 {
		capture `svy' mean `var' `if' `in' [`weight'`exp'] , over(`over') vce(`vce') cl(`cl')
	}
	else {
		capture `svy' mean `var' `if' `in' [`weight'`exp'] , over(`over') cl(`cl')
	}

	capture nlcom ([`var']`1')
	qui mat B=r(b)
	qui mat V=r(V)
	if B[1,1]~=.&V[1,1]~=. {
		qui mat `coef0' = nullmat(`coef0') \ r(b)
		qui mat `se0' = nullmat(`se0') \ sqrt(V[1,1])
	}
	else {
	qui mat `coef0' = nullmat(`coef0') \ [-999]
	qui mat `se0' = nullmat(`se0') \ [-999]
	}

	capture nlcom ([`var']`2')
	qui mat B=r(b)
	qui mat V=r(V)

	if B[1,1]~=.&V[1,1]~=. {
		qui mat `coef1' = nullmat(`coef1') \ r(b)
		qui mat `se1' = nullmat(`se1') \ sqrt(V[1,1])
	}
	else {
		qui mat `coef1' = nullmat(`coef1') \ [-999]
		qui mat `se1' = nullmat(`se1') \ [-999]
	}


	capture nlcom ([`var']`2' - [`var']`1')
	qui mat B=r(b)
	qui mat V=r(V)
	if B[1,1]~=.&V[1,1]~=. {
		qui mat `coef' = nullmat(`coef') \ r(b)
		qui mat `se' = nullmat(`se') \ sqrt(V[1,1])
	}
	else {
		qui mat `coef' = nullmat(`coef') \ [-999]
		qui mat `se' = nullmat(`se') \ [-999]
	}

	noi disp "."

}
*end loop by var


*if to block mean columns
if "`meanonly'"=="meanonly" {
	qui mkest, replace bmat(`coef0') vmat(`se0') depvar(`1') /*
		*/ cmd(correlation) noobs noesample matname(`varlist') df_r(`r(df)')

	outreg2 using `using'.txt, nonotes noobs `replace' `excel' `label' ctitle("`idname'",`1')

	qui mkest, replace bmat(`coef1') vmat(`se1') depvar(`2') /*
		*/ cmd(correlation) noobs noesample matname(`varlist') df_r(`r(df)')

	outreg2 using `using'.txt, nonotes noobs append `excel' `label'  ctitle("`idname'",`2')
}
*end loop to block mean columns

if "`diffonly'"=="diffonly" {

	qui mkest, replace bmat(`coef') vmat(`se') depvar(`labelA') /*
		*/ cmd(correlation) noobs noesample matname(`varlist') df_r(`r(df)')

	outreg2 using `using'.txt, nonotes noobs `replace' `excel' `label'  ctitle("`idname'","Difference")
}
*end no diff

if "`meanonly'"~="meanonly"&"`diffonly'"~="diffonly" {
	qui mkest, replace bmat(`coef0') vmat(`se0') depvar(`1') /*
		*/ cmd(correlation) noobs noesample matname(`varlist') df_r(`r(df)')

	outreg2 using `using'.txt, nonotes noaster noobs `replace' `excel' `label' ctitle("`idname'",`1')

	qui mkest, replace bmat(`coef1') vmat(`se1') depvar(`2') /*
		*/ cmd(correlation) noobs noesample matname(`varlist') df_r(`r(df)')

	outreg2 using `using'.txt, nonotes noobs noaster append `excel' `label'  ctitle("`idname'",`2')

	qui mkest, replace bmat(`coef') vmat(`se') depvar(`labelA') /*
		*/ cmd(correlation) noobs noesample matname(`varlist') df_r(`r(df)')

	outreg2 using `using'.txt, nonotes noobs append `excel' `label'  ctitle("`idname'","Difference")

	}


} /* qui */

*scale variables back
restore

end



* mkest 1.0.1 14jan2010 by roywada@hotmail.com
* Convert variables to estimates matrix
program def mkest, eclass
version 8

syntax [if] [in], replace [b(str asis) v(str asis) bmat(str asis) /*
	*/ vmat(str asis) depvar(str asis) cmd(str asis) obs(numlist) /*
	*/ NOESAMPLE NOOBS matname(str asis) df_r(str asis)]

tempname ebmat eVmat
marksample touse

if "`depvar'"=="" {
	local depvar `b'
}
if "`cmd'"=="" {
	local cmd `depvar'
}

if "`bmat'"=="" {
	* get from variables
	mkmat `b' if `touse', matrix(`ebmat') nomiss
	mat `ebmat'=`ebmat''

	mkmat `v' if `touse', matrix(`eVmat') nomiss
	mat `eVmat'=`eVmat''
	mat `eVmat'=(`eVmat''*`eVmat')
}
else {
	* get from matrices
	mat `ebmat'=`bmat''

	mat `eVmat'=`vmat''
	mat `eVmat'=(`eVmat''*`eVmat')
}

if "`obs'"=="" {
	local obs=colsof(`ebmat')
}
if "`noobs'"~="" {
	local obs .
}
if "`matname'"~="" {
	matname `ebmat' `matname', col(.)
	matname `eVmat' `matname', row(.)
	matname `eVmat' `matname', col(.)
}


tempvar sample
cap gen `sample'=1 if `b'~=. & `touse'
cap gen `sample'=1 if `b'~="" & `touse'
cap qui replace `sample'=0 if `sample'==.

	if "`replace'"=="replace" {
		eret clear
		if "`noesample'"=="" {
			eret post `ebmat' `eVmat', esample(`sample')
		}
		else {
			eret post `ebmat' `eVmat'
		}
		eret local depvar `"`depvar'"'
		eret local cmd "`cmd'"
		eret scalar N = `obs'
		if "`df_r'"~="" {
			eret scalar df_r = `df_r'
		}
	}

end


* 03nov2009 from outreg2.ado
prog define versionSet
        * sends back the version as c_local
        version 7.0

        * invisible to Stata 7
        cap local Version7 `c(stata_version)'
        c_local Version7 `Version7'

        if "`Version7'"=="" {
                * it is version 7
                c_local version 7
        }
        else if `Version7'>=8.2 {
                version 8.2
                c_local version 8.2
        }

        if "`Version7'"=="" {
                c_local bind ""
        }
        else {
                c_local bind "bind"
        }
end
exit

1.0.0 26jan2010

1.0.1 30jan2010
if in weight fixed
passthru to both outreg2 calls

example
sysuse auto, clear
percent2 trunk mpg if price<5000 using myfile.txt, over(foreign) replace cat(Domestic Foreign) dec(2) label