*! 3.0.0 Philippe Van Kerm - Stephen P. Jenkins 26 October 2004 (SJ 4-4: gr0001_1)
* touched by NJC 24 October 2004
*! 2.0 Philippe Van Kerm - Stephen P. Jenkins, 19 Feb 2001 (TSJ-1: gr0001)
*! 1.2 Stephen P. Jenkins - Philippe Van Kerm, Apr 1999 STB-49 sg107.1
* Syntax: glcurve y [fw aw] [if in], [GLvar(x1) Pvar(x2) SOrtvar(svar)
*            Lorenz RTIP(string) ATIP(string)
*            BY(gvar) SPlit GRaph REPLACE plot(plot) graph_options]

program glcurve, sort
	version 8.0
	syntax varname [if] [in] [fweight aweight] ///
		[, GLvar(string) Pvar(string) SOrtvar(varname) ///
		Lorenz RTIP(string) ATIP(string) ///
		BY(varname numeric) SPlit NOGRaph REPLACE PLOT(string asis) * ]

	tempvar inc cumwy cumw maxw badinc wi gl p
	tempname byname

	local graph = cond("`nograph'" != "", "", "graph")

	if "`by'" != "" {
		if "`graph'" != "" & "`split'" == "" {
			di as err "{p}split must be used to combine by()" ///
			" with a graph; nograph option assumed{p_end}"
			local graph
		}
	}
	else {
		if "`split'" != "" {
			di as err "{p}split must be combined with by(); " ///
			" split ignored{p_end}"
			local split
		}
	}

	if "`replace'" != "" {
		if "`split'" != "" & "`by'" != "" {
			local prefix "`glvar'"
			cap drop `prefix'_*
			cap drop `pvar'
		}
		else {
			cap drop `glvar'
			cap drop `pvar'
		}
	}

	if "`weight'" == "" qui gen byte `wi' = 1
	else qui gen `wi' `exp'

	marksample touse
	markout `touse' `sortvar' `by'

	if "`split'" == "" {
		if "`glvar'" != "" {
			confirm new variable `glvar'
			di as txt "new variable `glvar' created"
		}
		else tempvar glvar
	}
	else qui {
		if "`glvar'" == "" {
			tab `by' `by' if  `touse', matrow(`byname')
			local nv = rowsof(`byname')
			forval i = 1/`nv' {
				tempvar newvar`i'
			}
		}
		else {
			tab `by' `by' if  `touse', matrow(`byname')
			local prefix "`glvar'"
			local nv = rowsof(`byname')
			forval i = 1/`nv' {
				local suffix = `byname'[`i',1]
				local newvar`i' "`prefix'_`suffix'"
				confirm new variable `newvar`i''
   				di as txt "new variable `newvar`i'' created"
			}
		}
	}

	if "`pvar'" != "" {
		confirm new variable `pvar'
		di as txt "new variable `pvar' created"
	}
	else tempvar pvar

	qui gen `inc' = `varlist' if `touse'

	qui if "`atip'" != "" {
		if "`rtip'" != "" {
			di as err "cannot use options atip() and rtip() together"
			exit 198
		}
		if "`lorenz'" != ""{
			di as err "cannot use option atip() with lorenz"
			exit 198
		}
		replace `inc' = max(0,`atip' - `varlist') if `touse'
		if "`sortvar'" == "" local sortvar "`varlist'"
	}

	qui if "`rtip'" != "" {
		if "`lorenz'" != ""{
			di as err "cannot use option rtip() with lorenz"
			exit 198
		}
		replace `inc' = max(0,(`rtip' - `varlist')/`rtip') if `touse'
		if "`sortvar'" == "" local sortvar "`varlist'"
	}

	quietly {
		count if `inc' < 0 & `touse'
		if r(N) > 0 {
			noi di as txt _n "warning: `inc' has `r(N)' values < 0;" ///
			"used in calculations"
		}

		if "`by'" == "" {
			tempvar placebo
			gen byte `placebo' = 1
			local by "`placebo'"
		}

		if "`sortvar'" == "" local sortvar `inc'
		bysort `by' (`sortvar') : gen double `cumwy' = sum(`wi' * `inc') if `touse'
		by `by': gen double `cumw' = sum(`wi') if `touse'
		egen `maxw' = max(`cumw'), by(`by')
		gen double `pvar' = `cumw'/`maxw' if `touse'
		label variable `pvar' "Cumulative population proportion"

		if "`split'" == "" {
			gen `glvar' = `cumwy'/`maxw' if `touse'
			if "`lorenz'" != "" label var `glvar' "Lorenz(`varlist')"
			else label var `glvar' "GLorenz(`varlist')"
			if "`lorenz'" != ""{
				su `inc' [`weight' `exp'] if `touse', meanonly
				replace `glvar' = `glvar' / r(mean)
			}
			if "`graph'" != "" {
				twoway scatter `glvar' `pvar' if `touse', ///
				ms(i i) c(l l) `options'  || `plot'
			}
		}
		else {
			local lname : value label `by'
			forval i = 1/`nv' {
				bysort `by' (`sortvar'): ge `newvar`i'' = `cumwy'/`maxw' ///
					if `touse' & `by' == `byname'[`i',1]
				if "`lorenz'" != "" {
					su `inc' [`weight' `exp'] if `touse' & `by' == `byname'[`i',1], meanonly
					replace `newvar`i'' = `newvar`i'' / r(mean)
				}
				if "`lname'" != "" {
					local cl = `byname'[`i',1]
					local lab : label `lname' `cl'
					label variable `newvar`i'' "`varlist'[`lab']"
				}
				local listvar "`listvar' `newvar`i''"
				local legtext `"`legtext' `i' "`varlist'[`= `byname'[`i',1]']" "'
			}
			if "`graph'" != "" {
				if `nv' > 1 {
					local yti = ///
			cond("`lorenz'" != "", "Lorenz(groups of `by')", "GLorenz(groups of `by')")
					local yti "yti(`yti')"
				}
				twoway scatter `listvar' `pvar' if `touse', ///
				ms(i ..) c(l ..) `yti' legend(order(`legtext')) `options'  || `plot'
			}
		}
	}
end