*! 1.0.4 NJC 30 May 1999
* 1.0.3 NJC 16 May 1999
* 1.0.2 NJC 9 May 1999
* 1.0.1 NJC 3 May 1999
* 1.0.0 NJC 1 March 1999
program define cihplot
    version 6.0
    #delimit ;
    syntax varlist(numeric) [if] [in] [aweight fweight]
    [ , BY(varname) LEVel(integer $S_level) Poisson BINomial SOrt(str)
    Exposure(varname) Symbol(str) TItle(str) Total SHOWN TWOtier * ]
    ;
    #delimit cr

    if "`total'" != "" & "`by'" == "" {
        di "by( ) option required with total"
        exit 198
    }

    tokenize `varlist'
    local nvars : word count `varlist'

    if `nvars' > 1 & "`by'" != ""  {
        di in r "too many variables specified"
        exit 103
    }

    if "`exposure'" != "" { local exposur "e(`exposure')" }

    if "`by'" != "" {
        local bylab : value label `by'
        qui tab `by' `if' `in', miss
        local nobs = r(r) + ("`total'" == "total")
    }
    else local nobs = `nvars'

    if `nvars' > _N {
        tempvar orig
        gen byte `orig' = 1
        qui set obs `nvars'
    }
    local extra = "`orig'" != ""

    tempvar touse a group n mean l`level' u`level' which l50 u50
    tempname lbl

    qui {
        gen byte `which' = _n in 1/`nobs'
	gen `n' = .
        gen `mean' = .
        label var `mean' "mean"
        gen `l`level'' = .
        label var `l`level'' "lower `level'%"
        gen `u`level'' = .
        label var `u`level'' "upper `level'%"
        if "`twotier'" != "" {
            gen `l50' = .
            label var `l50' "lower 50%"
            gen `u50' = .
            label var `u50' "upper 50%"
        }
    }

    local I = 1
    qui while "`1'" != "" {
        mark `touse' `if' `in'
        markout `touse' `1'

        sort `touse' `by' `1'
        by `touse' `by' : gen byte `group' = _n == 1 if `touse'
        replace `group' = sum(`group')
        local max = `group'[_N]
        count if !`touse'
        local J = 1 + r(N)
        local j = 1

        while `j' <= `max' {
            count if `group' == `j'
            local obs = r(N)
	    replace `n' = r(N) if `which' == `I'
            ci `1' [`weight' `exp'] if `group' == `j', /*
            */ `exposure' `binomial' `poisson' l(`level')
            replace `mean' = $S_3 if `which' == `I'
            replace `l`level'' = $S_5 if `which' == `I'
            replace `u`level'' = $S_6 if `which' == `I'
            if "`twotier'" != "" {
                ci `1' [`weight' `exp'] if `group' == `j', /*
                */ `exposure' `binomial' `poisson' l(50)
                replace `l50' = $S_5 if `which' == `I'
                replace `u50' = $S_6 if `which' == `I'
            }

            if "`by'" != "" {
                local name = `by'[`J']
                if "`name'" == "" | "`name'" == "." {
                    local name "missing"
                }
                else if "`bylab'" != "" {
                    local name : label `bylab' `name'
                }
            }
            else {
	   	local name "`1'"
		local vlbl : variable label `1'
		if "`vlbl'" != "" { local name "`vlbl'" }
	    }
            label def `lbl' `I' "`name'", modify

            local I = `I' + 1
            local J = `J' + `obs'
            local j = `j' + 1
        }

        mac shift
        if `nvars' > 1 { drop `touse' `group' }
    }

    qui if "`total'" == "total" {
        ci `varlist' [`weight' `exp'] if `touse', /*
        */ `exposure' `binomial' `poisson' l(`level')
	replace `n' = $S_1 if `which' == `I'
        replace `mean' = $S_3 if `which' == `I'
        replace `l`level'' = $S_5 if `which' == `I'
        replace `u`level'' = $S_6 if `which' == `I'

        if "`twotier'" != "" {
            ci `varlist' [`weight' `exp'] if `touse', /*
            */ `exposure' `binomial' `poisson' l(50)
            replace `l50' = $S_5 if `which' == `I'
            replace `u50' = $S_6 if `which' == `I'
        }

        label def `lbl' `I' "Total", modify
        local I = `I' + 1
    }

    label val `which' `lbl'

    if `"`title'"' == `""' & `nvars' == 1 {
        local title : variable label `varlist'
        if "`title'" == "" { local title "`varlist'" }
    }
    if `"`title'"' != `""' { local title `"ti(`title')"' }

    if "`sort'" == "" { local sort "sort(`which')" }
    else local sort "sort(`sort')"

    if "`shown'" != "" { local shown "rl(`n')" }

    if "`twotier'" != "" {
        if "`symbol'" == "" { local symbol "|,O,|" }
        hplot `l`level'' `l50' `mean' `u50' `u`level'' if `which' < `I', /*
        */ sy(`symbol') range line l(`which') `title' `sort' `shown' `options'
    }
    else {
        if "`symbol'" == "" { local symbol "|O|" }
        hplot `l`level'' `mean' `u`level'' if `which' < `I', /*
        */ sy(`symbol') range line l(`which') `title' `sort' `shown' `options'
    }

    label drop `lbl'
    if `extra' { qui keep if `orig' == 1 }
end