-
Notifications
You must be signed in to change notification settings - Fork 2
/
pt_sum.ado
143 lines (116 loc) · 3.82 KB
/
pt_sum.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
capture program drop pt_sum
prog define pt_sum
syntax varlist(numeric) [if] [in] , POSTname(string) stats(namelist) ///
[ ///
over(varname) ///
over_grps(numlist) ///
overall(string) ///
order(string) ///
gap(integer 0) /// adds empty rows to baseline table after each variable
gap_end(integer 0) /// adds empty rows to baseline table after all variables
decimal(integer 1) ///
range_decimal(integer 1) /// specify number of decimal places to be used for range
med_iqr_decimal(integer 1) /// specify number of decimal places to be used for median and IQR
append_label(string) /// appends extra text to variable label
var_lab(string) /// Overides default variable label
comment(string) /// adds column to end of table with text contained in string
]
*************IF****************
if "`if'" != "" | "`in'" != "" {
preserve
keep `if' `in'
}
***************************Processing options***********************************
*Setting default for over and extracting the levels of `over'
if "`over'" != "" {
if "`over_grps'" == "" levelsof `over', local(over_grps)
if "`overall'" == "first" local over_grps overall `over_grps'
if "`overall'" == "last" local over_grps `over_grps' overall
}
if "`over'" == "" local over_grps overall
*setting default for order
if "`order'" == "" local order group_over
*Checking valid stats specified
local stats_list "N", "missing", "mean_sd", "median_iqr", "range" // list of permited stats
foreach s in `stats' {
if inlist("`s'", "`stats_list'") ==0 {
di "stats option incorrectly specified. stats must be one of: `stats_list'"
exit
}
}
*Decimal places
if `range_decimal' ==1 local range_decimal = `decimal'
if `med_iqr_decimal' ==1 local med_iqr_decimal = `decimal'
***************************Looping over variables*******************************
foreach v in `varlist' {
*Processing Variable name
local var_label `var_lab'
if "`var_lab'" == "" local var_label:variable label `v'
local var_label `var_label' `append_label' // adding user specified append to variable label
local var_label ("`var_label'")
di _newline(3) "****** `v' ******" _newline(1)
foreach i in `over_grps' {
*Implementing command
di "Group `i'"
if "`i'" == "overall" su `v', detail
if "`i'" != "overall" su `v' if `over' == `i', detail
local N_`i' = r(N)
foreach s in mean sd {
local `s'_`i' = string(r(`s'), "%12.`decimal'f")
}
foreach s in p50 p25 p75 {
local `s'_`i' = string(r(`s'), "%12.`med_iqr_decimal'f")
}
foreach s in min max{
local `s'_`i' = string(r(`s'), "%12.`range_decimal'f")
}
local mean_sd_`i' `mean_`i'' (`sd_`i'')
local median_iqr_`i' `p50_`i'' (`p25_`i''-`p75_`i'')
local range_`i' `min_`i'' - `max_`i''
if "`i'" == "overall" qui count if missing(`v') ==.
if "`i'" != "overall" qui count if missing(`v') ==. & `over' == `i'
local missing_`i' = r(N)
}
*********Posting***********
if "`order'" == "group_sum" {
local summaries ""
foreach s in `stats' {
foreach i in `over_grps' {
local summaries `summaries' ("``s'_`i''")
}
}
}
if "`order'" == "group_over" {
local summaries ""
foreach i in `over_grps' {
foreach s in `stats' {
local summaries `summaries' ("``s'_`i''")
}
}
}
*Comments
if "`comment'" != "" & "`comment'" != "no comment" local comment1 ("`comment'")
if "`comment'" == "no comment" local comment1 ("")
post `postname' `var_label' `summaries' `comment1'
*Gaps
local summaries ""
foreach i in `over_grps' {
foreach s in `stats' {
local summaries `summaries' ("")
if "`comment'" != "" local comment1 ("")
}
if `gap' > 0 {
forvalues i = 1 (1) `gap' {
post `postname' ("") `summaries' `comment1'
}
}
}
}
if `gap_end' > 0 {
forvalues i = 1 (1) `gap_end' {
post `postname' ("") `summaries' `comment1'
}
}
*************IF****************
if "`if'" != "" | "`in'" != "" restore
end