📄 ztspli_5.ado
字号:
*! 2.0.1 16sep04 (Jeroen Weesie/ICS) STB-41 ssa11
program define ztspli_5
version 5.0, missing
zt_is_5
capt parsoptp
if _rc ~= 199 {
* use parsopt to match on parentheses
local cmd "`*'"
* parse at()
parsoptp at `cmd'
local at "$S_2" /* at(str) */
local cmd "$S_3" /* cmd, with at() removed */
* parse every()
parsoptp every `cmd'
local every "$S_2" /* every(str) */
local cmd "$S_3" /* cmd, with every() removed */
* parse the rest
local varlist "new req max(1)"
local if "opt"
local in "opt"
local options "noSHow expr"
parse "`cmd'"
}
else {
di in bl "Install -parsoptp- if you need real expressions in at/every"
* standard Stata high-level parsing
local varlist "new req max(1)"
local if "opt"
local in "opt"
local options "at(str) every(str) noSHow expr"
parse "`*'"
}
* verify input
if "`at'`every'" == "" {
di in re "option { at() | every() } required"
exit 100
}
quietly {
* scratch
tempvar touse tsp v xid it0 it nt
rename `varlist' `v'
* sample
mark `touse' `if' `in'
local N0 = _N
* interface to -st- characteristics
_st_aux, `show'
local id : char _dta[st_id]
local t : char _dta[st_t]
local t0 : char _dta[st_t0]
local d : char _dta[st_d]
* regular time splittng t0,t
quietly compress
if "`every'" ~= "" {
tempname e
capt gen `e' = `every' if `touse'
if _rc {
di in re "syntax error in `e'"
exit 198
}
capt assert `e' > 0 if `touse'
if _rc {
di in re "every() should be a strictly positive"
exit 498
}
* in which intervals are t0 and t
* 1 [0,e) 2 [e,2e) 3 [2e,3e) etc
gen int `it0' = 1 + int(`t0' / `e') if `touse'
gen int `it' = 1 + int((`t' / `e') -1E-10) if `touse'
* number of intervals [t0,t)
gen int `nt' = `it' - `it0' + 1 if `touse'
gen int `xid' = _n if `touse'
* expand
expn `nt'
* adapt key-variables
sort `xid'
by `xid' : replace `v' = `it0' + _n - 1 if `touse'
by `xid' : replace `t0' = `e' * (`v'-1) if _n > 1 & `touse'
by `xid' : replace `t' = `e' * `v' if _n < _N & `touse'
by `xid' : replace `d' = 0 if _n < _N & `touse'
}
* at() splitting
else {
if "`expr'" == "" {
* -at- should be a _numlist
_numlist `at', real sort
local at "$S_1"
}
else {
* -at- is interpreted as a list of expressions
tempvar att
gen `att' = .
}
local nat : word count `at'
replace `v' = 1 if `touse'
local i 1
while `i' <= `nat' {
if "`expr'" == "" {
local att : word `i' of `at'
}
else {
di in bl "add: test that expressions are increasing"
local e : word `i' of `at'
capt replace `att' = `e' if `touse'
if _rc {
di in re "syntax error in `e'"
exit 198
}
}
* which obs to split ?
gen byte `tsp' = (`att' > `t0') & (`att' < `t') & `touse'
* split records
local N = _N
local N1 = _N+1
expand =`tsp'+1
if _N > `N' {
replace `t' = `att' in 1/`N' if `tsp'
replace `d' = 0 in 1/`N' if `tsp'
replace `t0' = `att' in `N1'/l
replace `v' = `i'+1 in `N1'/l
}
* adopt TVC for unsplited records
replace `v' = `i'+1 if `att' <= `t0' & `touse' & ~`tsp'
drop `tsp'
local i = `i'+1
}
}
if `N0' ~= _N {
noi di _n in gr "number of episodes generated : " in ye =_N-`N0'
}
else noi di _n in bl "no new episode generated!"
rename `v' `varlist'
/* end quietly */ }
end
program define expn
capt expand = `*'
if _rc {
di in re "impossible to expand to splity episodes--probably too little memory"
di in re "hint: drop variables if possible"
exit 950
}
end
*! 1.1.0 Jan 03, 1997 Jeroen Weesie/ICS STB-35 ip14
* -- added -sort-
* -- added -format-
* 1.0.0 Apr 96, Jeroen Weesie/ICS
program define _numlist
* ------------------------------------------------------------------------
* parse off and check options
* ------------------------------------------------------------------------
parse "`*'", p(",")
local terms "`1'"
if "`2'" != "" & "`2'" != "," {
exit 198
}
local options "Display MIn(str) MAx(str) Real Sort Format(str)"
parse ",`3'"
* type of numbers
if "`real'" == "" {
local nmbtype "integer"
}
else local nmbtype
* restrictions on number of terms
if "`min'" != "" { confirm integer number `min' }
if "`max'" != "" { confirm integer number `max' }
if "`min'" != "" & "`max'" != "" {
if `min' > `max' {
di in re "numlist: invalid (min, max)"
exit 198
}
}
if "`format'" != "" {
capture local f : display `format' 1
if _rc {
di in re "invalid format `format'"
exit 120
}
}
* ------------------------------------------------------------------------
* parse the terms
* ------------------------------------------------------------------------
global nlist
parse "`terms'", p(" ")
while "`1'" != "" {
local term `1'
local inc
local rng2
* term = number
* = number-number
* = number-number/number
* split [term] in [term / inc]
local pinc = index("`term'","/")
if `pinc' > 0 {
local inc = substr("`term'",`pinc'+1,.)
confirm `nmbtype' number `inc'
if `inc' == 0 {
di in re "numlist: Increment is zero!"
exit 198
}
local term = substr("`term'", 1, `pinc'-1)
}
* check if range is specified
* beware of trailing -
local prng = index(substr("`term'",2,.),"-")+1
if `prng' > 1 {
local rng1 = substr("`term'",1,`prng'-1)
local rng2 = substr("`term'",`prng'+1,.)
confirm `nmbtype' number `rng1'
confirm `nmbtype' number `rng2'
if "`inc'" == "" { local inc 1 }
local nincr = 1 + /*
*/ int((float(`rng2')-float(`rng1')+1E-6) / float(`inc'))
if `nincr' <= 0 {
di in re "range n-m/incr should imply > 0 terms"
exit 198
}
* loop rng1-rng2/inc
while `nincr' > 0 {
if "`format'" != "" {
local f : display `format' `rng1'
local nlist "`nlist'`f' "
}
else {
local nlist "`nlist'`rng1' "
}
local rng1 = `rng1' + `inc'
local nincr = `nincr' - 1
}
}
else {
* no increment expected at this point
if "`inc'" != "" {
di in re "numlist : invalid /"
exit 198
}
confirm `nmbtype' number `term'
local nlist "`nlist'`term' "
}
mac shift
}
* ------------------------------------------------------------------------
* sort list in ascending order
* ------------------------------------------------------------------------
if "`sort'" != "" {
capture sortlist "`nlist'", a
if _rc {
di in re "error in sorting. Was -sortlist- installed?"
exit 198
}
local nlist "$S_1"
}
* ------------------------------------------------------------------------
* check min <= #terms <= max
* ------------------------------------------------------------------------
local nnl : word count `nlist'
if "`min'" != "" {
if `nnl' < `min' {
di in re "numlist: at least `min' values required, `nnl' specified"
exit 198
}
}
if "`max'" != "" {
if `nnl' > `max' {
di in re "numlist: at most `max' values allowed, `nnl' specified"
exit 198
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -