*! Make a categorical variable by cutting at break points
*! Addition to egen
*! David Clayton and Michael Hills, version 1.1, July 1999
program def _gcut
    version 5.0
    local type "`1'"
    mac shift
    local g "`1'"
    mac shift
    mac shift

    local varlist "req ex min(1) max(1)"
    local if "opt"
    local in "opt"
    local options "BReaks(string) Group(real 0) ICodes Label"
    parse "`*'"
    parse "`varlist'", parse(" ")

    tempvar pctile x

    qui gen `x' = `1' `if' `in'

    if "`breaks'"=="" & `group'==0{
        di in red "needs breaks or group option"
        exit
    }
    if "`breaks'"=="" & `group'!=0 & "`icodes'"==""{
        local icodes = "icodes"
    }
    if "`label'"!="" & "`icodes'"=="" {
        local icodes = "icodes"
    }
    cap label drop `g'


    if "`breaks'"=="" {
        local count = 1
        qui summ `x'
        local extra = string(_result(5))
        local breaks  "`extra'"
        qui pctile `pctile' = `x', nq(`group')
        while `count' < `group' {
        local extra = string(`pctile'[`count'])
        local breaks  "`breaks',`extra'"
        local count = `count' + 1
        }
        qui summ `x'
        local extra = string(_result(6)+1)
        local breaks  "`breaks',`extra'"
     }

    parse "`breaks'", parse(",[]")
    local cutp "`1'"
    while "`1'"!="" {
        if "`2'"=="," {
            if `3'<=`cutp' {
                di in red "Break points must be in ascending order"
                exit
            }
            else {
                mac shift
                mac shift
                local cutp "`1'"
            }
        }
        else if "`2'"=="[" {
            if "`4'"!="]" {
                di in red "Syntax error in breaks list"
                exit
            }
            else {
                if `5'<=`1' {
                    di in red "Break points must be in ascending order"
                    exit
                }
                mac shift
                mac shift
                local cutw "`1'"
                mac shift
                mac shift
                while `cutp'<`1' { local cutp = `cutp'+`cutw'}
                if `cutp'>`1' { local cutp "`1'" }
            }
        }
        else if "`2'"=="" {
            mac shift
        }
        else {
            di in red "Syntax error in breaks list"
            exit
        }
    }
    local max "`cutp'"

    qui gen `g'=.
    parse "`breaks'", parse(",[]")
    local i=0
    while "`1'"!="" {
        if ("`2'"=="," | "`2'"=="[") {
            local cutp "`1'"
            mac shift
            mac shift
        }
        else if "`2'"=="]" {
            local cutp = `cutp' + `1'
            if `cutp'>=`3' {
                local cutp "`3'"
                mac shift
                mac shift
                mac shift
                mac shift
            }
        }
        else if "`2'"=="" {
            local cutp "`1'"
            mac shift
        }
        if "`1'"=="" {
            qui replace `g' = . if `x'>float(`cutp')
        }
        else {
            if "`icodes'"=="" {
                qui replace `g' = `cutp' if `x'>=float(`cutp')
            }
            else {
                qui replace `g' = `i' if `x'>=float(`cutp')
            }
        }

        if `i'>0 & "`label'"!="" & "`icodes'" != "" {
            local code = `i' - 1
*            la def `g' `code' "`lastcut'-`cutp'", a
            la def `g' `code' "`lastcut'-", a
        }
        if `i'>0 & "`label'"!="" & "`icodes'" == "" {

*            la def `g' `lastcut' "`lastcut'-`cutp'", a
            la def `g' `lastcut' "`lastcut'-", a
        }
        local lastcut "`cutp'"
        local i = `i'+1
    }
    if "`label'"!="" {
       la val `g' `g'
    }

end