-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcortable_split.ado
612 lines (555 loc) · 19 KB
/
cortable_split.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
*! v1.3.2 04aug2022 PLakin
*! v1.3.1 11nov2021 PLakin
*! v1.3 07jul2020 RRaciborski
program define cortable_split, rclass
version 15.1
syntax [if] ///
[, ///
/// NEW STUFF
put(string) /// putpdf | putdocx
rowvarlist(varlist) /// rowvariable list
binvar(string) /// variable types, string so if it's empty it doesn't complain
catvar(string) ///
convar(string) ///
currow(integer 0) ///
touse(varname) /// holds the touse variable
post(integer 0) /// will be 0 or 1
handle(string) /// name of handle file for postfile
///
na(string) /// what to display when suppressed
tname(string) /// name(<handle>, replace append) name of table, used when you're not saving
///
/// options for what goes in the table
COLvars(string) /// list of variables or factor variables for columns
SUPpress(integer 5) /// when to supress cells for small size - may leave undocumented
constat(string) /// stats can be anything produced by -summ , detail-
/// OR msd (mean +- sd) ///
/// mediqr (median [iqr]) medqt (median [p25, p75]) median (synonym p50)
noHEader /// suppresses adding a first row
ADDHeader /// forces the normal header below altheader
NODENOMinator /// option to suppress denominator
TABNumber(string) /// table number, identifies data in file
PCTFoverride(string) /// percent format override for cat and binary
CONFoverride(string) /// continuous statistics format override
font(string) /// passing font through
]
*v1.3: set suppress to suppress-1 so strictly less than specified (rar, 7/7/20)
local suppress = `suppress'-1
local nodenom = ("`nodenominator'" ~= "")
************ start actual statistics here*********************
tokenize `rowvarlist'
local vi = 1
while "``vi''"!="" {
local var ``vi''
local varlab
local varlabprefix
local tab
local grcontinued = 0
local parseonsemi = 0
local bbrdr
local xbrdr
* check to see if variable has a group label set by corset or the registry
if `"`: char `var'[corsetGroupLabel]'"'!=`""' | ///
`"`: char `var'[GroupLabel]'"'!=`""' | ///
ustrpos(`"`: var label `var''"',";")>0 {
* Figure out which label to use - label set by corset overrides registry
if `"`: char `var'[corsetGroupLabel]'"'!="" {
local varlabprefix `"`: char `var'[corsetGroupLabel]'"'
}
else if `"`: char `var'[GroupLabel]'"'!="" {
local varlabprefix `"`: char `var'[GroupLabel]'"'
}
else if ustrpos("`: var label `var''",";")>0 {
psemi `var'
local varlabprefix `"`r(prefix)'"'
local parseonsemi = 1
}
* Now, figure out if we need to do anything
if `vi'>1 { // make sure this isn't the first variable
* If the previous variable had this same group label
if `"`: char ``=`vi'-1''[corsetGroupLabel]'"'=="`varlabprefix'" | ///
`"`: char ``=`vi'-1''[GroupLabel]'"'=="`varlabprefix'" | ///
ustrpos("`: var label ``=`vi'-1'''","`varlabprefix';")>0 {
* get the thing after the semicolon
if `parseonsemi'==1 {
psemi `var'
local varlab = ustrtrim(`"`r(label)'"')
}
else {
* remove group label aka "prefix" from row label
local varlab = ustrtrim(subinstr(`"`: var label `var''"', ///
`"`: char `var'[corsetGroupLabel]'"', /// thing to remove
"",1)) // replace it with nothing once
if "`varlab'"=="" {
local varlab = " "
}
}
local grcontinued = 1
local tab uchar(8195) +
}
}
* next variable has the same group label
* first check to make sure there is a next variable
if "``=`vi'+1''"!="" {
if `"`: char ``=`vi'+1''[corsetGroupLabel]'"'==`"`varlabprefix'"' | ///
`"`: char ``=`vi'+1''[GroupLabel]'"'==`"`varlabprefix'"' | ///
ustrpos(`"`: var label ``=`vi'+1'''"',`"`varlabprefix';"')>0 {
// if it's not already continued
if (`grcontinued'==0) {
if "`header'"!="noheader" {
`put' table `tname'(`currow',.), addrows(1, after) nosplit
local currow = `currow' + 1
}
local header
`put' table `tname'(`currow',.), border(bottom, nil)
`put' table `tname'(`currow',1) = ("`varlabprefix'")
}
* get the thing after the semicolon
if `parseonsemi'==1 {
psemi `var'
local varlab = ustrtrim(`"`r(label)'"')
}
else {
* remove group label aka "prefix" from row label
local varlab = ustrtrim(subinstr(`"`: var label `var''"', ///
`"`: char `var'[corsetGroupLabel]'"', /// thing to remove
"",1)) // replace it with nothing once
if "`varlab'"=="" {
local varlab = " "
}
}
local brdr border(bottom, nil)
local tab uchar(8195) +
local nforgroup
}
}
* ELSE do nothing, you're not special
}
* if you didn't get a variable label in any of the above, set it now
* and remove the varlabprefix
if "`varlab'"=="" {
local varlab : variable label `var'
local varlab = ustrtrim("`varlab'")
local varlabprefix
}
local con : list var in convar
if `con' {
// row heading for continuous variable: variable label
* if continued
if `grcontinued' {
`put' table `tname'(`currow',.), addrows(2, after) nosplit
`put' table `tname'(`currow'/`=`currow'+2',.), border(bottom, nil) border(top, nil)
local currow = `currow' + 2
`put' table `tname'(`currow',1) = (`tab' `"`varlab'"')
}
* if not continued
if `grcontinued'==0 {
if "`varlabprefix'"!="" {
local xbrdr border(bottom, nil)
}
if "`header'"!="noheader" {
`put' table `tname'(`currow',.), addrows(1, after) `xbrdr' nosplit
local currow = `currow' + 1
}
`put' table `tname'(`currow',1) = (`tab' `"`varlab'"')
}
local i = 0
foreach stat of local constat {
`put' table `tname'(`=`currow'+`i'',.), addrows(1, after) border(bottom, nil) nosplit
local i = `i' + 1
* msd = mean +-sd
if "`stat'"=="msd" {
// row subheadings for continuous variable are fixed w/ statistic names
`put' table `tname'(`=`currow'+`i'',1) = (`tab' uchar(8195) + "Mean (SD)")
}
* mediqr = Median [IQR]
else if "`stat'"=="mediqr" {
`put' table `tname'(`=`currow'+`i'',1) = (`tab' uchar(8195) + "Median [IQR]")
}
* medqt = Median [p25, p75]
else if "`stat'"=="medqt" {
`put' table `tname'(`=`currow'+`i'',1) = (`tab' uchar(8195) + "Median [p25, p75]")
}
* p50 -> Median
else if "`stat'"=="p50" {
local s = ustrupper("`stat'")
`put' table `tname'(`=`currow'+`i'',1) = (`tab' uchar(8195) + "Median")
}
* sd -> SD, iqr -> IQR
else if "`stat'"=="sd"|"`stat'"=="iqr" {
local s = ustrupper("`stat'")
`put' table `tname'(`=`currow'+`i'',1) = (`tab' uchar(8195) + "`s'")
}
else if "`stat'"=="Var" {
local s = "Variance"
`put' table `tname'(`=`currow'+`i'',1) = (`tab' uchar(8195) + "`s'")
}
else if "`stat'"!="n" {
local s = strproper("`stat'")
`put' table `tname'(`=`currow'+`i'',1) = (`tab' uchar(8195) + "`s'")
}
}
* actual stats start here
local c = 2
* note column variables are all factor variables
foreach cv of local colvars {
* we need to add to these tables, based on what was done above
local sup = 0
// calculate statistics for continuous variable
quietly summarize `var' if `touse'==1 & `cv'==1, detail
local n : display %9.0fc r(N)
local n = trim("`n'")
* list N if not part of a coninuting table
if r(N)<=`suppress' {
if ~`nodenom'{
`put' table `tname'(`currow',`c') = ("N = `n'") ///
, nformat(%9.0fc) halign(center)
}
if `post' {
post `handle' ("`var'") (-99) ("`cv'") ("n") (r(N)) ("`tabnumber'")
}
local sup = 1
}
else {
if ~`nodenom'{
`put' table `tname'(`currow',`c') = ("N = `n'") ///
, nformat(%9.0fc) halign(center)
}
if `post' {
post `handle' ("`var'") (-99) ("`cv'") ("n") (r(N)) ("`tabnumber'")
}
}
local i = 1
* now list all other stats
foreach stat of local constat {
if `sup' {
`put' table `tname'(`=`currow'+`i'',`c') = ("`na'") ///
, nformat(%9.0fc) halign(center)
}
else {
* mean, sd
if "`stat'"=="msd" {
local msd_m = strofreal(`r(mean)',"%9.2fc")
if `=abs(`r(mean)')' < 1 {
local msd_m = strofreal(`r(mean)',"%04.2f")
}
local msd_sd = strofreal(`r(sd)',"%9.2fc")
if `=abs(`r(sd)')' < 1 {
local msd_sd = strofreal(`r(sd)',"%04.2f")
}
local msd = "`msd_m'"+" (" + ///
"`msd_sd'"+ ")"
if "`confoverride'" != "" {
local msd = strofreal(`r(mean)',"`confoverride'")+" (" + ///
strofreal(`r(sd)',"`confoverride'") + ")"
}
`put' table `tname'(`=`currow'+`i'',`c') = ("`msd'"), halign(center)
if `post' {
post `handle' ("`var'") (-99) ("`cv'") ("mean") (r(mean)) ("`tabnumber'")
post `handle' ("`var'") (-99) ("`cv'") ("sd") (r(sd)) ("`tabnumber'")
}
}
* median, IQR
else if "`stat'"=="mediqr" {
tempname iqr
scalar `iqr' = `r(p75)'-`r(p25)'
local miqr_m = strofreal(`r(p50)',"%9.2fc")
if `=abs(`r(p50)')' < 1 {
local miqr_m = strofreal(`r(p50)',"%04.2f")
}
local miqr_iqr = strofreal(`iqr',"%9.2fc")
if `=abs(`iqr')' < 1 {
local miqr_iqr = strofreal(`iqr',"%04.2f")
}
local miqr = "`miqr_m'"+" ["+"`miqr_iqr'"+"]"
if "`confoverride'" != "" {
local miqr = strofreal(`r(p50)',"`confoverride'")+" ["+strofreal(`iqr',"`confoverride'")+"]"
}
`put' table `tname'(`=`currow'+`i'',`c') = ("`miqr'"), halign(center)
if `post' {
post `handle' ("`var'") (-99) ("`cv'") ("median") (r(p50)) ("`tabnumber'")
post `handle' ("`var'") (-99) ("`cv'") ("iqr") (scalar(`iqr')) ("`tabnumber'")
}
}
* median [p25 , p75]
else if "`stat'"=="medqt" {
local medqt_p50 = strofreal(`r(p50)',"%9.2fc")
if `=abs(`r(p50)')' < 1 {
local medqt_p50 = strofreal(`r(p50)',"%04.2f")
}
local medqt_p25 = strofreal(`r(p25)',"%9.2fc")
if `=abs(`r(p25)')' < 1 {
local medqt_p25 = strofreal(`r(p25)',"%04.2f")
}
local medqt_p75 = strofreal(`r(p75)',"%9.2fc")
if `=abs(`r(p75)')' < 1 {
local medqt_p75 = strofreal(`r(p75)',"%04.2f")
}
local medqt = "`medqt_p50'"+ ///
" ["+"`medqt_p25'"+", "+"`medqt_p75'"+"]"
if "`confoverride'" != "" {
local medqt = strofreal(`r(p50)',"`confoverride'")+ ///
" ["+strofreal(`r(p25)',"`confoverride'")+", "+strofreal(`r(p75)',"`confoverride'")+"]"
}
`put' table `tname'(`=`currow'+`i'',`c') = ("`medqt'"), halign(center)
if `post' {
post `handle' ("`var'") (-99) ("`cv'") ("median") (r(p50)) ("`tabnumber'")
post `handle' ("`var'") (-99) ("`cv'") ("p25") (r(p25)) ("`tabnumber'")
post `handle' ("`var'") (-99) ("`cv'") ("p75") (r(p75)) ("`tabnumber'")
}
}
* IQR
else if "`stat'"=="iqr" {
tempname iqr
scalar `iqr' = `r(p75)'-`r(p25)'
local siqr = ustrtrim(strofreal(scalar(`iqr'),"%9.2fc"))
if `=abs(`iqr')' < 1 {
local siqr = ustrtrim(strofreal(scalar(`iqr'),"%04.2f"))
}
if "`confoverride'" != "" {
local siqr = ustrtrim(strofreal(scalar(`iqr'),"`confoverride'"))
}
`put' table `tname'(`=`currow'+`i'',`c') = ("`siqr'"), halign(center)
if `post' {
post `handle' ("`var'") (-99) ("`cv'") ("iqr") (scalar(`iqr')) ("`tabnumber'")
}
}
* anything -summ , detail- produces
else {
local s = ustrtrim(strofreal(scalar(r(`stat')),"%9.2fc"))
if `=abs(scalar(r(`stat')))' < 1 {
local s = ustrtrim(strofreal(scalar(r(`stat')),"%04.2f"))
}
if "`confoverride'" != "" {
local s = ustrtrim(strofreal(scalar(r(`stat')),"`confoverride'"))
}
`put' table `tname'(`=`currow'+`i'',`c') = ("`s'"), halign(center)
if `post' {
post `handle' ("`var'") (-99) ("`cv'") ("`stat'") (r(`stat')) ("`tabnumber'")
}
}
} // of it else for non-supressed
local i = `i' + 1
} // end loop for stats
local c = `c' + 1
} // end loop for column variables
local currow = `currow' + `i' - 1
} // end of if for continuous variables
************************** not continuous
else {
local cat : list var in catvar
if `cat' {
tempname freq levs p
if `grcontinued' {
`put' table `tname'(`currow',.), addrows(1, after) nosplit
local currow = `currow' + 1
`put' table `tname'(`currow',.), border(bottom, nil) border(top, nil)
}
// add "heading" row for variable label to subtable
if "`header'"!="noheader" {
`put' table `tname'(`currow',.), addrows(1, after) nosplit
local currow = `currow' + 1
}
if ~`nodenom'{
`put' table `tname'(`currow',1) = (`tab' `"`varlab', n (%)"')
}
else {
`put' table `tname'(`currow',1) = (`tab' `"`varlab', %"')
}
if `grcontinued'|(`grcontinued'==0&`"`varlabprefix'"'!="") {
`put' table `tname'(`currow',.), border(top, nil)
}
quietly levelsof `var'
local nlevs = r(r)
local levs = r(levels)
// add row for each level forvalues i = 1/`nleves' {
`put' table `tname'(`currow',.), addrows(`nlevs', after) nosplit
`put' table `tname'(`=`currow'+1'/`=`currow'+`nlevs'',.), border(top, nil)
local mylist
local isunk 0
// this loop pushes unknown to the end of the list
local i = 1
foreach l of local levs {
// get the value of the level
local vlab: label (`var') `l'
* if label contains "Unk"
if `= strpos(proper("`vlab'"),"Unk")' {
local isunk `l'
}
else {
local mylist `mylist' `l'
}
}
if `isunk' {
local mylist `mylist' `isunk'
}
local i = 1
foreach l of local mylist {
local vlab: label (`var') `l'
local vlab = ustrtrim("`vlab'")
`put' table `tname'(`=`currow'+`i'',1) = (`tab' uchar(8195) + "`vlab'")
local i = `i' + 1
}
local c = 2
* loop through column vars
* within each column, move down the rows
foreach cvar of local colvars {
local sup = 0
local r = 1
* total
quietly count if !missing(`var')&`touse'&`cvar'==1
local cn = r(N)
if r(N)<=`suppress' {
local sup = 1
}
local n : display %9.0fc r(N)
local n = trim("`n'")
if ~`nodenom'{
`put' table `tname'(`currow',`c') = ("N = `n'"), ///
nformat(%9.0fc) halign(center)
}
foreach i of local mylist {
quietly count if `var'==`i'&`touse'&`cvar'==1
if `sup' {
`put' table `tname'(`=`currow'+`r'',`c') = ///
("`na'"), halign(center)
}
else {
if abs(100*r(N)/`cn') < 1 {
local pct = trim("`: display %04.2f 100*r(N)/`cn''")
}
else {
local pct = trim("`: display %5.1f 100*r(N)/`cn''")
}
if "`pctfoverride'" ~= "" {
local pct = trim("`: display `pctfoverride' 100*r(N)/`cn''")
}
local n = trim("`: display %10.0gc r(N)'")
if ~`nodenom'{
`put' table `tname'(`=`currow'+`r'',`c') = ///
("`n' (`pct'%)"), halign(center)
}
else {
`put' table `tname'(`=`currow'+`r'',`c') = ///
("`pct'%"), halign(center)
}
}
if `post' {
post `handle' ("`var'") (`i') ("`cvar'") ("n") (r(N)) ("`tabnumber'")
local ppct = 100*r(N)/`cn'
post `handle' ("`var'") (`i') ("`cvar'") ("percent") (`ppct') ("`tabnumber'")
}
local r = `r'+1
} // end row levels loop
local c = `c' + 1
} // end column vars loop
local currow = `currow' + `r' - 1
}
*************** binary ******************
else {
local addtocols = 0
* no prefix = no special treatment
if "`header'"!="noheader" {
`put' table `tname'(`currow',.), addrows(1,after) nosplit
local currow = `currow' + 1
}
if "`varlabprefix'"=="" {
// binary adds variable name and label of target category
if ~`nodenom' {
`put' table `tname'(`currow',1) = (`"`varlab', n (%)"')
}
else {
`put' table `tname'(`currow',1) = (`"`varlab', %"')
}
`put' table `tname'(`currow',.), addrows(1, after) border(bottom, nil) nosplit
local currow = `currow' + 1
`put' table `tname'(`currow',1) = (`tab' uchar(8195) + `"`=ustrtrim(`"`:label (`var') 1'"')'"')
}
else {
* update the group label for these
if `grcontinued'==0 {
if ~`nodenom' {
`put' table `tname'(`=`currow'-1',1) = (`"`varlabprefix', n (%)"')
}
else {
`put' table `tname'(`=`currow'-1',1) = (`"`varlabprefix', %"')
}
}
`put' table `tname'(`currow',1) = (uchar(8195) + `"`varlab'"')
`put' table `tname'(`currow',.), border(top, nil)
}
// calculate N & percent for binary variable
local col = 2
* loop through column vars
foreach cvar of local colvars {
* add the total non-missing N, for non-grouped and first of group
if `grcontinued'==0 {
quietly count if !missing(`var')&`touse'&`cvar'==1
if r(N)<=`suppress' {
local sup = 1
}
local n : display %9.0fc r(N)
local n = trim("`n'")
*local j `subt`col''
if ~`nodenom' {
`put' table `tname'(`=`currow'-1',`col') = ("N = `n'"), ///
nformat(%9.0fc) halign(center)
}
local nforgroup`col' = r(N)
}
* add to list if N varies
if `grcontinued' {
quietly count if !missing(`var')&`touse'&`cvar'==1
if r(N)!=`nforgroup`col'' {
local nvaries `nvaries' `var' (n=`=r(N)') ,
}
}
quietly summarize `var' if `touse'&`cvar'==1, meanonly
local postpct = r(mean)*100
if r(N)<=`suppress' {
`put' table `tname'(`currow',`col') = ("`na'"), halign(center)
}
else {
if abs(r(mean)*100) < 1 {
local pct : display %04.2f `=r(mean)*100'
}
else {
local pct : display %5.1f `=r(mean)*100' // was %3.1f
}
if "`pctfoverride'" ~= "" {
local pct : display `pctfoverride' `=r(mean)*100' // was %3.1f
}
local pct = trim("`pct'")
quietly count if `touse'&`var'==1& `cvar'==1
local n : display %9.0fc r(N)
local n = trim("`n'")
if ~`nodenom' {
`put' table `tname'(`currow',`col') = ("`n' (`pct'%)"), halign(center)
}
else {
`put' table `tname'(`currow',`col') = ("`pct'%"), halign(center)
}
}
local col = `col' + 1
if `post' {
post `handle' ("`var'") (1) ("`cvar'") ("n") (r(N)) ("`tabnumber'")
post `handle' ("`var'") (1) ("`cvar'") ("percent") (`postpct') ("`tabnumber'")
}
}
}
}
local header
local vi = `vi' + 1 // increment variable, adding test comment
}
forvalues i = 1/`currow' {
putdocx table `tname'(`i',.), `font'
}
return local nvaries "`nvaries'"
return scalar currow = `currow'
end
program define psemi, rclass
args var
parse `"`: var label `var''"', parse(";")
return local prefix = `"`1'"'
return local label = `"`3'"'
end