-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetab.ado
124 lines (110 loc) · 3.48 KB
/
petab.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
capture program drop petab
program petab
// svy means in a twoway table and output results to Excel sheet using putexcel
// later: add options for other statistics (SEs, CIs etc.)
// / add option of one-way tables
// x alter syntax to resemble tab x y, sum(z)
// add option for other statistics as in tab
// add option of specifing multiple variables (tabulate one after the other or all in same table...)
// / Add col/row means by default
// Consider options to remove col/row means
// Default: variable labels in col A, left sub-group labels in col B
// return cell below bottom of table
// option to suppress top header -- so that user can continue below.
// Adjust so that it works for non-labelled variables
// Requires: findtoken; labelsof
syntax varname, [top(varname)] [left(varname)] [subpop(string)] sheet(string) [cell(string)] [round(real 0)]
if `"`subpop'"' == `""' {
local sub
}
else {
local sub , subpop(`subpop')
}
if "`cell'" == "" {
local cell A1
}
local type = cond(`"`top'"' == `""', ///
cond(`"`left'"' == `""', "single", "column"), ///
cond(`"`left'"' == `""', "row", "table"))
// Overall mean
svy `sub': mean `varlist'
matrix T = r(table)
matrix T = T[1, 1]
if `"`top'"' != `""' {
labelsof `top'
local toplabels = r(labels)
local toplevels = r(values)
local topn: word count `toplevels'
// Total row
matrix TR = J(1, `topn', .)
svy `sub': mean `varlist', over(`top', nolabel)
matrix R = r(table)
local outputlevels = e(over_labels)
forvalues i = 1/`topn' {
local toplevel: word `i' of `toplevels'
findtoken `toplevel', in(`outputlevels')
matrix TR[1, `i'] = R[1, r(P)]
}
}
if `"`left'"' != `""' {
labelsof `left'
local leftlabels = r(labels)
local leftlevels = r(values)
local leftn: word count `leftlevels'
matrix TC = J(`leftn', 1, .)
// Total column
svy `sub': mean `varlist', over(`left', nolabel)
matrix R = r(table)
local outputlevels = e(over_labels)
forvalues i = 1/`leftn' {
local leftlevel: word `i' of `leftlevels'
findtoken `leftlevel', in(`outputlevels')
matrix TC[`i', 1] = R[1, r(P)]
}
}
if "`type'" == "table" {
matrix M = J(`leftn', `topn', .)
* display `"--svy `sub': mean `varlist', over(`top' `left', nolabel)--"'
// Disaggregated table
svy `sub': mean `varlist', over(`top' `left', nolabel)
local outputlevels = e(over_labels)
matrix R = r(table)
forvalues i = 1/`leftn' {
local leftlevel: word `i' of `leftlevels'
forvalues j = 1/`topn' {
local toplevel: word `j' of `toplevels'
* display `"Looking for --`toplevel' `leftlevel'-- in --`meanlabels'--"'
findtoken `toplevel' `leftlevel', in(`outputlevels')
matrix M[`i', `j'] = R[1, r(P)]
* display "Found M[`i', `j'] = R[1, `=r(P)']"
}
}
}
if "`type'" == "table" {
matrix F = M, TC \ TR, T
local colnames `"`toplabels' "Total""'
local rownames `"`leftlabels' "Total""'
}
else if "`type'" == "column" {
matrix F = TC \ T
local rownames `"`leftlabels' "Total""'
local colnames Total // Could omit or use variable label here instead
}
else if "`type'" == "row" {
matrix F = TR, T
local rownames Total // Could omit or use variable label here instead
local colnames `"`toplabels' "Total""'
}
else {
matrix F = T
local rownames Total
local colnames Total
}
if `round' {
mata: st_matrix("F", round(st_matrix("F"), `round'))
}
matrix rownames F = `rownames'
matrix colnames F = `colnames'
matrix list F
putexcel `cell'=matrix(F, names), sheet(`sheet')
end