📄 estat_summ.ado
字号:
program Stats, rclass
args eqname vn labels vlwidth tsop wgtexp
tempname stats
matrix `stats' = J(1,4,.)
capture confirm variable `vn'
local notavar = _rc
local vvn `vn'
local abvn = abbrev("`vn'",12)
if substr("`tsop'",1,1) == "@" {
// output line with varname and label only
if `notavar' {
if "`labels'" != "" {
dis as txt %12s "`abvn'" ///
_col(14) "{c |} <not found>"
}
else {
dis as txt " " %12s "`abvn'" ///
_col(16) "{c |} <not found>"
}
}
else {
local vvn : char `vn'[varname]
if "`vvn'" == "" {
local vvn `vn'
}
local abvn = abbrev("`vvn'",12)
if "`labels'" != "" {
local vlab : var label `vn'
local vl : piece 1 `vlwidth' of `"`vlab'"'
dis as txt %12s "`abvn'" _col(14) "{c |}" ///
_col(57) as txt `"`vl'"'
}
else {
dis as txt " " %12s "`abvn'" _col(16) "{c |}"
}
if "`labels'" != "" {
// remainder of variable label
local j 2
local vl : piece `j' `vlwidth' of `"`vlab'"'
while `"`vl'"' != "" {
dis as txt _col(14) "{c |}" _col(57) `"`vl'"'
local ++j
local vl : piece `j' `vlwidth' ///
of `"`vlab'"'
}
}
}
local tsop = substr("`tsop'",2,.)
}
if "`tsop'" == "__." {
local op
local tsop "--."
}
else if "`tsop'" == "0" {
local op
}
else {
local op `tsop'
}
if "`tsop'" != "0" {
// output ts op with summary values (no var or label output)
if `notavar' {
if "`labels'" != "" {
dis as txt %12s "`tsop'" ///
_col(14) "{c |} <not found>"
}
else {
dis as txt " " %12s "`tsop'" ///
_col(16) "{c |} <not found>"
}
}
else {
capture confirm numeric variable `vn'
if !_rc {
quiet summ `op'`vn' `wgtexp' if e(sample)
matrix `stats' = (r(mean),r(sd),r(min),r(max))
if "`labels'" != "" {
dis as txt %12s "`tsop'" _col(14) "{c |}" ///
as res _col(16) %9.0g r(mean) ///
_col(27) %9.0g r(sd) ///
_col(37) %8.0g r(min) ///
_col(46) %8.0g r(max)
}
else {
dis as txt " " %12s "`tsop'" ///
_col(16) "{c |}" ///
as res _col(20) %9.0g r(mean) ///
_col(33) %9.0g r(sd) ///
_col(45) %8.0g r(min) ///
_col(56) %8.0g r(max)
}
}
else {
if "`labels'" != "" {
dis as txt %12s "`tsop'" ///
_col(14) "{c |} <string variable>"
}
else {
dis as txt " " %12s "`tsop'" ///
_col(16) "{c |} <string variable>"
}
}
}
}
else { // regular case -- no ts ops
if `notavar' {
if "`labels'" != "" {
dis as txt %12s "`abvn'" ///
_col(14) "{c |} <not found>"
}
else {
dis as txt " " %12s "`abvn'" ///
_col(16) "{c |} <not found>"
}
}
else {
local vvn : char `vn'[varname]
if "`vvn'" == "" {
local vvn `vn'
}
local abvn = abbrev("`vvn'",12)
if "`labels'" != "" {
local vlab : var label `vn'
local vl : piece 1 `vlwidth' of `"`vlab'"'
}
capture confirm numeric variable `vn'
if "`labels'" != "" {
if !_rc {
quiet summ `vn' `wgtexp' if e(sample)
matrix `stats' = (r(mean),r(sd),r(min),r(max))
dis as txt %12s "`abvn'" _col(14) "{c |}" ///
as res _col(16) %9.0g r(mean) ///
_col(27) %9.0g r(sd) ///
_col(37) %8.0g r(min) ///
_col(46) %8.0g r(max) ///
_col(57) as txt `"`vl'"'
}
else {
dis as txt %12s "`abvn'" ///
_col(14) "{c |} <string variable>" ///
_col(57) as txt `"`vl'"'
}
// remainder of variable label
local j 2
local vl : piece `j' `vlwidth' of `"`vlab'"'
while `"`vl'"' != "" {
dis as txt _col(14) "{c |}" ///
_col(57) `"`vl'"'
local ++j
local vl : piece `j' `vlwidth' ///
of `"`vlab'"'
}
}
else {
if !_rc {
quiet summ `vn' `wgtexp' if e(sample)
matrix `stats' = (r(mean),r(sd),r(min),r(max))
dis as txt " " %12s "`abvn'" ///
_col(16) "{c |}" ///
as res _col(20) %9.0g r(mean) ///
_col(33) %9.0g r(sd) ///
_col(45) %8.0g r(min) ///
_col(56) %8.0g r(max)
}
else {
dis as txt " " %12s "`abvn'" ///
_col(16) "{c |} <string variable>"
}
}
}
}
matrix colnames `stats' = mean sd min max
matrix rownames `stats' = `"`eqname':`op'`vvn'"'
return matrix stats = `stats'
end
// Header llength fw
// displays table header line length llength
// fw is freq. weight variable if it is to be used in computing N
//
program Header
args labels llength fw
tempvar last N touse
quietly gen byte `touse' = e(sample)
if "`fw'" == "" {
quietly count if `touse'
}
else { // have fweights
quietly summ `touse' [fw=`fw'], meanonly
}
local Obs = r(N)
if "`e(clustvar)'" == "" {
local ll = `llength' - 17
if "`labels'" != "" {
dis _n as txt "Estimation sample " as res "`e(cmd)'" ///
_col(`ll') as txt "Number of obs =" as res %7.0f `Obs'
}
else {
dis _n as txt " Estimation sample " ///
as res "`e(cmd)'" ///
_col(`=`ll'+6') as txt ///
"Number of obs =" as res %7.0f `Obs'
}
}
else {
local ll = `llength' - 26
if "`labels'" != "" {
dis _n as txt "Estimation sample " as res "`e(cmd)'" ///
_col(`ll') as txt "Number of obs =" ///
as res %9.0g `Obs' _n
}
else {
dis _n as txt " Estimation sample " ///
as res "`e(cmd)'"
dis _col(`=`ll'+6') as txt "Number of obs =" ///
as res %9.0g `Obs' _n
}
qui bys `touse' `e(clustvar)' : gen `last' = _N==_n if `touse'
if "`fw'" == "" {
qui bys `touse' `e(clustvar)' : gen `N' = _N if `touse'
}
else {
qui bys `touse' `e(clustvar)' : ///
gen `N'=sum(`fw') if `touse'
}
quietly count if `last'==1
if "`labels'" != "" {
dis _col(`ll') as txt "Number of clusters =" ///
as res %9.0g r(N)
}
else {
dis _col(`=`ll'+6') as txt "Number of clusters =" ///
as res %9.0g r(N)
}
quietly summ `N' if `last'==1
if "`labels'" != "" {
dis _col(`ll') as txt "Obs per cluster: min =" ///
as res %9.0f r(min)
dis _col(`ll') as txt " avg =" ///
as res %9.1f r(mean)
dis _col(`ll') as txt " max =" ///
as res %9.0f r(max)
}
else {
dis _col(`=`ll'+6') as txt "Obs per cluster: min =" ///
as res %9.0f r(min)
dis _col(`=`ll'+6') as txt " avg =" ///
as res %9.1f r(mean)
dis _col(`=`ll'+6') as txt " max =" ///
as res %9.0f r(max)
}
}
end
program HeaderTab
args llength labels
dis
Line "TT" `llength' "`labels'"
if "`labels'" != "" {
dis as txt " Variable {c |} " ///
" Mean Std. Dev. Min Max Label"
}
else {
dis as txt " Variable {c |} " ///
" Mean Std. Dev. Min Max"
}
Line "+" `llength' "`labels'"
end
// Line ch n
// displays a line (in green) of length n with separator ch (+, BT etc)
//
program Line
args ch llength labels
if "`labels'" != "" {
dis as txt "{hline 13}{c `ch'}{hline `=`llength'-10'}"
}
else {
dis as txt " {hline 13}{c `ch'}{hline `=`llength'-6'}"
}
end
program DispEqn
args eqn labels
local eqn = abbrev("`eqn'",12)
if "`labels'" != "" {
dis as res "`eqn'" _col(14) as txt "{c |}"
}
else {
dis as res " `eqn'" _col(16) as txt "{c |}"
}
end
program SplitTSOp, rclass
foreach v of local 0 {
local dot = strpos("`v'",".")
if `dot' {
local tsops `tsops' `= substr("`v'",1,`dot')'
local vars `vars' `= substr("`v'",`=`dot'+1',.)'
}
else {
local tsops `tsops' 0
local vars `vars' `v'
}
}
return local tsops `tsops'
return local vars `vars'
end
exit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -