📄 st_set.ado
字号:
if "`goodwgt'"!="" {
count if `goodwgt'==0 & `touse'
local L_wgt = r(N)
replace `touse'=0 if `goodwgt'==0
}
else local L_wgt 0
/*
Step 11. Clean up
*/
replace `t'=. if !`touse'
replace `t0'=. if !`touse'
replace `d' = . if !`touse'
compress `t0'
/*
if "`id'"=="" {
count if `t0'!=0 & `touse'
if r(N)==0 {
drop `t0'
}
}
*/
} /* quietly */
Clear
confirm new var _st _d _t _t0 _origin _insmpl
nobreak {
rename `touse' _st
rename `d' _d
rename `t' _t
rename `t0' _t0
/* save "full" option copies */
char _dta[st_fente] `"`fenter'"'
char _dta[st_fexit] `"`fexit'"'
char _dta[st_forig] `"`forig'"'
char _dta[st_fif] `"`fif'"'
char _dta[st_fever] `"`fever'"'
char _dta[st_fbefo] `"`fbefor'"'
char _dta[st_fafte] `"`fafter'"'
char _dta[st_full] `"`full'"'
/* end save "full" option copies */
/* save ver. 1 compatibility */
char _dta[st_t] "_t"
char _dta[st_t0] "_t0"
char _dta[st_d] "_d"
/* endsave ver. 1 compatibility */
/* save selection criteria */
char _dta[st_ifexp] `"`ifexp'"'
char _dta[st_if] `"`if'"'
if `"`full'"' != "" {
rename `ever' _insmpl
char _dta[st_ever] _insmpl
}
else char _dta[st_ever] `"`ever'"'
char _dta[st_never] `"`never'"'
char _dta[st_after] `"`after'"'
char _dta[st_befor] `"`before'"'
/* end save selection criteria */
/* save weights */
char _dta[st_wt] "`wt'"
char _dta[st_wv] "`wv'"
if "`wt'"!="" {
char _dta[st_w] `"[`wt'=`wv']"'
}
/* end save weights */
/* save exit() and enter() */
Combine st_exit `"`ex_ivar'"' `"`ex_list'"' `"`ex_exp'"'
Combine st_enter `"`en_ivar'"' `"`en_list'"' `"`en_exp'"'
/* end save exit() and enter() */
/* save scale() */
char _dta[st_bs] "`bs'"
char _dta[st_s] `_dta[st_bs]' /* for time being */
/* end save scale() */
/* save origin() */
if "`or_cl'"!="" {
char _dta[st_orig] "min"
}
else Combine st_orig `"`or_ivar'"' `"`or_list'"' `"`or_exp'"'
if "`zero'"!="0" {
qui summarize `zero' if _st
local zmin = r(min)
if !(r(min)==r(max) & r(min)<. & r(min)==`zmin') {
rename `zero' _origin
char _dta[st_o] "_origin"
}
else char _dta[st_o] `zmin'
local zmin
}
else char _dta[st_o] "0"
/* end save origin() */
/* save key variables */
char _dta[st_ev] `"`event'"'
char _dta[st_bd] `"`bd'"'
char _dta[st_bt] "`bt'"
char _dta[st_bt0] `"`bt0'"'
char _dta[st_id] "`id'"
/* end save key variables */
/* save show/noshow */
if "`show'"=="noshow" {
char _dta[st_show] "noshow"
}
/* end save show/noshow */
/* save marker */
char _dta[st_ver] 2
char _dta[_dta] "st"
/* end save marker */
}
st, `stopts'
di in smcl _n in gr "{hline 78}"
Di1 _N "total obs."
qui count if _st==0
local Left = _N - r(N)
if r(N) {
Di2 `L_ifexp' "ignored at outset due to -if <exp>-"
Di2 `L_id' `"ignored because `_dta[st_id]' missing"'
Di2h `L_bt' "event time missing" "(`_dta[st_bt]'>=.)"
Di2h `L_ut0m' "entry time missing" "(`_dta[st_bt0]'>=.)"
Di2h `L_ut0t' /*
*/ "entry on or after exit" `"(`_dta[st_bt0]'>`_dta[st_bt]')"'
Di2h `L_order' `"multiple records at same instant"' "" /*
*/ `"(`_dta[st_bt]'[_n-1]==`_dta[st_bt]')"'
Di2h `L_ord2' "overlapping records" /*
*/ `"(`_dta[st_bt]'[_n-1]>`_dta[st_bt0]')"'
Di2 `L_res' "ignored per request (if(), etc.)"
Di2 `L_never' "ignored because never entered"
Di2 `L_prior' "obs. end on or before enter()"
Di2 `L_t0t' "obs. end on or before origin()"
if `L_after' {
if `"`exit'"'=="" | `"`exit'"'=="failure" {
local m "(first) failure"
}
else local m "exit"
Di1 `L_after' "obs. begin on or after `m'"
}
Di2h `L_wgt' "weights invalid"
}
else Di1 0 "exclusions"
di in smcl in gr "{hline 78}"
if "`_dta[st_wt]'" == "fweight" {
Di1 `Left' "physical obs. remaining, equal to"
local wv `_dta[st_wv]'
qui sum `wv' if _st, meanonly
Di1 `r(sum)' "weighted obs., representing"
}
else {
Di1 `Left' "obs. remaining, representing"
tempvar wv
qui gen byte `wv'=1 if _st
}
tempvar wrk
if "`_dta[st_id]'" != "" {
sort _st `_dta[st_id]' _t
qui by _st `_dta[st_id]': gen byte `wrk' = 1 if _n==1 & _st
qui sum `wv' if `wrk'==1, meanonly
local msg = "subject" + cond(r(sum)==1,"","s")
Di1 `r(sum)' "`msg'"
drop `wrk'
}
if "`_dta[st_id]'" != "" {
capture by _st `_dta[st_id]': assert sum(_d)<=1 if _st
if _rc == 0 {
local m "in single failure-per-subject data"
local chk yes
}
else local m "in multiple failure-per-subject data"
}
else local m "in single record/single failure data"
qui sum `wv' if _d & _st, meanonly
local msg = "failure" + cond(r(sum)==1,"","s")
Di1 `r(sum)' "`msg' `m'"
if "`chk'"=="yes" {
qui by _st `_dta[st_id]': gen float `wrk' = /*
*/ sum(_d) & _n<_N if _st
qui by _st `_dta[st_id]': replace `wrk' = /*
*/ sum(`wrk')
capture assert `wrk'==0 if _st
if _rc {
qui by _st `_dta[st_id]': replace `wrk'=0 if _n!=_N
qui sum `wv' if `wrk', meanonly
local msg = cond(r(sum)~=1,"subjects remain",/*
*/ "subject remains")
Di1 `r(sum)' "`msg' at risk after failure"
}
drop `wrk'
}
qui gen double `wrk' = `wv'*(_t - _t0) if _st
qui sum `wrk', meanonly
di in ye %9.0g r(sum) in gr /*
*/ " total analysis time at risk, at risk from t = " in ye %9.0g 0
drop `wrk'
qui sum _t0 if _st, meanonly
di in gr _col(30) "earliest observed entry t = " in ye %9.0g r(min)
qui sum _t if _st, meanonly
di in gr _col(35) "last observed exit t = " in ye %9.0g r(max)
end
program define Combine
args chname ivar ilist texp
local abb = substr(`"`chname'"',1,5)
if `"`ivar'"'=="" & `"`texp'"'=="" { exit }
if `"`ivar'"' != "" {
char _dta[`chname'] `"`ivar'==`ilist'"'
char _dta[`abb'vn] `"`ivar'"'
char _dta[`abb'nl] `"`ilist'"'
if `"`texp'"'!="" {
char _dta[`abb'exp] `"`texp'"'
char _dta[`chname'] `"`_dta[`chname']' time `texp'"'
}
exit
}
char _dta[`abb'exp] `"`texp'"'
char _dta[`chname'] `"time `texp'"'
end
program define Choose
args newvar minmax t1 t2 touse
if "`t2'"=="" {
rename `t1' `newvar'
}
else if "`t1'"=="" {
rename `t2' `newvar'
}
else {
replace `t1' = `minmax'(`t1',`t2') if `touse'
drop `t2'
rename `t1' `newvar'
}
end
program define Failure, sclass
sret clear
if `"`0'"' == "" { exit }
gettoken name 0 : 0, parse(" =")
gettoken eqsign 0 : 0, parse(" =")
if !(`"`eqsign'"'=="" | `"`eqsign'"'=="=" | `"`eqsign'"'=="==") {
di in red "option failure(): syntax error"
exit 198
}
unab name: `name', max(1)
sret local bd `name'
if `"`eqsign'"'=="" { exit }
numlist `"`0'"', missingok
sret local event `"`r(numlist)'"'
end
program define Di1
di in ye %9.0g `1' in gr `" `2'"'
mac shift 2
while `"`1'"' != "" {
di in gr _skip(11) `"`1'"'
mac shift
}
end
program define Di2
if (`1')==0 { exit }
di in ye %9.0g `1' in gr `" `2'"'
mac shift 2
while `"`1'"' != "" {
di in gr _skip(11) `"`1'"'
mac shift
}
end
program define Di2h
if (`1')==0 { exit }
di in ye %9.0g `1' in gr `" `2'"' in gr " `3'" /*
*/ _col(65) "PROBABLE ERROR"
mac shift 3
while `"`1'"' != "" {
di in gr _skip(11) `"`1'"'
mac shift
}
end
/*
program define Di2
if (`1')==0 { exit }
di in ye _skip(11) %9.0g (`1') in gr `" `2'"'
mac shift 2
while `"`1'"' != "" {
di in gr _skip(11) _skip(10) `"`1'"'
mac shift
}
end
*/
program define Di2z
di in ye _skip(11) %9.0g (`1') in gr `" `2'"'
mac shift 2
while `"`1'"' != "" {
di in gr _skip(11) _skip(10) `"`1'"'
mac shift
}
end
/*
origin(varname=3 4 5 time exp)
origin(time exp)
orign(varname)
*/
program define Iop, sclass
args idvar opname cnts
/* returns texp, ivar, ilist */
sret clear
if `"`cnts'"' == "" { exit }
gettoken word cnts : cnts, parse(" =")
gettoken eqsign : cnts, parse(" =")
if `"`eqsign'"'=="" {
unab word : `word', max(1) name(`opname'())
sret local texp `word'
exit
}
if `"`eqsign'"'=="=" | `"`eqsign'"'=="==" {
gettoken eqsign cnts : cnts, parse(" =")
unab ivar : `word', max(1)
sret local ivar `ivar'
gettoken word cnts: cnts
while `"`word'"' != "" & /*
*/ substr("time",1,length(`"`word'"'))!=`"`word'"' {
local list `list' `word'
gettoken word cnts: cnts
}
numlist `"`list'"', missingok
sret local ilist `r(numlist)'
if `"`word'"' == "" { exit }
}
if substr("time",1,length("`word'")) == "`word'" {
local ivar "`s(ivar)'" /* hold values because ... */
local ilist "`s(ilist)'" /* ... unabbrev clears */
capture unabbrev `cnts', max(1)
if _rc==0 {
local cnts `cnts'
}
sret local ivar `ivar'
sret local ilist `ilist'
sret local texp `cnts'
exit
}
di in red "option `opname'() invalid"
exit 198
end
program define HasId
args opname id
if "`id'"=="" {
di in red "option `opname'() requires option id()"
exit 198
}
end
program define Smallest
args newvar exp touse id bt
if "`id'"!="" {
sort `touse' `id' `bt'
by `touse' `id': gen double `newvar'=`exp' if `touse'
sort `touse' `id' `newvar'
by `touse' `id': replace `newvar' = `newvar'[1]
}
else gen double `newvar' = `exp' if `touse'
end
program define FirstEv
args newvar list touse id bt bd
sort `touse' `id' `bt'
gen double `newvar' = .
tokenize `list'
while "`1'"!="" {
replace `newvar' = `bt' if `bd'==`1' & `touse'
mac shift
}
by `touse' `id': replace `newvar' = `newvar'[_n-1] /*
*/ if `touse' & `newvar'[_n-1]<.
by `touse' `id': replace `newvar' = `newvar'[_N] if `touse'
end
program define Clear
nobreak {
char _dta[_dta]
char _dta[st_set]
char _dta[st_ver]
char _dta[st_id]
char _dta[st_bt0]
char _dta[st_bt]
char _dta[st_bd]
char _dta[st_ev]
char _dta[st_enter]
char _dta[st_exit]
char _dta[st_orig]
char _dta[st_bs]
char _dta[st_ifexp]
char _dta[st_if]
char _dta[st_ever]
char _dta[st_never]
char _dta[st_after]
char _dta[st_befor]
char _dta[st_wt]
char _dta[st_wv]
char _dta[st_w]
char _dta[st_show]
capture drop `_dta[st_o]'
char _dta[st_o]
char _dta[st_s]
Drop _dta[st_t] _t
Drop _dta[st_t0] _t0
Drop _dta[st_d] _d
capture drop _st
capture confirm integer number `_dta[st_n0]'
if _rc == 0 {
local i 1
while `i' <= `_dta[st_n0]' {
char _dta[st_n`i']
local i = `i' + 1
}
}
char _dta[st_n0]
char _dta[st_fev]
char _dta[st_fente]
char _dta[st_fexit]
char _dta[st_forig]
char _dta[st_fif]
char _dta[st_fever]
char _dta[st_fafte]
char _dta[st_fbefo]
char _dta[st_full]
capture drop _insmpl
capture drop _origin
}
end
program define Drop
args char default
if "``char''" == "`default'" {
capture drop `default'
}
char `char'
end
exit
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -