📄 codebook.ado
字号:
*! version 1.3.18 05oct2004
program codebook, rclass
version 8.1, born(09sep2003) missing
// codebook stores information in globals T_cb_*
// necessary to make report of problems
nobreak {
macro drop T_cb_*
capture noisily break {
Codebook_u `0'
}
local rc = _rc
if `rc' == 0 {
ReturnGlobals
return add
}
macro drop T_cb_*
}
exit `rc'
end
program Codebook_u
syntax [varlist] [if] [in] [, ///
All Tabulate(integer 9) Mv Notes ///
Header Problems Detail ///
LANGuages LANGuages2(str) ]
if "`problems'" != "" & "`detail'" == "" {
local quiet quiet
local notes
}
if "`all'" != "" {
local header header
local notes notes
}
ParseLanguage `"`languages'"' `"`languages2'"'
local lns `s(lns)'
if "`lns'" != "" {
local lnopt languages(`lns')
}
if "`header'" != "" {
Header, `lnopt' `notes'
}
// program-to-be-run-sortpreserve
Codebook_vars `varlist' `if' `in' , ///
`mv' `quiet' `notes' tabulate(`tabulate') `lnopt'
if "`problems'" != "" {
if ("`quiet'" == "") display
ReportProblems
}
end
program Codebook_vars, sortpreserve
syntax [varlist] [if] [in] [, ///
Tabulate(integer 9) Mv Notes quiet ///
languages(str) ]
marksample touse, novarlist
qui count if `touse'
if r(N) == 0 {
error 2000
}
// The DoType commands write reports on a single variable
// They also set the globals with varlist with potential problems
if "`languages'" != "" {
local lnopt languages(`languages')
}
foreach v of loc varlist {
if "`quiet'" == "" {
VarHeader `v' , `lnopt'
}
capture confirm string variable `v'
if _rc {
local fmt : format `v'
local ch = substr(`"`fmt'"', 2, 1)
local DoType = cond(inlist(`"`ch'"',"d","t"), "DoDate", "DoNum")
}
else {
local DoType DoStr
}
`quiet' `DoType' `v' `touse' "`tabulate'" "`mv'" "`languages'"
if "`notes'" != "" {
note `v'
}
}
global T_cb_labelnotfound : list uniq global T_cb_labelnotfound
global T_cb_notlabeled : list uniq global T_cb_notlabeled
end
program DoNum
args v touse tabulate mv languages
dis _col(19) as txt "type: numeric (" as res `"`:type `v''"' as txt ")"
Labeled `v' `touse' "`languages'"
local vlabeled = `r(vlabeled)' // 0/1 : value labeled attached
local vlabdef = `r(vlabdef)' // 0/1 : value labeled attached & defined
dis
Units `v' `touse'
#del ;
dis _col(18) as txt "range: ["
as res r(min)
as txt ","
as res r(max)
as txt "]"
_col(55) as txt "units: "
as res r(units) ;
#del cr
CntUniq `v' `touse'
local ntouse = r(ntouse) // number of obs
local miss_sys = r(miss_sys) // mv code .
local miss_ext = r(miss_ext) // emv code .a/.z
local uniq_nmv = r(uniq_nmv)
local uniq_mv = r(uniq_mv)
if `uniq_nmv' < 2 {
global T_cb_cons $T_cb_cons `v'
}
ShowUniq num `uniq_nmv' `uniq_mv' `miss_sys' `miss_ext' `ntouse'
dis
if `uniq_nmv' <= `tabulate' {
if `vlabdef' {
FullTabValueLabeled `v' `touse' "`languages'"
}
else {
FullTab n `v' `touse' ""
}
}
else {
if `vlabdef' {
NumExamples `v' `touse' "`languages'"
}
else {
qui summ `v' if `touse' , detail
dis as txt _col(19) "mean:" ///
as res _col(26) %8.0g r(mean) _n ///
as txt _col(15) "std. dev:" ///
as res _col(26) %8.0g sqrt(r(Var)) _n
dis as txt _col(12) "percentiles:" ///
_col(32) "10%" ///
_col(42) "25%" ///
_col(52) "50%" ///
_col(62) "75%" ///
_col(72) "90%"
dis as res _col(27) %8.0g r(p10) ///
_col(37) %8.0g r(p25) ///
_col(47) %8.0g r(p50) ///
_col(57) %8.0g r(p75) ///
_col(67) %8.0g r(p90)
}
}
if `"`mv'"' != "" {
CrcMiss `touse' `v' _all
}
end
program DoDate
args v touse tabulate mv languages
local type : type `v'
local fmt : format `v'
local ch1 = substr(`"`fmt'"',2,1)
local ch2 = substr(`"`fmt'"',3,1)
if `"`ch1'"' == "d" {
local ch1 "t"
local ch2 "d"
}
if `"`ch2'"' == "d" {
local desc "daily"
local udesc "days"
}
else if `"`ch2'"' == "w" {
local desc "weekly"
local udesc "weeks"
}
else if `"`ch2'"' == "m" {
local desc "monthly"
local udesc "months"
}
else if `"`ch2'"' == "q" {
local desc "quarterly"
local udesc "quarters"
}
else if `"`ch2'"' == "h" {
local desc "halfyearly"
local udesc "half years"
}
else if `"`ch2'"' == "y" {
local desc "yearly"
local udesc "years"
}
else {
DoNum `v' `touse' `tabulate' `mv' "`languages'"
exit
}
local bfmt "%t`ch2'"
dis _col(19) as txt "type: " ///
as txt "numeric `desc' date (" as res `"`type'"' as txt ")"
if "`languages'" != "" {
quiet label language
local inln " in `r(language)'"
}
Labeled `v' `touse' "`languages'"
local vlabeled = `r(vlabeled)' // 0/1 : value labeled attached
local vlabdef = `r(vlabdef)' // 0/1 : value labeled attached & defined
dis
Units `v' `touse'
#del ;
dis _col(18) as txt "range: ["
as res r(min)
as txt ","
as res r(max)
as txt "]"
_col(55) as txt "units: "
as res r(units) ;
dis _col(8) as txt "or equivalently: ["
as res trim(string(r(min),"`bfmt'"))
as txt ","
as res trim(string(r(max),"`bfmt'"))
as txt "]"
_col(55) as txt "units: "
as res "`udesc'" ;
#del cr
CntUniq `v' `touse'
local ntouse = r(ntouse) // n of obs
local miss_sys = r(miss_sys) // mv code .
local miss_ext = r(miss_ext) // mv code .a/.z
local uniq_nmv = r(uniq_nmv) // unique nonmissing values
local uniq_mv = r(uniq_mv) // unique missing values
if `uniq_nmv' < 2 {
global T_cb_cons $T_cb_cons `v'
}
ShowUniq date `uniq_nmv' `uniq_mv' `miss_sys' `miss_ext' `ntouse'
dis
if `uniq_nmv' <= `tabulate' {
FullTab d `v' `touse' "`bfmt'"
}
else if !`vlabeled' {
qui summ `v' if `touse', detail
local x = trim(string(r(mean), "`bfmt'"))
if "`x'" == "." {
local xnote " (invalid `desc' date)"
}
else {
local diff = r(mean) - int(r(mean))
if `diff' != 0 {
if "`ch2'" == "d" {
loc diff = round(24*`diff',1)
local xnote "hour"
}
else if "`ch2'" == "w" {
local diff = trim(string( /*
*/ 7*`diff', "%9.1f"))
local xnote "day"
}
else if "`ch2'" == "m" {
local diff = round(365.25/12*`diff',1)
local xnote "day"
}
else if "`ch2'" == "q" {
local diff = trim(string(/*
*/ 3*`diff',"%9.1f"))
local xnote "month"
}
else if "`ch2'" == "h" {
local diff = trim(string(/*
*/ 6*`diff',"%9.1f"))
local xnote "month"
}
else {
local diff = trim(string(/*
*/ 6*`diff',"%9.1f"))
local xnote "month"
}
if `diff' == int(`diff') {
local diff = int(`diff') /* sic */
}
/*
if "`diff'" == "1.0" {
local diff "1"
}
*/
if "`diff'" != "1" {
local xnote "`xnote's"
}
local xnote " (+ `diff' `xnote')"
}
}
dis as txt _col(19) "mean:" ///
as res _col(26) %8.0g r(mean) ///
as txt " = " as res "`x'" as txt "`xnote'" _n ///
as txt _col(15) "std. dev:" ///
as res _col(26) %8.0g sqrt(r(Var)) _n
dis as txt _col(12) "percentiles:" ///
_col(32) "10%" ///
_col(42) "25%" ///
_col(52) "50%" ///
_col(62) "75%" ///
_col(72) "90%"
dis as res _col(27) %8.0g r(p10) ///
_col(37) %8.0g r(p25) ///
_col(47) %8.0g r(p50) ///
_col(57) %8.0g r(p75) ///
_col(67) %8.0g r(p90)
dis as res _col(26) %9s trim(string(r(p10),"`bfmt'")) ///
_col(36) %9s trim(string(r(p25),"`bfmt'")) ///
_col(46) %9s trim(string(r(p50),"`bfmt'")) ///
_col(56) %9s trim(string(r(p75),"`bfmt'")) ///
_col(66) %9s trim(string(r(p90),"`bfmt'"))
}
else {
NumExamples `v' `touse' "`languages'"
}
if `"`mv'"'!="" {
CrcMiss `touse' `v' _all
}
capt assert `v'==round(`v') if `touse'
if _rc {
global T_cb_realdate $T_cb_realdate `v'
}
end
program DoStr
args v touse tabulate mv languages
tempvar cnt
local type : type `v'
local tabvar `v'
dis _col(19) as txt "type: " ///
as txt "string (" as res `"`type'"' as txt ")" _c
qui gen byte `cnt' = length(`v') if `touse'
qui summ `cnt' if `touse' , meanonly
if r(max) != real(substr("`type'",4,.)) {
dis as txt ", but longest is str" r(max)
global T_cb_str_type $T_cb_str_type `v'
}
else {
dis
}
drop `cnt'
dis
CntUniq `v' `touse'
local uniq_nmv `r(uniq_nmv)'
if `uniq_nmv' < 2 {
global T_cb_cons $T_cb_cons `v'
}
ShowUniq str `uniq_nmv' `r(uniq_mv)' `r(miss_sys)' `r(miss_ext)' `r(ntouse)'
dis
if `uniq_nmv' <= `tabulate' {
FullTab s `v' `touse' ""
}
else {
dis as txt _col(15) "examples: " _c
sort `touse' `v'
qui count if `touse'==0
local f = r(N)
local wd = _N-r(N)
local i = 0
foreach inc in .2 .4 .6 .8 {
local x = `v'[`f'+`inc'*`wd']
if `i++' {
dis _col(26) _c
}
Piece res 26 53 `""`x'""'
}
}
if `"`mv'"' != "" {
CrcMiss `touse' `v' _all
}
// update problem globals
qui count if `touse' & substr(`v',1,1)==" "
if r(N) {
local haslead 1
local ttl "leading"
global T_cb_str_leading $T_cb_str_leading `v'
}
else {
local haslead 0
}
qui count if `touse' & substr(`v',-1,1)==" "
if r(N) {
local hastrail 1
local ttl "trailing"
global T_cb_str_trailing $T_cb_str_trailing `v'
}
else {
local hastrail 0
}
if `haslead' | `hastrail' {
tempvar clean
qui gen `type' `clean' = trim(`v') if `touse'
capture assert index(`clean'," ")==0 if `touse'
drop `clean'
}
else {
capture assert index(`v'," ")==0 if `touse'
}
if _rc {
local hasem 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -