📄 statsby.ado
字号:
*! version 3.2.5 27feb2005
program statsby
version 9, missing
local version : di "version " string(_caller()) ", missing:"
// Stata 8 syntax
capture _on_colon_parse `0'
if c(rc) | `"`s(after)'"' == "" {
gettoken old : 0 , qed(qed)
if `qed' {
`version' statsby_8 `0'
exit
}
}
`version' capture noisily StatsBy `0'
_prefix_clear, e
exit c(rc)
end
program StatsBy, rclass
version 9, missing
local version : di "version " string(_caller()) ", missing:"
// <my_stuff> : <command>
_on_colon_parse `0'
local command `"`s(after)'"'
local 0 `"`s(before)'"'
// quick check for -force- option
syntax [anything(name=exp_list equalok)] ///
[fw iw pw aw] [if] [in] [, ///
FORCE ///
Level(passthru) ///
* /// other options
]
// NOTE: -force- allows command to be a "svy" command
if "`weight'" != "" {
local wgt [`weight'`exp']
}
// parse the command and check for conflicts
`version' _prefix_command statsby `wgt' `if' `in' , ///
`level': `command'
if "`s(wtype)'" == "pweight" {
di as err "`s(wtype)'s are not supported by statsby"
exit 101
}
local version `"`s(version)'"'
local cmdname `"`s(cmdname)'"'
local cmdargs `"`s(anything)'"'
local wgt `"`s(wgt)'"'
local wtype `"`s(wtype)'"'
local wexp `"`s(wexp)'"'
local cmdif `"`s(if)'"'
local cmdin `"`s(in)'"'
local cmdopts `"`s(options)'"'
local rest `"`s(rest)'"'
local level `"`s(level)'"'
// ignore s(efopt)
local command `"`s(command)'"'
local command1 `"`version' `cmdname' `cmdargs' `wgt'"'
local command2 `"`cmdopts' `rest'"'
local command2 : list retok command2
if "`cmdin'" == "" {
if `"`cmdopts'"' != "" {
local comma ","
}
local commandin `"`command1' `cmdif' in 1/l`comma'`command2'"'
}
local exclude permute simulate statsby
if `:list cmdname in exclude' ///
| ("`force'" == "" & substr("`cmdname'",1,3) == "svy") {
di as err "`cmdname' is not supported by statsby"
exit 199
}
// now check the rest of the options
local 0 `", `options'"'
syntax [, ///
BY(string) /// allows by(varlist, missing)
Total ///
Subsets ///
CLEAR ///
noDOTS ///
SAving(string) ///
DOUBle /// not documented
noLegend ///
Verbose ///
NOIsily /// "prefix" options
TRace ///
]
// check options
if `"`saving'"' == "" {
tempfile saving
local filetmp "yes"
}
else {
if `"`clear'"' != "" {
di as err ///
"clear and saving() are mutually exclusive options"
exit 198
}
_prefix_saving `saving'
local saving `"`s(filename)'"'
if "`double'" == "" {
local double `"`s(double)'"'
}
local every `"`s(every)'"'
local replace `"`s(replace)'"'
}
if c(changed) & ("`clear'" == "") & (`"`filetmp'"' == "yes" ) {
error 4
}
if `"`by'"' != "" {
_parse comma by missing : by
unab by : `by'
}
if substr("`missing'", 1, 1) == "," {
local missing : subinstr local missing "," ""
local missing `=trim(`"`missing'"')'
local lkey = length(`"`missing'"')
if `"`missing'"' != substr("missing", 1, max(3,`lkey')) {
di as error "by() incorrectly specified"
exit 198
}
}
if "`trace'" != "" {
local noisily noisily
local traceon set trace on
local traceoff set trace off
}
if "`noisily'" != "" {
local dots nodots
}
local nodots `dots'
local dots = cond("`dots'" != "" | "`noisily'" != "", "*", "_dots")
// preliminary parse of <exp_list>
_prefix_explist `exp_list', stub(_stat_)
local eqlist `"`s(eqlist)'"'
local idlist `"`s(idlist)'"'
local explist `"`s(explist)'"'
local eexplist `"`s(eexplist)'"'
_prefix_note `cmdname', `nodots'
if "`noisily'" != "" {
di "statsby: First call to `cmdname' with data as is:" _n
di as inp `". `command'"'
}
// run the command using the entire dataset
_prefix_clear, e r
local inable inable
if `"`cmdin'"' == "" {
capture noisily quietly `noisily' `commandin'
if c(rc) {
local inable
}
}
if "`inable'" == "" | `"`cmdin'"' != "" {
`traceon'
capture noisily quietly `noisily' ///
`version' `command'
`traceoff'
}
local rc = c(rc)
// error occurred while running on entire dataset
if `rc' {
_prefix_run_error `rc' statsby `cmdname'
}
// determine default <exp_list>, or generate an error message
if `"`exp_list'"' == "" {
_prefix_explist, stub(_stat_) default
local eqlist `"`s(eqlist)'"'
local idlist `"`s(idlist)'"'
local explist `"`s(explist)'"'
local eexplist `"`s(eexplist)'"'
}
// expand eexp's that may be in eexplist, and build a matrix of the
// computed values from all expressions
tempname b
_prefix_expand `b' `explist', ///
stub(_stat_) ///
eexp(`eexplist') ///
colna(`idlist') ///
coleq(`eqlist') ///
// blank
local k_eq `s(k_eq)'
local k_exp `s(k_exp)'
local k_eexp `s(k_eexp)'
local K = `k_exp' + `k_eexp'
local k_extra `s(k_extra)'
local names `"`s(enames)' `s(names)'"'
local legnames `"`names'"'
local express `"`s(explist)'"'
local eexpress `"`s(eexplist)'"'
forval i = 1/`K' {
local exp`i' `"`s(exp`i')'"'
}
forval i = 1/`k_eexp' {
gettoken ename legnames : legnames
local elegnames `elegnames' `ename'
}
local legopts command(`command') ///
enames(`elegnames)') ///
names(`legnames)') ///
express(`express') ///
`noisily' `verbose'
// list of the expressions
forvalues i = 1/`K' {
local exps `exps' (`exp`i'')
}
// display header
if "`legend'" == "" {
_prefix_legend statsby, `legopts'
if `"`by'"' == "" {
di as txt %`s(col1)'s "by" `": <none>"'
}
else di as txt %`s(col1)'s "by" `": `by'"'
}
tempvar touse
mark `touse' `cmdif' `cmdin'
if `"`missing'"' == "" {
if `"`by'"' != "" {
markout `touse' `by', strok
}
}
quietly replace `touse' = . if `touse' == 0
if "`by'" != "" {
foreach byvar of local by {
local byvartype : type `byvar'
local postby `postby' `byvartype' `byvar'
}
}
local badnames : list names & postby
if `"`badnames'"' != "" {
gettoken badname : badnames
di as err "`badname' already defined"
exit 198
}
tempname postname
postfile `postname' `postby' `names' using `"`saving'"', `double' ///
`replace' `every'
di
if "`subsets'" != "subsets" {
if "`nodots'" == "" | "`noisily'" != "" {
_dots 0, title(Statsby groups) `nodots'
}
`version' PostGroups `touse' = `K', ///
cmd(`command') ///
command1(`command1') ///
command2(`command2') ///
by(`by') ///
by0(`by') ///
postname(`postname') ///
dots(`dots') ///
`noisily' ///
`trace' ///
`total' ///
express(`exps') ///
`inable' ///
// blank
return scalar N_groups = r(N_groups)
local count `r(N_groups)'
}
else {
if "`nodots'" == "" | "`noisily'" != "" {
_dots 0, title(Statsby subsets) `nodots'
}
`version' SubSets `touse' = `K', ///
cmd(`command') ///
command1(`command1') ///
command2(`command2') ///
by(`by') ///
postname(`postname') ///
dots(`dots') ///
`noisily' ///
`trace' ///
express(`exps') ///
`inable' ///
// blank
return scalar N_subsets = r(N_subsets)
local count `r(N_subsets)'
}
`dots' `count'
// save variable labels of by vars
if "`by'" != "" {
tokenize `by'
local j = 1
while "``j''" != "" {
local varlb`j' : variable label ``j''
local lb`j' : value label ``j''
if "`lb`j''" != "" {
tempfile f`j'
quietly label save `lb`j'' using `"`f`j''"'
}
local j = `j' + 1
}
}
postclose `postname'
if `"`filetmp'"' == "" {
preserve
}
// get/save the labels of the by vars
quietly use `"`saving'"', clear
if "`by'" != "" {
sort `by'
tokenize `by'
local j = 1
while "``j''" != "" {
label var ``j'' `"`varlb`j''"'
if `"`f`j''"' != "" {
quietly run `"`f`j''"'
label val ``j'' `lb`j''
}
local j = `j' + 1
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -