program def barplot
*! 1.3.0 NJC 26 January 1999
* 1.2.0 NJC 16 July 1998
* 1.1.0 NJC 14 April 1998
* 1.0.0 NJC 30 March 1998
    version 5.0
    local varlist "min(2)"
    local if "opt"
    local in "opt"
    local options "Width(str) Base(str) Months XLAbel(string)"
    local options "`options' BY(str) JOIN *"
    parse "`*'"
    parse "`varlist'", parse(" ")
    local nvars : word count `varlist'
    local x ``nvars''
    local `nvars'
    local ylist "`*'"

    tempvar Width Base touse id
    mark `touse' `if' `in'
    markout `touse' `x'
    if `nvars' == 2 { markout `touse' `1' }

    if "`width'" != "" {
        capture confirm variable `width'
        if _rc { confirm number `width' }
    }
    else local width 1
    gen `Width' = `width'

    local basevar 0
    if "`base'" != "" {
        capture confirm variable `base'
        if _rc { confirm number `base' }
        else local basevar 1
    }
    else local base 0
    gen `Base' = `base'

    preserve

    qui {
        keep if `touse'
        keep `by' `varlist' `Width' `Base'
        sort `by' `varlist' `Width' `Base'
        by `by' `varlist' `Width' `Base' : keep if _n == 1
        expand 6
        if `basevar' {
            tempvar minx
            egen `minx' = min(`x'), by(`Base')
            replace `minx' = - `minx'
        }
        if "`join'" != "" { sort `by' `minx' `Base' `x' `ylist'}
        else gsort `by' `minx' `Base' - `x' `ylist'
        gen byte `id' = mod(_n,6)
        local i 1
        while `i' < `nvars' {
            replace ``i'' = `Base' if (`id' <= 1 | `id' >= 4) & ``i'' < .
            local i = `i' + 1
        }
        replace `x' = `x' - 0.5 * `Width' /*
         */ if `id' == 1 | `id' == 2 | `id' == 5
        replace `x' = `x' + 0.5 * `Width' /*
         */ if `id' == 3 | `id' == 4 | `id' == 0
    }

    if "`xlabel'" == ""  {
        if "`months'" != "" {
            local xlabel "xla(1,2,3,4,5,6,7,8,9,10,11,12)"
        }
        else local xlabel "xla"
    }
    else local xlabel "xla(`xlabel')"

    local connect : di  _dup(20) "L"
    local symbol : di _dup(20) "i"

    if "`by'" != "" { local byby "by(`by')" }

    gra `varlist', c(`connect') sy(`symbol') `xlabel' `options' `byby'
end