⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabstat.ado

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 ADO
📖 第 1 页 / 共 2 页
字号:
*! version 2.8.2  03jan2005
program define tabstat, rclass byable(recall) sort
	version 8, missing

	syntax varlist(numeric) [if] [in] [aw fw] [ , /*
	*/ 	BY(varname) CASEwise Columns(str) Format Format2(str) /*
	*/ 	LAbelwidth(int -1) VArwidth(int -1) LOngstub Missing /*
	*/ 	SAME SAVE noSEParator Statistics(str) STATS(str) noTotal ]

	if "`casewise'" != "" {
		local same same
	}

	if `"`stats'"' != "" {
		if `"`statistics'"' != "" {
			di as err /*
			*/ "may not specify both statistics() and stats() options"
			exit 198
		}
		local statistics `"`stats'"'
		local stats
	}

	if "`total'" != "" & "`by'" == "" {
		di as txt "nothing to display"
		exit 0
	}

	if "`format'" != "" & `"`format2'"' != "" {
		di as err "may not specify both format and format()"
		exit 198
	}
	if `"`format2'"' != "" {
		capt local tmp : display `format2' 1
		if _rc {
			di as err `"invalid %fmt in format(): `format2'"'
			exit 120
		}
	}

	if `"`columns'"' == "" {
		local incol "variables"
	}
	else if `"`columns'"' == substr("variables",1,length(`"`columns'"')) {
		local incol "variables"
	}
	else if `"`columns'"' == substr("statistics",1,length(`"`columns'"')) {
		local incol "statistics"
	}
	else if `"`columns'"' == "stats" {
		local incol "statistics"
	}
	else {
		di as err `"column(`columns') invalid -- specify "' /*
			*/ "column(variables) or column(statistics)"
		exit 198
	}

	if "`longstub'" != "" | "`by'" == "" | `varwidth' != -1 {
		local descr descr
	}

	if `varwidth' == -1 {
		local varwidth 12
	}
	else if !inrange(`varwidth',8,16) {
		local varwidth = clip(`varwidth',8,16)
		dis as txt ///
	    "(option varwidth() outside valid range 8..16; `varwidth' assumed)"
	}

	if `labelwidth' == -1 {
		local labelwidth 16
	}
	else if !inrange(`labelwidth',8,32) {
		local labelwidth = clip(`labelwidth',8,32)
		dis as txt ///
	"(option labelwidth() outside valid range 8..32; `labelwidth' assumed)"
	}

	* sample selection

	marksample touse, novar
	if "`same'" != "" {
		markout `touse' `varlist'
	}
	if "`by'" != "" & "`missing'" == "" {
		markout `touse' `by' , strok
	}
	qui count if `touse'
	local ntouse = r(N)
	if `ntouse' == 0 {
		error 2000
	}
	if `"`weight'"' != "" {
		local wght `"[`weight'`exp']"'
	}

	// varlist -> var1, var2, ... variables
	//            fmt1, fmt2, ... display formats

	tokenize "`varlist'"
	local nvars : word count `varlist'
	forvalues i = 1/`nvars' {
		local var`i' ``i''
		if "`format'" != "" {
			local fmt`i' : format ``i''
		}
		else if `"`format2'"' != "" {
			local fmt`i' `format2'
		}
		else {
			local fmt`i' %9.0g
		}
	}
	if `nvars' == 1 & `"`columns'"' == "" {
		local incol statistics
	}

	* Statistics

	Stats `statistics'
	local stats   `r(names)'
	local expr    `r(expr)'
	local summopt `r(summopt)'
	local nstats : word count `stats'

	tokenize `expr'
	forvalues i = 1/`nstats' {
		local expr`i' ``i''
	}
	tokenize `stats'
	forvalues i = 1/`nstats' {
		local name`i' ``i''
		local names "`names' ``i''"
		if `i' < `nstats' {
			local names "`names',"
		}
	}
	if "`separator'" == "" & ( (`nstats' > 1 & "`incol'" == "variables") /*
		     */         |(`nvars' > 1  & "`incol'" == "statistics")) {
		local sepline yes
	}

	local matsize : set matsize
	local matreq = max(`nstats',`nvars')
	if `matsize' < `matreq' {
		di as err /*
		*/ "set matsize to at least `matreq' (see help matsize for details)"
		exit 908
	}

	* compute the statistics
	* ----------------------

	if "`by'" != "" {
		* conditional statistics are saved in matrices Stat1, Stat2, etc

		* the data are sorted on by groups, putting unused obs last
		* be careful not to change the sort order
		* note that touse is coded -1/0 rather than 1/0!

		qui replace `touse' = - `touse'
		sort `touse' `by'

		local bytype : type `by'
		local by2 0
		local iby 1
		while `by2' < `ntouse'  {
			tempname Stat`iby'
			mat `Stat`iby'' = J(`nstats',`nvars',0)
			mat colnames `Stat`iby'' = `varlist'
			mat rownames `Stat`iby'' = `stats'

			* range `iby1'/`iby2' refer to obs in the current by-group
			local by1 = `by2' + 1
			qui count if (`by'==`by'[`by1']) & (`touse')
			local by2 = `by1' + r(N) - 1

			* loop over all variables
			forvalues i = 1/`nvars' {
				qui summ `var`i'' in `by1'/`by2' `wght', `summopt'
				forvalues is = 1/`nstats' {
					mat `Stat`iby''[`is',`i'] = `expr`is''
				}
			}

			* save label for groups in lab1, lab2 etc
			if substr("`bytype'",1,3) != "str" {
				local iby1 = `by'[`by1']
				local lab`iby' : label (`by') `iby1'
			}
			else {
				local lab`iby' = `by'[`by1']
			}

			local iby = `iby' + 1
		}
		local nby = `iby' - 1
	}
	else {
		local nby 0
	}

	if "`total'" == "" {
		* unconditional (Total) statistics are stored in Stat`nby+1'
		local iby = `nby'+1

		tempname Stat`iby'
		mat `Stat`iby'' = J(`nstats',`nvars',0)
		mat colnames `Stat`iby'' = `varlist'
		mat rownames `Stat`iby'' = `stats'

		forvalues i = 1/`nvars' {
			qui summ `var`i'' if `touse' `wght' , `summopt'
			forvalues is = 1/`nstats' {
				mat `Stat`iby''[`is',`i'] = `expr`is''
			}
		}
		local lab`iby' "Total"
	}


	* constants for displaying results
	* --------------------------------

	if "`by'" != "" {
		if substr("`bytype'",1,3) != "str" {
			local lv : value label `by'
			if "`lv'" != "" {
				local lg : label (`by') maxlength
				local byw = min(`labelwidth',`lg')
			}
			else {
				local byw 8
			}
		}
		else {
			local byw=min(real(substr("`bytype'",4,.)),`labelwidth')
			local bytype str
		}
		capture local for : format `by'
		capture local if_date_for = substr("`for'", index("`for'", "%"), index("`for'", "d"))
		capture local if_time_for = substr("`for'", index("`for'", "%"), index("`for'", "t"))
		if "`if_date_for'" == "%d" | "`if_time_for'" == "%t" {
			if "`if_date_for'" == "%d" {
				local has_M = index("`for'", "M")
				local has_L = index("`for'", "L")
				if `has_M' > 0 | `has_L' > 0 {
					local byw = 18
				}
				else {
					local byw = 11
				}
			}
			else {
				local byw = 9
			}
		}
		else {
			local byw = max(length("`by'"), `byw')
		}
	}
	else {
		local byw 8
	}

	* number of chars in display format
	local ndigit  9
	local colwidth = `ndigit'+1

	if "`incol'" == "statistics" {
		local lleft = (1 + `byw')*("`by'"!="") + ///
				(`varwidth'+1)*("`descr'"!="")
	}
	else {
		local lleft = (1 + `byw')*("`by'"!="") + (8+1)*("`descr'"!="")
	}
	local cbar  = `lleft' + 1

	local lsize = c(linesize)
	* number of non-label elements in the row of a block
	local neblock = int((`lsize' - `cbar')/10)
	* number of blocks if stats horizontal
	local nsblock  = 1 + int((`nstats'-1)/`neblock')
	* number of blocks if variables horizontal
	local nvblock  = 1 + int((`nvars'-1)/`neblock')

	if "`descr'" != "" & "`by'" != "" {
		local byalign lalign
	}
	else {
		local byalign ralign
	}

	* display results
	* ---------------

	if "`incol'" == "statistics" {

		* display the results: horizontal = statistics (block wise)

		if "`descr'" == "" {
			di as txt _n `"Summary for variables: `varlist'"'
			if "`by'" != "" {
				local bylabel : var label `by'
				if `"`bylabel'"' != "" {
					local bylabel `"(`bylabel')"'
				}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -