*! 2.0.0 27Aug97 Jeroen Weesie/ICS STB-41 ssa11 program define sttvc version 5.0 st_is if "`*'" == "" { exit } * split on first comma outside () splitc `*' local Lead "$S_1" /* input to first comma outside () */ local Opt "$S_2" /* rest of input line */ * split Lead in xpart and Lrest on "=" parse "`Lead'", p("=") if "`1'" == "=" | "`2'" != "=" | "`3'" == "=" | "`4'" ~= "" { exit 198 } local xpart "`1'" mac shift 2 local Lrest "`*'" * split Lrest into list-of-expressions and, optionally, if/in parse "`Lrest'", p(" ") while "`1'" ~= "" & "`1'" ~= "in" & "`1'" ~= "if" { local expr "`expr' `1'" mac shift } local ifin "`*'" * verify that expr has odd number of expressions-seperated-by-blanks local nexpr : word count `expr' if mod(`nexpr',2) ~= 1 { di in re "number of expressions should be odd" exit 498 } * high level parsing of "x", ifin, and options local varlist "new max(1)" local if "opt" local in "opt" local options "noSHow Preserve Zero(str)" parse "`xpart' `ifin' `Opt'" * scratch tempvar mv mv0 touse T T0 tsp v x rename `varlist' `x' * selected sample mark `touse' `if' `in' * verify option zero() if "`zero'" ~= "" { confirm exist `zero' capt assert `zero' ~= . if `touse' if _rc { di in re "zero-time should be non-missing" exit 498 } local Zero "- `zero'" } * allow restoration if "`preserv'" ~= "" { preserve local Done "restore, not" } local N0 = _N * interface to st 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] * loop over expressions, verifying that the t(j)'s are increasing and positive quietly { parse "`expr'", p(" ") replace `x' = `1' if `touse' gen `T0' = 0 gen byte `mv0' = 0 local i 2 while `i'+1 <= `nexpr' { gen `T' = ``i'' `Zero' if `touse' gen byte `mv' = (`T' == .) if `touse' capt assert `T' >= 0 if `touse' if ~_rc { di in bl "time ``i''`Zero' takes negative values" } local i = `i'+1 gen `v' = ``i'' if `touse' local i = `i'+1 * check that the T(j)'s are increasing or "constant missing" if `i' > 4 { capt assert `mv'<=`mv0' if `touse' if _rc { di in re "missing at one time point, should imply missing at all later points" exit 498 } capt assert `T'>`T0' if `touse' & ~`mv' if _rc { di in re "time points should be increasing" exit 498 } } * T before (t0,t] replace `x' = `v' if `T'<=`t0' & `touse' * T during (t0,t] : episode splitting gen byte `tsp' = (`T'>`t0') & (`T'<`t') & (`x'~=`v') & `touse' /* ~mv implied */ local N = _N local N1 = _N+1 expand = `tsp'+1 if _N > `N' { * adopt t0 and t and died for splitted obs replace `t' = `T' if `tsp' in 1/`N' replace `d' = 0 if `tsp' in 1/`N' replace `t0' = `T' in `N1'/l /* if tsp is implied */ * set tvc for splitted obs replace `x' = `v' in `N1'/l /* if tsp is implied */ } replace `T0' = `T' replace `mv0' = `mv' drop `v' `T' `mv' `tsp' } rename `x' `varlist' `Done' noi di _n in gr "number of episode splits : " in ye =_N-`N0' } /* quietly */ end * splits a string on "free comma's" into the parts NonOptions and Options * the comma actually is a toggle between the two processing statuses * comma's inside grouping characters (parentheses, brackets) are ignored. * as a side product, we test whether the grouping characters are properly * nested and closed program define splitc version 5.0 * replaces in input spaces by -space- if "$S_PCHAR" == "" { local space "@" } else local space "$S_PCHAR" while "`1'" ~= "" { local input "`input'`1'`space'" mac shift } * toggle Opt at a ",", that does not occur within parentheses or brackets local H 0 /* level of nesting */ local Mode0 "None" /* None, p(arentheis), b(racket) */ local ProcOpt 0 /* set to 1 while processing Options */ local Opt /* set to options */ local NonOpt /* set to non-options */ parse "`input'", p("`space'()[],") while "`1'" ~= "" { if "`1'" == "," & `H' == 0 { * toggle processing status local ProcOpt = 1 - `ProcOpt' local 1 } else if "`1'" == "(" { local H = `H'+1 /* push "parenthesis" */ local Mode`H' "p" } else if "`1'" == "[" { local H = `H'+1 /* push "bracket" */ local Mode`H' "b" } else if "`1'" == ")" { if "`Mode`H''" ~= "p" { ErrNest } local H = `H'-1 /* pop the previous mode */ } else if "`1'" == "]" { if "`Mode`H''" ~= "b" { ErrNest } local H = `H'-1 /* pop the previous mode */ } else if "`1'" == "`space'" { local 1 " " /* restore space */ } if `ProcOpt' == 0 { local NonOpt "`NonOpt'`1'" } else local Opt "`Opt'`1'" mac shift } if `H' ~= 0 { di in re "too few ')' or ']'" exit 132 } global S_1 "`NonOpt'" global S_2 "`Opt'" *di in gr "S_1: $S_1 " *di in gr "S_2: $S_2" end program define ErrNest di in re "too many or mismatching ')' or ']'" exit 132 end