*! 2.0.0 25Aug97 jw/ics STB-41 ssa11 * -- added -preserv- option * -- faster algorithm to split records, inspired by coding of st_rpool * 1.0.0 10Feb97 (Jeroen Weesie/ICS) program define strepl version 5.0 quietly { st_is capt parsoptp if _rc ~= 199 { * use parsoptp to parse options matched on parentheses local cmd "`*'" * parse at() parsoptp at `cmd' local at "$S_2" /* at(str) */ local cmd "$S_3" /* cmd, with at() removed */ * parse from() parsoptp from `cmd' local from "$S_2" /* from(str) */ local cmd "$S_3" /* cmd, with from() removed */ * parse to() parsoptp to `cmd' local to "$S_2" /* to(str) */ local cmd "$S_3" /* cmd, with to() removed */ * parse the rest local varlist "ex req max(1)" local if "opt" local in "opt" local options "noSHow Censor Preserv" parse "`cmd'" } else { di in bl "Install -parsoptp- if you need real expressions in at/from/to" * use Stata high-level parsing that does not match parentheses local varlist "ex req max(1)" local if "opt" local in "opt" local options "at(str) from(str) to(str) noSHow Censor Preserv" parse "`*'" } * scratch tempvar att rslt touse tsp value qui gen `rslt' = `varlist' * sample to be modified mark `touse' `if' `in' * check arguments * at() if "`at'" == "" { di in re "option -at- required" exit 100 } capt gen `att' = `at' if `touse' if _rc { di in re "error in evaluating `at'" exit 198 } * att should not be missing if "`censor'" == "" { capt assert `att' ~= . if `touse' if _rc { di in re "at() should be non-missing" exit 498 } } else { * missing -att- should not be affected count if `touse' & `att' == . if _rc { noi di in bl _result(1) " cases with missing at() will not be modified" } replace `touse' = 0 if `touse' & `att' == . } capt assert `att' >= 0 if `touse' if _rc { di in bl "at() takes negative values" } * check to() and from() if ("`to'" == "" & "`from'" == "") | ("`to'" ~= "" & "`from'" ~= "") { di in re "from() or to() should be specified" exit 100 } * value is set -from- or -to-, whichever was specified capt gen `value' = `from' `to' if `touse' if _rc { di in re "error in evaluating `from'`to'" exit 198 } * allow restoration if "`preserv'" ~= "" { preserve local Done "restore, not" } * which obs to split ? local t : char _dta[st_t] local t0 : char _dta[st_t0] if "`t0'" == "" { local t0 0 } gen byte `tsp' = (`att'>`t0') & (`att'<`t') & (`rslt'~=`value') & `touse' count if `tsp' == 1 if _result(1) > 0 { noi di _n in gr "number of episode splits : " in ye _result(1) } else { noi di _n in bl "no episode splitting required!" st_show `show' /* no comma ! */ if "`to'" ~= "" { replace `rslt' = `value' if `att' <= `t0' & `touse' } if "`from'" ~= "" { replace `rslt' = `value' if `att' >= `t' & `touse' } replace `varlist' = `rslt' `Done' exit } * interface to -st- characteristics noi 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] * perform episode splitting ! * -expand- locates records, in same order, after original records * inspired by st_rpool by W. Gould local N = _N local N1 = _N+1 expand = `tsp'+1 * adopt t0 and t and died for splitted obs replace `t' = `att' if `tsp' in 1/`N' replace `d' = 0 if `tsp' in 1/`N' replace `t0' = `att' in `N1'/l * set tvc for splitted obs if "`to'" ~= "" { replace `rslt' = `value' in `N1'/l } if "`from'" ~= "" { replace `rslt' = `value' if `tsp' in 1/`N' } * set tvc for unsplitted obs if "`to'" ~= "" { replace `rslt' = `value' if `att' <= `t0' & `touse' & ~`tsp' } if "`from'" ~= "" { replace `rslt' = `value' if `att' >= `t' & `touse' & ~`tsp' } replace `varlist' = `rslt' `Done' } end