*! version 1.0 by Jesper B. Sorensen Nov. 20 1999.

* program to estimate piecewise-constant exponential models using
* various Stat routines.
* syntax: stpiece [varlist] [if] [in] , [tp(numlist)] [tv(varlist)] [presplit] [nopreserve]

program define stpiece
    version 6.0
    syntax [varlist(default=none numeric)] [if] [in] , [tp(numlist >=0) tv(varlist) PREsplit(integer -1) noPREserve *]
    if "`preserv'"=="" {
        preserve
    }
    if "`presplit'" == "-1" {
        if "`tp'"=="" {
            di in red "You must either declare data to be presplit or use tp()"
            exit 198
        }
        di in gr "Invoking stsplit..."
        qui stsplit tp, at(`tp')
        di in gr "Creating time pieces..."
        qui tab tp, gen(tp)
        local i = _result(2)
        drop tp
    }
    else {
        local i = "`presplit'"
    }
    if "`tv'" ~= "" {
        di in gr "Creating interaction effects..."
        tokenize `tv'
        while "`1'" ~= "" {
            local stub = substr("`1'",1,4)
            local j=0
            while "`j'" < "`i'" {
                local j = `j'+1
                local x = "tp`stub'`j'"
                qui gen  `x'=`1'*tp`j'
            }
         mac shift
        }
    }

    streg tp* `varlist' `if' `in' , nocons d(e) `options'

end