*! version 2.1.5  12jan2000
program define prtesti, rclass
	version 5.0
	global S_6		/* will be z	*/
	parse "`*'", parse(" ,")
	cap confirm number `4'
	if "`4'" != "" & _rc == 0 {
		confirm integer number `1'
		confirm number `2'
		confirm integer number `3'
		confirm number `4'
		local n1 `1'
		local p1 `2'
		local n2 `3'
		local p2 `4'

		if `p1' > 1  & `p1' < `n1' { local p1 = `p1'/`n1' }
		if `p2' > 1  & `p2' < `n2' { local p2 = `p2'/`n2' }

		if `p1' > 1 | `p1' < 0 {
			noi di in red "`p1' not in [0,1]"
			exit 198
		}
		if `p2' > 1 | `p2' < 0 {
			noi di in red "`p2' not in [0,1]"
			exit 198
		}
		mac shift 4
		local n = `n1'+`n2'
		local p = (`n1'*`p1'+`n2'*`p2')/(`n1'+`n2')
		local dp = `p1'-`p2'
		if `n1'<=1 { error 2001 }

		local options "Yname(string) Xname(string) integer"
		local options "`options' Level(integer $S_level)"
		parse "`*'"
		if `level' < 10 | `level' > 99 {
			error 198
		}
		global S_level = `level'
		if `"`xname'"'=="" { local xname "x" }
		else local xname = trim(substr(trim(`"`xname'"'),1,8))
		if `"`yname'"'=="" { local yname "y" }
		else local yname = trim(substr(trim(`"`yname'"'),1,8))
		di

		local c1 = 53 - length(`"`xname'"')
		local c2 = 53 - length(`"`yname'"')

		di in gr "Two-sample test of proportion" _col(`c1') /*
		*/ in ye `"`xname'"' in gr _col(53) ": Number of obs = " /*
		*/ in ye %8.0g `n1'
		di in gr _col(`c2') in ye `"`yname'"' in gr /*
		*/ _col(53) ": Number of obs = " in ye %8.0g `n2'
		local s1 = sqrt(`p1'*(1-`p1')/(`n1'))
		local s2 = sqrt(`p2'*(1-`p2')/(`n2'))
		_ttest1 `"`xname'"' `n1' `p1' `s1' normal `level'
		_ttest2 `"`yname'"' `n2' `p2' `s2' normal `level'
		di in gr "---------+" _dup(68) "-"

		local sp = sqrt(`p1'*(1-`p1')/`n1'+`p2'*(1-`p2')/`n2')
		local spp = sqrt(`p'*(1-`p')/`n1'+`p'*(1-`p')/`n2')

		tnew diff `n' `dp' `spp' `sp'
		di in gr _dup(78) "-"

		ret scalar N_1 = `n1'
		ret scalar P_1 = `p1'
		ret scalar N_2 = `n2'
		ret scalar P_2 = `p2'
		ret scalar z = (`p1'-`p2')/`spp'

		/* Double saves */
		global S_1  "`return(N_1)'"
		global S_2  "`return(P_1)'"
		global S_3  "`return(N_2)'"
		global S_4  "`return(P_2)'"
		global S_6  "`return(z)'"

                di
                local c1 = 17 - int((length(`"`xname'"') + /*
		*/ length(`"`yname'"'))/2)
                di in gr _col(`c1') "Ho: proportion(" in ye `"`xname'"' in gr /*
                */ ") - proportion(" in ye `"`yname'"' in gr ") = diff = 0"
                di
                di in gr _col(8) "Ha: diff" in gr " < " in ye "0" in gr /*
                */ _col(32) "Ha: diff" in gr " ~= " in ye "0" in gr     /*
                */ _col(58) "Ha: diff" in gr " > " in ye "0"

                di in gr  _col(9) "z = " in ye %6.3f return(z) /*
                */ in gr _col(34) "z = " in ye %6.3f return(z) /*
                */ in gr _col(59) "z = " in ye %6.3f return(z)

		local p2 = 2*(normprob(-abs(return(z))))
		if return(z) < 0 {
			local p1 = `p2'/2
			local p3 = 1 - `p1'
		}
		else {
			local p3 = `p2'/2
			local p1 = 1 - `p3'
		}

                di in gr _col(7) "P < z = " in ye %6.4f `p1' /*
                */ in gr _col(31) "P > |z| = " in ye %6.4f `p2' /*
                */ in gr _col(57) "P > z = " in ye %6.4f `p3'

		exit
	}
	else {
		confirm integer number `1'
		confirm number `2'
		confirm number `3'
		local n1 `1'
		local p1 `2'
		local p2 `3'

		if `p1' > 1  & `p1' < `n1' { local p1 = `p1'/`n1' }
		if `p1' > 1 | `p1' < 0 {
			noi di in red "`p1' not in [0,1]"
			exit 198
		}
		if `p2' > 1 | `p2' < 0 {
			noi di in red "`p2' not in [0,1]"
			exit 198
		}
		mac shift 3
		local n = `n1'
		local dp = `p1'-`p2'
		if `n1'<=1 { error 2001 }

		local options "Yname(string) Xname(string) "
		local options "`options' Level(integer $S_level)"
		parse "`*'"
		local yname = trim(substr(trim(`"`yname'"'),1,8))
		if `level' < 10 | `level' > 99 {
			error 198
		}
		global S_level = `level'
		local xname "x"
		local c1 = 53 - length(`"`xname'"')
		di
		di in gr "One-sample test of proportion" /*
		*/ _col(`c1') in ye `"`xname'"' in gr _col(53) /*
		*/ ": Number of obs = " /*
		*/ in ye %8.0g `n1'

		local varlist `"`xname'"'
		local exp "`p2'"
		local s = sqrt(`p1'*(1-`p1')/(`n1'))
		_ttest1 `"`xname'"' `n1' `p1' `s' normal `level'
		di in gr _dup(78) "-"
		di

                local c1 = 27 - int((length(`"`xname'"'))/2)
                di in gr _col(`c1') "Ho: proportion(" in ye `"`xname'"' in gr /*
                */ ") = " in ye `exp'
                di
                di in gr _col(8) "Ha: `varlist' < " in ye `exp' in gr /*
                */ _col(32) "Ha: `varlist' ~= " in ye `exp' in gr     /*
                */ _col(58) "Ha: `varlist' > " in ye `exp'


		return scalar N_1 = `n1'
		return scalar P_1 = `p1'
		return scalar z = (`p1'-`exp')/sqrt(`exp'*(1-`exp')/`n1')

		/* Double Saves */
		global S_1  "`return(N_1)'"
		global S_2  "`return(P_1)'"
                global S_6  "`return(z)'"

                di in gr  _col(9) "z = " in ye %6.3f `return(z)' /*
                */ in gr _col(34) "z = " in ye %6.3f `return(z)' /*
                */ in gr _col(59) "z = " in ye %6.3f `return(z)'

                local pp2 = 2*normprob(-abs(`return(z)'))
                if `return(z)' < 0 {
                        local pp1 = `pp2'/2
                        local pp3 = 1 - `pp1'
                }
                else {
                        local pp3 = `pp2'/2
                        local pp1 = 1 - `pp3'
                }

                di in gr _col(7) "P < z = " in ye %6.4f `pp1' /*
                */ in gr _col(30) "P > |z| = " in ye %6.4f `pp2' /*
                */ in gr _col(57) "P > z = " in ye %6.4f `pp3'

		exit
	}
end


program define tnew
	local name "`1'"
	local n "`2'"
	local mean "`3'"
	local se "`4'"
	local cse "`5'"

	local tval = `mean'/`se'
	*local tval = abs(`tval')
	local pval = 2*(1 - normprob(abs(`tval')))

	local vval = (100 - (100-$S_level)/2)/100
	local beg = 9 - length("`name'")
	noi di in gr _col(`beg') "`name'" /*
		*/ in gr _col(10) "|" in ye /*
		*/ _col(12) %9.0g  `mean'   /*
		*/ _col(24) %9.0g  `cse'     /*
		*/ _col(58) %9.0g  `mean'-invnorm(`vval')/*
		*/ *`cse'   /*
		*/ _col(70) %9.0g  `mean'+invnorm(`vval')*`cse'
	noi di in gr _col(`beg') "" /*
		*/ in gr _col(10) "|" in ye /*
		*/ in gr _col(13) "under Ho:"   /*
		*/ in ye _col(24) %9.0g  `se'     /*
		*/ _col(35) %8.0g  `tval'   /*
		*/ _col(46) %6.4f  `pval'
end
exit
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
   _cons |   6165.257   342.8719     17.981   0.000       5481.914      6848.6