Code:
program sumweight, rclass
syntax varlist(numeric) [if] [in] [aw]
marksample sample
tempvar y exp
tempname num den
tokenize `varlist'
quietly {
if "`weight'" != "" {
tempvar wvar
local wgt "[`weight'=`wvar']"
}
mark `wgt'
su `y' `wgt' if `sample'
scalar `num' = r(N)
return scalar obsw = `num'
}
end
sysuse auto
sumweight mpg [aw = weight]
0 Response to Write a stata program to control for sample weight?
Post a Comment