program def barplot2
*! 1.0.0 NJC 13 Sept 1998
    version 5.0
    local varlist "req min(2) max(2)"
    local if "opt"
    local in "opt"
    local options "Width(str) Base(str) XLAbel(string)"
    local options "`options' BY(str) Error(string) *"
    parse "`*'"
    parse "`varlist'", parse(" ")
    local x `2'
    local y `1'

    tempvar Width Base touse id
    mark `touse' `if' `in'
    markout `touse' `varlist'

    if "`error'" != "" { confirm variable `error' }

    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' `error'
        sort `by' `varlist' `Width' `Base'
        by `by' `varlist' `Width' `Base' : keep if _n == 1
        expand 7
        if `basevar' {
            tempvar minx
            egen `minx' = min(`x'), by(`Base')
            replace `minx' = - `minx'
        }
        gsort `by' `minx' `Base' - `x' `y'
        gen `id' = mod(_n,7)
        replace `y' = `Base' if (`id' == 1 | `id' >= 4 ) & `y' < .
        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' == 6

        if "`error'" != "" {
                tempvar upper lower
                gen `upper' = `y' + `error' if `id' == 0
                gen `lower' = `y' - `error' if `id' == 0
                replace `y' = . if `id' == 0
        }
    }

    if "`xlabel'" == ""  { local xlabel "xla" }
    else { local xlabel "xla(`xlabel')" }

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

    gra `y' `upper' `lower' `x', c(LII) sy(iii) `xlabel' `options' `byby'
end