*! version 6.0.0  17dec1999   (STB-52 sg35.1)
program define robvar, rclass
        version 6
	syntax varname [if] [in] , by(varname)
	marksample touse
	qui {
		count if `touse'
		local N=r(N)
		tempvar xbar
		egen double `xbar'=mean(`varlist') if `touse',by(`by')
		DoCal `varlist' `by' `xbar' `touse'
		drop `xbar'
		tempname num den ng num w0 w50 w10 p df1 df2
		scalar `num'=`s(num)'
		scalar `den'=`s(den)'
		scalar `ng'=`s(ng)'
		scalar `num'=`num'/(`N'-`ng' )
		scalar `w0'=`den'/`num'
		scalar `p'=fprob((`ng' -1),(`N'-`ng' ),`w0')
		scalar `df1'=`ng'-1
		scalar `df2'=`N'-`ng'
		noi tab `by' if `touse',sum(`varlist')
		ret scalar df_2=`df2'
		ret scalar df_1=`df1'
		ret scalar p_w0=`p'
		ret scalar w0=`w0'

		egen double `xbar'=median(`varlist') if `touse',by(`by')
		DoCal `varlist' `by' `xbar' `touse'
		drop `xbar'
		scalar `num'=`s(num)'
		scalar `den'=`s(den)'
		scalar `ng'=`s(ng)'
		scalar `num'=`num'/(`N'-`ng' )
		scalar `w50'=`den'/`num'
		scalar `p'=fprob((`ng' -1),(`N'-`ng' ),`w50')
		ret scalar p_w50=`p'
		ret scalar w50=`w50'

		tempvar  p10 p90 m10
		egen double `p10'=pctile(`varlist') if `touse',p(10) by(`by')
		egen double `p90'=pctile(`varlist') if `touse',p(90) by(`by')
		egen double `m10'=mean(`varlist') if `varlist'>=`p10' /*
		*/ & `varlist'<=`p90' & `touse',by(`by')
		egen double `xbar'=min(`m10') if `touse',by(`by')
		drop `m10' `p10' `p90'
		DoCal `varlist' `by' `xbar' `touse'
		drop `xbar'
		scalar `num'=`s(num)'
		scalar `den'=`s(den)'
		scalar `ng'=`s(ng)'
		scalar `num'=`num'/(`N'-`ng' )
		scalar `w10'=`den'/`num'
		scalar `p'=fprob((`ng' -1),(`N'-`ng' ),`w10')
		ret scalar p_w10=`p'
		ret scalar w10=`w10'
		ret scalar N=`N'
		noi di _n in gr "W0=   " in ye `w0' in gr "   df(" in ye /*
		*/ `df1' ", " `df2' in gr ") ""    Pr > F = " in ye return(p_w0)
		noi di _n in gr "W50=  " in ye `w50' in gr "   df(" in ye /*
		*/ `df1' ", " `df2' in gr ")     Pr > F = " in ye return(p_w50)
		noi di _n in gr "W10=  " in ye `w10' in gr "   df(" in ye /*
		*/ `df1' ", " `df2' in gr ")     Pr > F = " in ye return(p_w10)
	}
 end

prog def DoCal, sclass
	args x g xbar touse
		tempvar zij zbari ni den s num
		gen double `zij'=abs(`x'-`xbar') if `touse'
		egen double `zbari'=mean(`zij') if `touse',by(`g')
		sum `zij' if `touse', meanonly
		local zbarp=r(mean)
		sort `touse' `g'
		by `touse' `g': gen double `ni'=_N
		gen double `den'=`ni'*((`zbari'-`zbarp')^2) if `touse'
		drop `ni'
		sort `touse' `g'
		by `touse' `g':gen double `s' =`den' if _n==1 & `touse'
		drop `den'
		sum `s' if `touse',meanonly
		local ng =r(N)
		local sd =r(sum)
		local den=`sd' /(`ng' -1)
		gen double `num'=(`zij'-`zbari')^2 if `touse'
		sum `num' if `touse', meanonly
		drop `num' `zij' `zbari'
		local num=r(sum)
		sret local den= `den'
		sret local num= `num'
		sret local ng= `ng'
end