*! version 1.0.1 Stephen P. Jenkins, Dec 1998 STB-48 sg104 *! Quantiles, shares of total, and (generalised) Lorenz ordinates *! Syntax: sumdist , ngps(# quantile gps) qgp(name) program define sumdist version 5.0 local varlist "req ex max(1)" local if "opt" local in "opt" local options "Ngps(int 10) QGP(string)" local weight "aweight fweight" parse "`*'" parse "`varlist'", parse (" ") local inc "`1'" if (`ngps' <= 0 | `ngps' > 100) { di in r "# quantile groups should be integer in range (0,100]" exit 198 } tempvar nk vk fik last wi touse qrel meanyk /* */ incsh cuincsh gl qtile badinc if "`qgp'" ~= "" {confirm new variable `qgp' } else {tempvar qgp} if "`weight'" == "" {ge `wi' = 1} else {ge `wi' `exp'} mark `touse' `if' `in' markout `touse' `varlist' set more 1 quietly { count if `inc' < 0 & `touse' local ct = _result(1) if `ct' > 0 { noi di " " noi di in blue "Warning: `inc' has `ct' values < 0." _c noi di in blue " Used in calculations" } count if `inc' == 0 & `touse' local ct = _result(1) if `ct' > 0 { noi di " " noi di in blue "Warning: `inc' has `ct' values = 0." _c noi di in blue " Used in calculations" } /* reinstate this bit if want to exclude obs with `inc' <=0 from calculations */ /* ge `badinc' = 0 replace `badinc' =. if `inc' <= 0 markout `touse' `badinc' */ sum `inc' [w = `wi'] if `touse', de local sumwi = _result(2) local meany = _result(3) local medy = _result(10) xtile `qgp' = `inc' [w= `wi'] if `touse', nq(`ngps') sort `qgp' `touse' `inc' by `qgp': ge `last' = (_n==_N) if `touse' egen `nk' = sum(`wi') if `touse', by(`qgp') ge `vk' = `nk'/`sumwi' if `touse' ge `fik' = `wi'/`nk' if `touse' egen `meanyk' = sum(`fik'*`inc') if `touse', by(`qgp') sort `qgp' `touse' `inc' by `qgp': ge `qtile' = `inc' if `last' & `touse' & `qgp'~=. by `qgp': ge `qrel' = 100 * `qtile'/`medy' if `touse' ge `incsh' = 100 * `vk' * `meanyk'/`meany' if `touse' & `last' sort `qgp' ge `cuincsh' = sum(`incsh') if `touse' ge `gl' = `cuincsh'*`meany'/100 if `touse' replace `qtile' = . if `qgp' == `ngps' replace `qrel' = . if `qgp' == `ngps' lab var `qgp' "Quantile group" lab var `qtile' "Quantile" lab var `qrel' "% of median" lab var `incsh' "Share, %" lab var `cuincsh' "L(p), %" lab var `gl' "GL(p)" } di "Distributional summary statistics, `ngps' quantile groups" tabdisp `qgp' if `last' & `touse', /* */ c(`qtile' `qrel' `incsh' `cuincsh' `gl') f(%9.2f) di in gr "Share = quantile group share of total `inc'; " di in gr "L(p)=cumulative group share; GL(p)=L(p)*mean(`inc')" end