*! 1.0.0 NJC 11 May 1999
program define civplot
    version 6.0
    #delimit ;
    syntax varlist(numeric) [if] [in] [aweight fweight]
    [ , BY(varname) LEVel(integer $S_level) Poisson BINomial Total
    Exposure(varname) Symbol(str) Connect(str) T1title(str) * ]
    ;
    #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 mean l`level' u`level' which
    tempname lbl

    qui {
        gen byte `which' = _n in 1/`nobs'
        gen `mean' = .
        gen `l`level'' = .
        gen `u`level'' = .
    }

    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)
            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 "`by'" != "" {
                local name = `by'[`J']
                if "`name'" == "" | "`name'" == "." {
                    local name "missing"
                }
                else if "`bylab'" != "" {
                    local name : label `bylab' `name'
                }
            }
            else {
	   	local name "`1'"
* no good because labels get truncated to 8 chars
*		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 `mean' = $S_3 if `which' == `I'
        replace `l`level'' = $S_5 if `which' == `I'
        replace `u`level'' = $S_6 if `which' == `I'

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

    label val `which' `lbl'

    if `nvars' >= 1 {
    	label var `which' "`varlist'"
        label var `mean' "means and `level'% confidence limits"
    }

    if "`by'" != "" {
        _crcslbl `which' `by'
	_crcslbl `mean' `varlist'
	if `"`t1title'"' == `""' {
	    local t1title "means and `level'% confidence limits"
	}
    }

    if `"`t1title'"' != `""' { local t1title `"t1("`t1title'")"' }

    local Im1 = `I' - 1
    if "`symbol'" == "" { local symbol "Oii" }
    if "`connect'" == "" { local connect ".II" }

    graph `mean' `l`level'' `u`level'' `which' if `which' < `I', /*
    */ sy(`symbol') c(`connect') xla(1/`Im1') `t1title' `options'

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