*! version 1.0.0 ZWANG 04Jun1999
    program define gby
    version 6.0
    gettoken fby: 0, parse(" :") quotes
    gettoken sep: 0, parse(" :") quotes

    while `"`sep'"' != ":" & `"`sep'"' != "" {
        local left `"`left' `sep'"'
        gettoken sep 0:0, parse(" :") quotes
    }
    if `"`sep'"' != ":" {
        error 198
    }
    unab `fby':`fby'
    gettoken cmd 0:0, parse(" ")
    global FBY "`fby'"
    global S_CMD "`cmd'"

    if "`cmd'"=="kdensity" {
        _kdg6 `0'
        exit
    }
    else if substr("`cmd'", 1,2)=="gr" {
        _gr6 `0'
        exit
    }
    else {
        _eby6 `0'
        exit
    }
    end

    program define _gr6
    syntax varlist [if] [in] [, BY(varname) HIlow GName(str) /*
        */ Connect(str) Symbol(str) noLab *]
    marksample touse
    markout `touse' `by'
    preserve
    qui keep if `touse'
    token `varlist'
    local yvar "`1'"
    local warn in gr `"use "' in w `"gn"' in gr `"ame"'
    local warn1 in w `"(newname)"' in gr /*
        */ `" to label your group variable!"'

    if "`gname'"==""{local gname "`yvar'" }
    local gen1 "gen(`gname')"

    if "`hilow'"==""{ /* For scatter,box,bar,lines but high-low */
        qui sum $FBY, mean
        if length("`gname'`r(max)'")>8 {
di in r "name `gname' too long, use gname option with shorter name"
            exit
        }
        local xvar "`2'"
        qui separate `yvar', by($FBY) `gen1'
        if _rc==110 {local gen1 gen(_grp)
            qui separate `yvar', by($FBY) `gen1'
        }
        local ylist `r(varlist)'
        token `ylist'
        if "`lab'"=="nolab"{
            while "`1'"!="" {
                lab var `1'
                mac shift
            }
        }
        if "`by'" ~="" {
            sort `by' `xvar'
            local by1 "by(`by')"
        }

        if "`symbol'"~=""{local s1 s(`symbol')}
        if "`connect'"~=""{local c1 c(`connect')}
        $S_CMD `ylist' `xvar', `by1' `c1' `s1' `options'
    }
    else {                      /*for high-low chart*/
        local hi "`2'"
        local low "`3'"
        local xvar "`4'"
        qui separate `yvar', by($FBY) gen(_Grp) seq
        if _rc==110 {
            di `warn' `warn1'
            exit
        }
        local ylist `r(varlist)'
        token `ylist'
        if "`lab'"=="nolab"{
            while "`1'"!="" {
                lab var `1'
                mac shift
            }
        }
        qui separate `hi', by($FBY) gen(_hi) seq
        local hilist `r(valist)'
        qui separate `low', by($FBY) gen (_low) seq
        local lilist `r(varlist)'
        if "`connect'"=="" {local connect .II}
        local s1 "Oii"
        local s2 "Sii"
        local s3 "Tii"
        local s4 "oii"
        local s5 "dii"
        local s6 "pii"
        local s7 ".ii"
        local i=1
        qui tab $FBY
        local nrow=`r(r)'
        while `i'<=`nrow'{
            local hilist `hilist' _Grp`i' _hi`i' _low`i'
            local con1 `con1'`connect'
            local symb1 `symb1'`s`i''
            local i = `i'+1
            local pen1 `pen1'`i'`i'`i'
        }
        sort $FBY `xvar'
        if "`by'" ~="" {
            sort `by' `xvar'
            local by1 by(`by')
            }
    $S_CMD `hilist' `xvar', c(`con1') s(`symb1') `by1' /*
        */ pen(`pen1') `options'
    }
end

program define _kdg6
  version 6.0
  syntax [varlist (min=1 max=1 numeric)] [if] [in] /*
        */ [,N(integer 50) BIweight COSine EPan GAUss /*
        */ RECtangle PARzen /*
        */ TRIangle STUd(int 0) Connect(string) * ]
  tokenize `varlist'
  marksample touse
  preserve

 if "$FBY" =="" {
      di in r /*
      */ "Please use -kdensity- for one group kernel density plot"
      exit
  }
  # delimit ;
  local kernel "`biweight' `cosine' `epan' `causs'
  `rectangle' `parzen' `triangle'" ;
  # delimit cr
  tempvar grp x fx
  egen `grp' = group($FBY)
  local ngrps = `grp'[_N]
  local i = 1
  while `i' <= `ngrps' {
    tempvar fx`i'
    local i = `i' + 1
  }
  if "`stud'"~=""{local stud1 stud(`stud')}
  kdensity `1', nogr gen(`x' `fx') n(`n')  `kernel' `stud1'
  if "`connect'"==""{local con1 l}
  else {local con1 `connect'}
  local i = 1
  local j = 1
  qui sort `grp'
  qui drop if `grp'==.
  local nmin=0
  while `i'<=`ngrps'{
     kdensity `1' if `grp'==`i', /*
     */`kernel' n(`n') nogr gen(`fx`i'') at(`x') `stud1'
     local num = $FBY[`j']
     if "$FBY" != "" {local n`i' : label $FBY `num' }
     lab var `fx`i'' "`n`i''"
     qui count if `grp' == `i'
     local connect `connect'`con1'
     local j = `j' + _result(1)
     local i = `i'+1
  }
  gr `fx1'-`fx`ngrps'' `x', c(`connect') `options'
end

program define _eby6
    syntax [varlist] [if] [in] [, by(varname) *]
    marksample touse
    preserve
    qui keep if `touse'
    if "`varlist'"==""{local varlist _all}
    unab vnames: `varlist'
    token `vnames'
    if "`by'"~=""{local by1 "by(`by')"}
    while "`1'"~=""{
        di
        di in g "For variable `1':"
        cap drop _Grp*
        qui separate `1', by($FBY) gen(_Grp)
        cap sort `by'
        $S_CMD `r(varlist)', `options' `by1'
        capture qui drop `r(varlist)'
        mac shift
    }
end