📄 tabstat.ado
字号:
di as txt _col(6) `"by categories of: `by' `bylabel'"'
}
}
di
* loop over all nsblock blocks of statistics
local is2 0
forvalues isblock = 1/`nsblock' {
* is1..is2 are indices of statistics in a block
local is1 = `is2' + 1
local is2 = min(`nstats', `is1'+`neblock'-1)
* display header
if "`by'" != "" {
local byname = abbrev("`by'",`byw')
di as txt "{`byalign' `byw':`byname'} {...}"
}
if "`descr'" != "" {
di as txt "{ralign `varwidth':variable} {...}"
}
di as txt "{c |}" _c
forvalues is = `is1'/`is2' {
di as txt %`colwidth's "`name`is''" _c
}
local ndash = `colwidth'*(`is2'-`is1'+1)
di as txt _n "{hline `lleft'}{c +}{hline `ndash'}"
* loop over the categories of -by- (1..nby) and -total- (nby+1)
local nbyt = `nby' + ("`total'" == "")
forvalues iby = 1/`nbyt'{
forvalues i = 1/`nvars' {
if "`by'" != "" {
if `i' == 1 {
local lab = substr(`"`lab`iby''"'0, 1,`byw')
if `"`lab'"' != "Total" {
capture local val_lab : value label `by'
if "`val_lab'" == "" {
local type : type `by'
local yes_str = index("`type'", "str")
if `yes_str' == 0 {
capture local for : format `by'
capture local if_date_for = index("`for'", "%d")
capture local if_time_for = index("`for'", "%t")
if `if_date_for' > 0 | `if_time_for' > 0 {
local date_for : display `for' `lab'
di in txt `"{`byalign' `byw':`date_for'} {...}"'
}
else {
di in txt `"{`byalign' `byw':`lab'} {...}"'
}
}
else {
di in txt `"{`byalign' `byw':`lab'} {...}"'
}
}
else {
di in txt `"{`byalign' `byw':`lab'} {...}"'
}
}
else {
di in txt `"{`byalign' `byw':`lab'} {...}"'
}
}
else {
di "{space `byw'} {...}"
}
}
if "`descr'" != "" {
local avn = abbrev("`var`i''",`varwidth')
di as txt "{ralign `varwidth':`avn'} {...}"
}
di as txt "{c |}{...}"
forvalues is = `is1'/`is2' {
local s : display `fmt`i'' `Stat`iby''[`is',`i']
di as res %`colwidth's "`s'" _c
}
di
}
if (`iby' >= `nbyt') {
di as txt "{hline `lleft'}{c BT}{hline `ndash'}"
}
else if ("`sepline'" != "") | ((`iby'+1 == `nbyt') & ("`total'" == "")) {
di as txt "{hline `lleft'}{c +}{hline `ndash'}"
}
}
if `isblock' < `nsblock' {
display
}
} /* isblock */
}
else {
* display the results: horizontal = variables (block wise)
if "`descr'" == "" {
di as txt _n `"Summary statistics:`names'"'
if "`by'" != "" {
local bylabel : var label `by'
if `"`bylabel'"' != "" {
local bylabel `"(`bylabel')"'
}
di as txt `" by categories of: `by' `bylabel'"'
}
}
di
* loop over all nvblock blocks of variables
local i2 0
forvalues iblock = 1/`nvblock' {
* i1..i2 are indices of variables in a block
local i1 = `i2' + 1
local i2 = min(`nvars', `i1'+`neblock'-1)
* display header
if "`by'" != "" {
di as txt "{`byalign' `byw':`by'} {...}"
}
if "`descr'" != "" {
di as txt " stats {...}"
}
di as txt "{c |}{...}"
forvalues i = `i1'/`i2' {
* here vars are abbreviated to 8 chars
di as txt %`colwidth's abbrev("`var`i''",8) _c
}
local ndash = (`ndigit'+1)*(`i2'-`i1'+1)
di as txt _n "{hline `lleft'}{c +}{hline `ndash'}"
* loop over the categories of -by- (1..nby) and -total- (nby+1)
local nbyt = `nby' + ("`total'" == "")
forvalues iby = 1/`nbyt'{
forvalues is = 1/`nstats' {
if "`by'" != "" {
if `is' == 1 {
local lab = substr(`"`lab`iby''"'0, 1, `byw')
di as txt `"{`byalign' `byw':`lab'} {...}"'
}
else {
di as txt "{space `byw'} {...}"
}
}
if "`descr'" != "" {
* names of statistics are at most 8 chars
di as txt `"{ralign 8:`name`is''} {...}"'
}
di as txt "{c |}{...}"
forvalues i = `i1'/`i2' {
local s : display `fmt`i'' `Stat`iby''[`is',`i']
di as res %`colwidth's "`s'" _c
}
di
}
if (`iby' >= `nbyt') {
di as txt "{hline `lleft'}{c BT}{hline `ndash'}"
}
else if ("`sepline'" != "") | ((`iby'+1 == `nbyt') & ("`total'" == "")) {
di as txt "{hline `lleft'}{c +}{hline `ndash'}"
}
} /* forvalues iby */
if `iblock' < `nvblock' {
display
}
} /* forvalues iblock */
}
* save results (mainly for certification)
* ---------------------------------------
if "`save'" != "" {
if "`total'" == "" {
local iby = `nby'+1
if _caller() < 9 {
return matrix StatTot `Stat`iby''
}
else {
return matrix StatTotal `Stat`iby''
}
}
if "`by'" == "" {
exit
}
forvalues iby = 1/`nby' {
return matrix Stat`iby' `Stat`iby''
return local name`iby' `"`lab`iby''"'
}
}
end
* ---------------------------------------------------------------------------
* subroutines
* ---------------------------------------------------------------------------
/* Stats str
processes the contents() option. It returns in
r(names) -- names of statistics, separated by blanks
r(expr) -- r() expressions for statistics, separated by blanks
r(summopt) -- option for summarize command (meanonly, detail)
note: if you add statistics, ensure that the name of the statistic
is at most 8 chars long.
*/
program define Stats, rclass
if `"`0'"' == "" {
local opt "mean"
}
else {
local opt `"`0'"'
}
* ensure that order of requested statistics is preserved
* invoke syntax for each word in input
local class 0
foreach st of local opt {
local 0 = lower(`", `st'"')
capt syntax [, n MEan sd Variance SUm COunt MIn MAx Range SKewness Kurtosis /*
*/ SDMean SEMean p1 p5 p10 p25 p50 p75 p90 p95 p99 iqr q MEDian CV ]
if _rc {
di in err `"unknown statistic: `st'"'
exit 198
}
if "`median'" != "" {
local p50 p50
}
if "`count'" != "" {
local n n
}
* class 1 : available via -summarize, meanonly-
* summarize.r(N) returns #obs (note capitalization)
if "`n'" != "" {
local n N
}
local s "`n'`min'`mean'`max'`sum'"
if "`s'" != "" {
local names "`names' `s'"
local expr "`expr' r(`s')"
local class = max(`class',1)
}
if "`range'" != "" {
local names "`names' range"
local expr "`expr' r(max)-r(min)"
local class = max(`class',1)
}
* class 2 : available via -summarize-
if "`sd'" != "" {
local names "`names' sd"
local expr "`expr' r(sd)"
local class = max(`class',2)
}
if "`sdmean'" != "" | "`semean'"!="" {
local names "`names' se(mean)"
local expr "`expr' r(sd)/sqrt(r(N))"
local class = max(`class',2)
}
if "`variance'" != "" {
local names "`names' variance"
local expr "`expr' r(Var)"
local class = max(`class',2)
}
if "`cv'" != "" {
local names "`names' cv"
local expr "`expr' (r(sd)/r(mean))"
local class = max(`class',2)
}
* class 3 : available via -detail-
local s "`skewness'`kurtosis'`p1'`p5'`p10'`p25'`p50'`p75'`p90'`p95'`p99'"
if "`s'" != "" {
local names "`names' `s'"
local expr "`expr' r(`s')"
local class = max(`class',3)
}
if "`iqr'" != "" {
local names "`names' iqr"
local expr "`expr' r(p75)-r(p25)"
local class = max(`class',3)
}
if "`q'" != "" {
local names "`names' p25 p50 p75"
local expr "`expr' r(p25) r(p50) r(p75)"
local class = max(`class',3)
}
}
return local names `names'
return local expr `expr'
if `class' == 1 {
return local summopt "meanonly"
}
else if `class' == 3 {
return local summopt "detail"
}
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -