📄 codebook.ado
字号:
local ttl "embedded"
global T_cb_str_embedded $T_cb_str_embedded `v'
}
else {
local hasem 0
}
if `haslead' + `hastrail' + `hasem' >= 2 {
if `haslead' & `hastrail' & `hasem' {
local ttl "leading, embedded, and trailing"
}
else if `haslead' & `hastrail' {
local ttl "leading and trailing"
}
else if `haslead' & `hasem' {
local ttl "leading and embedded"
}
else {
local ttl "embedded and trailing"
}
}
if `haslead' | `hastrail' | `hasem' {
dis _n as txt _col(16) "warning: variable has `ttl' blanks"
}
end
program Labeled, rclass
args v touse languages
if "`languages'" != "" {
// check whether v is valued labeled in any of the languages
local vlabeled = 0
local vlabdef = 0
foreach ln of local languages {
if "`ln'" == "`:char _dta[_lang_c]'" {
local lbl : value label `v'
}
else {
local lbl : char `v'[_lang_l_`ln']
}
if ("`lbl'" != "") local vlabeled = 1
}
if (!`vlabeled') {
return local vlabeled = 0
return local vlabdef = 0
exit
}
// if reached, v is value labeled in >=1 languages
local exist = 0
foreach ln of local languages {
LabelLine `v' `touse' `ln'
local exist = `exist' | `r(exist)'
}
return local vlabeled = 1
return local vlabdef = `exist'
}
else {
local lbl : value label `v'
return local vlabeled = "`lbl'" != ""
LabelLine `v' `touse' ""
return local vlabdef = r(exist)
}
end
program LabelLine, rclass
args v touse ln
if ("`ln'" == "") | ("`ln'" == "`:char _dta[_lang_c]'") {
local lbl : value label `v'
}
else {
local lbl : char `v'[_lang_l_`ln']
}
if "`lbl'" == "" {
if "`ln'" != "" {
dis as txt "{ralign 22:label in `ln'}: (unlabeled)"
}
return local exist = 0
exit
}
else {
if ("`ln'" != "") local inln " in `ln'"
dis as txt "{ralign 22:label`inln'}: {res:`lbl'}" _c
}
capture label list `lbl'
if _rc==111 {
dis as txt ", but label does not exist"
global T_cb_labelnotfound $T_cb_labelnotfound `v'
return local exist = 0
}
else {
tempname codes
capture qui tab `v' if `touse' & !missing(`v'), matrow(`codes') missing
if _rc {
DoTab1 `v' `touse' `lbl'
if r(exist) == "1" {
return local exist = 1
}
}
else {
local n = r(N)
if `n' == 0 {
matrix `codes' = (.)
}
local uncoded = 0
forvalues i = 1 / `=rowsof(`codes')' {
local c = `codes'[`i',1]
local s : label `lbl' `c'
if "`macval(s)'" == "." {
local s = 1
}
if ("`macval(s)'" == "`c'") local ++uncoded
}
if `uncoded' > 0 {
dis as txt ", but {res:`uncoded'} nonmissing value" ///
cond(`uncoded'==1," is", "s are") " not labeled"
global T_cb_notlabeled $T_cb_notlabeled `v'
}
else {
dis
}
return local exist = 1
}
}
end
program FullTab
args vtype v touse bfmt
tempname
tempvar cnt first
sort `touse' `v'
qui by `touse' `v' : gen long `cnt' = _N if `touse'
qui by `touse' `v' : gen byte `first' = 1 if _n==1 & `touse'
// if "`sort'" != "" {
// tempvar mcnt
// qui gen `mcnt' = - `cnt'
// }
sort `touse' `first' `mcnt' `v'
qui count if `touse'==0
local j = r(N)+1
dis as txt _col(13) "tabulation: Freq. Value"
while `first'[`j'] == 1 {
if "`vtype'" == "n" {
// numeric unlabeled
dis _col(24) as res %7.0g `cnt'[`j'] ///
_col(33) as res (`v'[`j'])
}
else if "`vtype'" == "d" {
// date
dis _col(24) as res %7.0g `cnt'[`j'] ///
_col(33) as res (`v'[`j']) ///
_col(40) as txt `bfmt' (`v'[`j'])
}
else {
// string
dis _col(24) as res %7.0g `cnt'[`j'] ///
_col(33) as txt _c
local s = `v'[`j']
Piece ye 33 40 `""`macval(s)'""'
}
local ++j
}
end
program NumExamples
args v touse languages
if "`languages'" != "" {
quiet label dir
local labdef `r(names)'
foreach ln of local languages {
if "`ln'" == "`:char _dta[_lang_c]'" {
local lbl : value label `v'
}
else {
local lbl : char `v'[_lang_l_`ln']
}
if ("`lbl'" != "") & (`:list lbl in labdef') {
local inln " in `ln'"
continue, break
}
}
}
else {
local lbl : value label `v'
}
sort `touse' `v'
qui count if `touse'==0
local f = r(N)
local wd = _N-r(N)
local text "examples`inln':"
foreach inc in .2 .4 .6 .8 {
local vv = `v'[`f'+`inc'*`wd']
local s : label `lbl' `vv'
if ("`macval(s)'" == "`vv'") local s
dis as txt "{ralign 23: `text'}" ///
_col(26) as res (`vv') _col(32) as txt `"`s"'
local text
}
end
program FullTabValueLabeled
args v touse languages
local nln : list sizeof languages
if `nln' > 0 {
foreach ln of local languages {
if "`ln'" == "`: char _dta[_lang_c]'" {
local lbl : value label `v'
}
else {
local lbl : char `v'[_lang_l_`ln']
}
local lbllist `lbllist' `lbl'
}
local lbllist : list uniq lbllist
// keep only those that are actually defined
qui label dir
local labdef `r(names)'
local lbllist : list lbllist & labdef
local nlbl : list sizeof lbllist
if `nlbl' > 3 {
tokenize `lbllist'
local lbllist `1' `2' `3'
local nlbl = 3
mac shift 3
local notshown `*'
}
}
else {
local lbllist : value label `v'
assert "`lbllist'" != ""
local nlbl = 1
}
tokenize `lbllist'
local cw = 0
forvalues i = 1 / `nlbl' {
local cw = max(`cw',`:label ``i'' maxlength',length("``i''"))
}
local cwidth = min(`cw', int((`c(linesize)'-43)/`nlbl')-3)
if `nlbl' > 1 {
dis as txt _col(13) "tabulation: Freq. Numeric" _c
forvalues i = 1 / `nlbl' {
dis `" {lalign `cwidth':`=abbrev("``i''",`cwidth')'}"' _c
}
dis
}
else {
if (`nln'==1) local incln " in `languages'"
dis as txt _col(13) "tabulation: Freq. Numeric Label`incln'"
}
tempname cnt codes
capture qui tab `v' if `touse' , matcell(`cnt') matrow(`codes') missing
if _rc {
DoTab2 `v' `touse' `nlbl' `cwidth' `notshown'
}
else {
forvalues i = 1 / `=rowsof(`cnt')' {
local c = `codes'[`i',1]
if `nlbl' == 1 {
dis _col(24) as res %7.0g `cnt'[`i',1] ///
_col(33) as res %8.0g `c' ///
_col(43) as txt _c
local s : label `1' `c'
if `"`macval(s)'"' != "`c'" {
Piece txt 43 32 `"`macval(s)'"'
}
else display
}
else {
dis _col(24) as res %7.0g `cnt'[`i',1] ///
_col(31) as res %8.0g `c' ///
_col(42) as txt _c
forvalues j = 1 / `nlbl' {
local s : label ``j'' `c' `cwidth'
if (`"`macval(s)'"' == "`c'") local s ""
dis as res `"{lalign `cwidth':`macval(s)'}"' _c
if (`j'<`nlbl') dis _skip(3) _c
}
dis
}
}
if "`notshown'" != "" {
dis as txt _n "(value labels `notshown' are not listed)"
}
}
end
program CntUniq, rclass
args v touse
quietly {
count if `touse'
ret scalar ntouse = r(N)
tempvar tag
bys `touse' `v': gen byte `tag'= 1 if _n==1 & `touse'
count if `tag'==1 & !missing(`v') & `touse'
ret scalar uniq_nmv = r(N)
if substr("`:type `v''",1,3) == "str" {
count if missing(`v') & `touse'
ret scalar miss_sys = r(N)
ret scalar miss_ext = 0
ret scalar uniq_mv = return(miss_sys) > 0
}
else {
count if `tag'==1 & missing(`v') & `touse'
ret scalar uniq_mv = r(N)
count if `v'==. & `touse' // sic -- "`v'==."
ret scalar miss_sys = r(N) // sysmiss (.)
count if missing(`v') & `touse'
ret scalar miss_ext = r(N) - return(miss_sys)
// extended mv (.a/.z)
}
}
end
program ShowUniq
args vtype uniq_nmv uniq_mv miss_sys miss_ext ntouse1
if "`vtype'" == "str" {
dis _col(10) as txt "unique values: " as res `uniq_nmv' ///
_col(51) as txt `"missing "": "' as res `miss_sys' ///
as txt "/" as res `ntouse1'
}
else { // numeric var, both miss_sys and miss_ext
dis _col(10) as txt "unique values: " as res `uniq_nmv' ///
_col(51) as txt "missing .: " as res `miss_sys' ///
as txt "/" as res `ntouse1'
if `miss_ext' > 0 {
dis _col(8) as txt "unique mv codes: " ///
as res `uniq_mv' ///
_col(50) as txt "missing .*: " ///
as res `miss_ext' as txt "/" ///
as res `ntouse1'
}
}
end
/* CrcMiss touse varlist
reports the logical relations between missingness of the first var and
missingness of the other vars in varlist
*/
program CrcMiss
syntax varlist(min=3)
gettoken touse varlist : varlist
gettoken v varlist : varlist
capt confirm string var `v'
local vs = (_rc == 0)
// v ever missing ?
capt assert !missing(`v') if `touse'
if _rc == 0 {
exit
}
tempvar ismiss newmiss
local frst 1
local abv = abbrev("`v'",12)
qui gen byte `ismiss' = missing(`v') if `touse'
qui gen byte `newmiss' = .
foreach tv of local varlist {
if "`tv'" == "`v'" {
continue
}
capt confirm string var `tv'
local tvs = (_rc == 0)
// tv ever missing ?
qui replace `newmiss' = missing(`tv') if `touse'
capt assert `newmiss' == 0 if `touse'
if !_rc {
continue
}
// v . ==> tv . ?
capt assert `ismiss' if `newmiss' & `touse'
if _rc {
continue
}
if `frst' {
local frst 0
dis as txt _n _col(9) "missing values:" _c
}
else {
dis _col(24) _c
}
local abtv = abbrev("`tv'",12)
dis as txt "{ralign 14:`abtv'}==" cond(`tvs',`""" "',"mv ") _c
// v>=. ==> tv>=.?
capt assert `newmiss' if `ismiss' & `touse'
dis as txt cond(_rc==0,"<-> ", "--> ") _c
dis as txt "`abv'==" cond(`vs',`""""',"mv")
}
end
program Units, rclass /* varname touse */
args v touse
// v always missing
capture assert missing(`v') if `touse'
if _rc == 0 {
return scalar units = .
return scalar min = .
return scalar max = .
return scalar mean = .
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -