program def tpvar
*! NJC 1.0.0 9 June 1998
    version 5.0
    local varlist "max(1)"
    local if "opt"
    local in "opt"
    local options "Generate(str) Above(real 0.016) Below(real 0.034)"
    parse "`*'"
    local v `varlist'

    if "`generat'" == "" { local generat = "_" + substr("`v'",1,7) }
    confirm new variable `generat'

    qui su `varlist' `if' `in', meanonly
    local range = _result(6) - _result(5)

    tempvar tp
    qui gen `tp' = `above' * (`v' > `v'[_n - 1] & `v' > `v'[_n + 1]) /*
     */ - `below' * (`v' < `v'[_n - 1] & `v' < `v'[_n + 1]) `if' `in'

    qui gen `generat' = `varlist' + `tp' * `range' if `tp'

end