-
Notifications
You must be signed in to change notification settings - Fork 1
/
stbl.ado
2387 lines (2233 loc) · 83.8 KB
/
stbl.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// stbl.ado (was super_table.do)
/*
This program is designed to generate Excel tables of various data.
It is basically used for descriptive statistics. The stbl command, which
is implemented below, calls super_table with a correctly formatted request.
The problem with it is, it's a bit murky what it actually DOES. It needs some
updating.
Types of statistics offered: mean, proportion, count, obs, total, mean, change.
Each type of stat does something slightly different. Plus, there's a flip option which rotates
the table. Various stats actually do slightly different things in a flipped table.
It's a little bit of a mess, actually.
13AUG09: I think I've figured out a way to actually allow for options within the list of stats,
formats and variables. Basically you could specifiy options within stats: (mean, standarderror),
would be an example. I think you could nest the parenthesis to allow for sets if you wanted.
(proportion mean, standarderror) would have the standarderror option apply to both stats, while
(proportion (mean, standarderror)) would have it only apply to mean.
Basically, options specified in this way would actually overwrite other options. The stbl program
could actually parse that text, and then pass it along to super_table in a simlar way to what I do
for variables stats and formats. You additionally send a list of "options" for each variable.
When the program loops through those variables, you would check to see if there were any options
specified, and if so, they override the other options specified at the end of the command.
I think that you could double nest options as well. I had an example of that in my brain, but I
can't think of it now.
Actually, this syntax will make the "round" statistic useless. Basically, round will apply to all
the results if you specify it at the end. Instead of round, you would type (mean, round(10)).
I wonder if I could set up something where you could make all instances of mean in the command
be rouned by specifying one option command. Something like: (mean, round(10) persist) -- so
subsequent (mean)'s will be automatically rounded. I'd want to implement this syntax before making
the move.
Also, is it the best setup to actually use Stata to parse this text. Perhaps I want to look at
another language for it.
18AUG09: I fixed my problems with the generation of the individual tables, but I think that I
may have caused a problem. Currently, each individual table is named after the variable that
it is using: some are over variables, and some are varlist variables. But, it will make sense
to include the same variable more than once in the list... after all you might use different
stats for the table -- or you could use the same stat with different options (I'm thinking
ahead here.)
Anyway, to solve this I will do two things. Add in the stats for the varlist variables, since
that will make it easier to find the table that you want. Also, I could write some code to
pick a filename if it already exists. Actually, I should write that up as an ado file.
*/
/*
Program name: stbl
Function: This program is the text parser. It takes the values passed to the "anything" macro and
turns them into lists of variable names, statistic names and formats. It then passes those lists
to super_table, which actually generates tables based on them.
*/
capture program drop stbl
program stbl
syntax anything using [if] [in] [pweight], [over(varlist) SUBGRoup(varlist) ALLSGroup] [FORMat(string)] [ROUND(passthru) STANDARDERRor STDERRBRacket STDERRPOSition(passthru) APPend REPLace FLIP noHEADer MISSing ALLCol noALLRow] [REGEX(passthru) RXREPLACEment(passthru)] [VERBose] [notes(string)] [title(string)] [rowtitle(passthru)] [SHOWTOTal] [ATOMize] [XOVER(passthru) XOVERAll] [OPENFile] [BACKup INDIVidualfiles]
marksample touse, novarlist
tempvar all
// global optdelimit " >>> "
global optdelimit ">"
display _newline as text "Running " as result "\$Id: stbl.ado 1399 2010-01-28 14:24:21Z mesa $"
generate `all' = 1
label define all 1 "All", modify
label values `all' all
/*
Backup and replace the file.
*/
grabfilename `using'
local filepath = r(filepath)
local dirpath = r(dirpath)
local backupfile = `"`dirpath'.bak"' /* " */
uniquefilename using "`backupfile'"
local backupfile = r(filename)
if "`replace'"=="replace" {
capture copy `filepath' `backupfile'
capture erase `filepath'
}
/*
Do the recursive atomize function for flipped tables.
*/
if "`atomize'"=="atomize" & "`flip'"=="flip" {
/*
Basically, the first table that we generate will follow the rules specified
in the overall command. Subsequent commands should be replaced. My strategy
for doing this is to define a local macro of my own which will contain the
contents of both append and replace macros that have been passed to the
original stbl command. After it is run once, I will change it to append
for the rest of the loop.
I'm not sure what to do about headers in this case. I could pass noheaders to
subsequent tables, but I don't think I will.
*/
if "`verbose'"=="verbose" {
display as text "Flipped atomized table. Running stbl recursively."
}
createdir `using'
local subtabledir "`r(dirname)'"
local originalfile "`r(filename)'"
local atomized_filelist ""
// local myappend_replace "`append' `replace'"
local myappend_replace "replace" // replace for each separate file.
foreach over_i of local over {
uniquefilename using "`subtabledir'O`over_i'.txt"
local tableoutputfilename "`r(filename)'"
stbl `anything' using "`tableoutputfilename'" /* " */`if' `in' [`weight'`exp'], over(`over_i') subgroup(`subgroup') `allsgroup' format(`format') `round' `standarderror' `stderrbracket' `stderrposition' `myappend_replace' `flip' `noheader' `missing' `allcol' `noallrow' `regex' `rxreplacement' `verbose' notes(`notes') title(`title') `showtotal'
local atomized_filelist `"`atomized_filelist' "`tableoutputfilename'""' /* " */
// local myappend_replace "append"
}
/*
Now we merge all the files together. We use the append and replace macros to
determine what files to include.
*/
local replacex "`replace'"
if "`append'"=="append" {
capture confirm file `originalfile'
if !_rc {
local firstfile "`originalfile'"
local replacex "replace" // We will need to replace the original file.
}
}
else {
local firstfile ""
}
inccat `firstfile' `atomized_filelist', to(`originalfile') replace
uniquefilename using "`subtabledir'filelist.txt"
local filelistfilename "`r(filename)'"
file open myfile using "`filelistfilename'", write all replace
foreach l of local atomized_filelist {
file write myfile `"`l'"' /* " */ _newline
}
file close myfile
}
else {
if "`verbose'"=="verbose" {
display _newline as text "Original List: " as result `"`anything'"' /* " */ as text "."
}
local myvarlist ""
local mystatlist ""
local myformatlist ""
local myoptionlist ""
local nextstat ""
local nextformat ""
local statgroup ""
local formatgroup ""
local open_para = 0
local option_flag = 0
local STAT_SET = 0
local stderrleft ""
local stderrright ""
if "`stderrbracket'"=="stderrbracket" {
local stderrleft "[ "
local stderrright " ]"
}
if "`format'" == "" {
local format "%12.1f"
}
local cur_stat "mean"
local cur_format "`format'"
// local option_list "default"
// Level 1 - while loop open
while `"`anything'"' != `""' /* " */ {
tokenize `"`anything'"' /* " */
local cur_var `1'
macro shift
local anything `"`*'"' /* " */
display _newline as text "Processing " as result `"`cur_var'"' /* " */ as text "."
display as text "Remaining tokens: " as result `"`anything'"' /* " */ as text "."
display as text "open_para = " as result "`open_para'" as text " option_flag = " as result "`option_flag'" as text "."
display _newline as text "Stat Group: " as result "`statgroup'" as text "."
display as text "Format Group: " as result "`formatgroup'" as text "."
display as text "Individual Option List: " as result `"`option_list'"' /* " */ as text "."
display _newline as text "Varlist (so far): " as result "`myvarlist'" as text "."
display as text "Statlist (so far): " as result "`mystatlist'" as text "."
display as text "Formatlist (so far): " as result "`myformatlist'" as text "."
display as text "Optionlist (so far): " as result `"`myoptionlist'"' /* " */ as text "."
/*
Single Stat Commands
First is with options, the second is without options.
*/
// Level 2 - if statement, stat with option
if regexm(`"`cur_var'"' /* " */,"\([ ]*([a-z]+[0-9]*)[ ]*,[ ]*([a-z]+.*)[ ]*\)$") {
// Options were provided
display as text "Single Stat with Options"
local cur_stat = regexs(1)
local option_list = regexs(2)
local option_list = `"`"`option_list'"'"'
local STAT_SET = 1
// Level 2 closing
}
// Level 2 - else if statment, not stat with options
else {
// Level 3 - if statement, stat without options
if regexm(`"`cur_var'"' /* " */,"\([ ]*([a-z]+[0-9]*)[ ]*\)$") { // single stat command
display as text "Single Stat without options"
local cur_stat = regexs(1)
local option_list "default"
local STAT_SET = 1
// Level 3 closing
}
// Level 3 - else if statement, not stat without opiton
else {
// Not a stat command, don't process the set thingy.
local STAT_SET = 0
// Level 3 closing
}
// Level 2 closing
}
// Dealing with the "set" stat (which is a predefined block of stats
// Level 2 - if statement -- stat was set this iteration
if `STAT_SET' == 1 {
// Level 3 -- is verbose on?
if "`verbose'"=="verbose" {
display as text "The current stat is " as result "`cur_stat'" as text "."
display as text "The current option list is " as result "`option_list'" as text "."
// Level 3 closing
}
// Level 3 -- if statement - was the cur_stat equal to set?
if "`cur_stat'"=="set" {
display as text "Set of stats invoked."
local statgroup "count mean min max pctile25 pctile50 pctile75 iqr"
local formatgroup = regexr("`cur_format'","[.][0-9]+",".0")
// Level 4 -- forvalues loop over the different stats
forvalues fcounter = 2/8 {
// local myvarlist "`myvarlist' `cur_var'"
// local myformatlist "`myformatlist' `cur_format'"
local formatgroup "`formatgroup' `cur_format'"
// Level 4 closing
}
// local mystatlist "`mystatlist' count mean min max pctile25 pctile50 pctile75 iqr"
// Level 3 Closing
}
// Level 3 -- else if statement, stat is not equal to 'set'
else {
// Single stat variable -- set groups to empty
local statgroup ""
local formatgroup ""
// Level 3 closing
}
// Level 2 closing
}
// Level 2 - else statement -- stat not set this iteration
else {
// Level 3 - if statement -- this is a format command
if regexm(`"`cur_var'"' /* " */,"^(%.+)") { // format command
display as text "Formatting Command"
local cur_format = regexs(1)
// Level 3 closing
}
// Level 3 - else statement -- not a formatting command
else {
// Level 4 - if statement -- item matches the opening for a group of stats
if regexm(`"`cur_var'"' /* " */,"\([ ]*([a-z%][a-z0-9.,]*)") { // opening para for set of stats
display "Opening Parenthesis (set of stats)"
local cur_var = regexs(1) // strip the parenthesis
// Level 5 -- check if paragraph already open
if `open_para'==1 {
// para already open
error 198
// level 5 closing
}
// level 5 -- paragraph not already open
else {
local open_para = 1 // indicate open parentheses
/*
Clear options!
*/
local option_list `""'
// level 6 -- if statement -- formatting command
if regexm(`"`cur_var'"' /* " */,"(%.+)") {
display as text "Formating within set of stats"
// the next time we get a stat -- use this as the format
local cur_format "`cur_var'"
// level 6 closing
}
// level 5 -- not a formatting command -- else statement
else {
display "Stat in the middle of a group of stats!"
// check for commas, which will set the option flag.
// level 6 -- if statement - includes a comma
if regexm(`"`cur_var'"' /* " */, "(.*),") {
display as text "stat with comma. Setting option flag."
// local option_list `"`option_list' ""' // Opening Quote Mark
/*
This section should deal properly with the
case where there is just a comma on its own.
It just drops an empty string into cur_var,
and sets the option flag.
*/
local cur_var = regexs(1) // strip the comma
local option_flag = 1
// level 6 closing
}
// add the stat to the statgroup macro
local statgroup "`cur_var'"
local formatgroup "`cur_format'"
// display _newline as text "Statgroup: " as result "`statgroup'"
// display as text "Format Group: " as result "`formatgroup'"
// level 5 closing
}
// level 4 closing
}
// level 3 closing
}
// level 4 -- else statement -- item does not match para opening
else {
// level 5 -- if statement -- option flag is set
if `option_flag'==1 {
display "Option flag on, adding options to group of stats"
/*
If the option flag is on, we are now adding
options to the option list.
If the options contain (, then we have to make sure
we don't close the thingy.
*/
// level 6 -- if statement -- contains option argument in ()?
if regexm(`"`cur_var'"' /* " */, "(.+\(.*\))") {
display as text "Option with argument detected."
local temp_cur_var = regexs(1)
/*
Was there a closing parenthsis at the end
*/
// level 7 - if statement -- two closing paras detected
if regexm(`"`cur_var'"' /* " */, "\)[ ]*\)") {
display as text "Two closing parenthsis detected because of option"
local open_para = 0
local option_flag = 0
// level 8 - if statement - trimming an extra )
if regexm(`"`cur_var'"' /* " */, "(.+\(*.\))\)") {
local temp_cur_var = regexs(1)
// level 8 closing
}
// level 7 closing
}
local cur_var `"`temp_cur_var'"' /* " */
// level 6 closing
}
// level 6 -- else statement -- does not contaion option argument
else {
// level 7 -- if statement -- closing para found
if regexm(`"`cur_var'"' /* " */, "(.+)[ ]*\)$") {
display as text "Closing parenthsis found with option"
/*
If there is a closing parenthsis, we
are done.
*/
local cur_var = regexs(1)
// level 8 -- para not open
if `open_para'==0 {
// no para open
error 198
// level 8 closing
}
// level 8 -- para is open
else {
local open_para = 0
local option_flag = 0
// local option_list `"`option_list'""' // Closing Quote Mark
// level 8 closing
}
// level 7 closing
}
// level 6 closing
}
local option_list `"`option_list' `cur_var'"' /* " */
// level 5 closing
}
// level 5 -- else statement -- option flag not set
else {
// level 6 -- if statement -- matches closing para
if regexm(`"`cur_var'"' /* " */,"([a-z][a-z0-9]*)[ ]*\)$") { // closing para for set of stats
display "Closing parenthsis for group of stats with no options"
local cur_var = regexs(1) // strip the para
// level 7 -- if statement -- no para open
if `open_para'==0 {
// no para open
error 198
// closing level 7
}
// level 7 -- else statement -- para is open
else {
local open_para = 0 // close the para
local option_flag = 0 // no more options either
local statgroup "`statgroup' `cur_var'"
local formatgroup "`formatgroup' `cur_format'"
// level 7 closing
}
// level 6 closing
}
// level 6 -- else statement -- doesn't match closing para
else {
// level 7 -- if statement -- para is open
if `open_para'==1 { // Open paraenthesis -- not a formatting code
display "Open parenthesis, not adding options (yet)"
/*
If there is an open parenthesis, we have to check to
see if there are options involved
*/
// check for commas, which will set the option flag.
// level 8 -- if statement -- comma check
if regexm(`"`cur_var'"' /* " */, "(.*),") {
display as text "Comma found at the end. Setting option flag"
// local option_list `"`option_list' ""' // Opening Quote Mark
/*
This section should deal properly with the
case where there is just a comma on its own.
It just drops an empty string into cur_var,
and sets the option flag.
*/
local cur_var = regexs(1) // strip the comma
local option_flag = 1
// level 8 closing
}
// level 8 -- else statement -- no comma
else {
display as text "No comma found at the end"
/*
If the comma is at the beginning of the
thingy, we basically have an option
in cur_var
*/
// level 9 -- if statement -- comma at beginning
if regexm(`"`cur_var'"' /* " */, ",(.*)") {
display as text "Comma found at the beginning"
local option_flag = 1
// local option_list `"`option_list' ""' // Opening Quote Mark
local cur_var = ""
local option_list = regex(1)
// level 9 closing
}
// level 8 closing
}
/*
If we just have a comma on it's on, all we
want it to do is set the option flag, and
not add anything to the statgroup or
formatgroup macros.
*/
// level 8 -- if statement -- cur var not empty
if `"`cur_var'"' /* " */ !=`""' {
local statgroup "`statgroup' `cur_var'"
local formatgroup "`formatgroup' `cur_format'"
// level 8 closing
}
// level 8 -- if statement -- is verbose on?
if "`verbose'"=="verbose" {
display _newline as text "Statgroup: " as result "`statgroup'"
display as text "Format Group: " as result "`formatgroup'"
// level 8 closing
}
// level 7 closing
}
// level 7 -- else statement -- para is not open
else { // if it's a variable
display "Variable or list of variables"
/*
Check to see if it's actually a varlist.
*/
unab expandlist : `cur_var'
// level 8 -- foreach statement -- loop through varlist
foreach indiv_var of varlist `expandlist' {
// level 9 -- if statement -- group not defined
if "`statgroup'" == "" { // no group/set of stats defined
local myvarlist "`myvarlist' `indiv_var'"
local mystatlist "`mystatlist' `cur_stat'"
local myformatlist "`myformatlist' `cur_format'"
local myoptionlist `"`myoptionlist' `"`option_list'${optdelimit}"'"' /* " */
// level 9 closing
}
// level 9 -- else statement -- group defined
else {
// level 10 - foreach statement -- loop through statgroup
foreach i of local statgroup {
local myvarlist "`myvarlist' `indiv_var'"
local myoptionlist `"`myoptionlist' `"`option_list'${optdelimit}"'"' /* " */
// level 10 closing
}
local mystatlist "`mystatlist' `statgroup'"
local myformatlist "`myformatlist' `formatgroup'"
// level 9 closing
}
// level 8 closing
}
// level 7 closing
}
// level 6 closing
}
// level 5 closing
}
// level 4 closing
}
// level 3 closing
}
// level 2 closing
}
// level 1 closing
}
if "`verbose'"=="verbose" {
display _newline as text "Submiting to super_table program:" _newline _newline
display as text "Variables: " as result "`myvarlist'" as text "."
display as text "Stats: " as result "`mystatlist'" as text "."
display as text "Formats: " as result "`myformatlist'" as text "."
display as text "Options: " as result `"`myoptionlist'"' /* " */ as text "."
}
// file open myfile `using', write all `append' `replace'
// if "`title'"!="" {
// file write myfile "`title'" _newline _newline
// }
// file close myfile
/*
if "`flip'"=="flip" {
// the xover command is not used with flipped tables
local xover ""
}
*/
super_table `myvarlist' `using' if `touse' [`weight'`exp'], statlist( `mystatlist' ) formatlist( `myformatlist' ) optionlist( `"`myoptionlist'"' /* " */ ) over( `over' ) subgroup( `subgroup' ) `allsgroup' append `flip' `header' `missing' `allcol' `allrow' title("`title'") notes("`notes'") `atomize' `xover' `xoverall' `rowtitle' stderrleft("`stderrleft'") stderrright("`stderrright'") `stderrposition' `showtotal'
}
if "`backup'"=="" {
capture erase `backupfile'
}
macshellout `using', `openfile'
end
/*
Program Name: super_table
Function: This was the original program, and I added stbl as an interface to this program.
Note: If we do end up passing a list of options to this program, to allow for custom options
per variable, we need to actually have the options be surrounded by double quotes. Several
of our options take strings.
*/
capture program drop super_table
program super_table
syntax varlist using [if] [in] [pweight], STATlist(string) FORMatlist(string) [OPTIONLIST(string)] [over(varlist) SUBGRoup(varlist) ALLSGroup] [ROUND(passthru) STANDARDERRor STDERRPOSition(string) APPend REPLace FLIP noHEADer MISSing ALLCol noALLRow] [REGEX(passthru) RXREPLACEment(passthru)] [title(string) rowtitle(string) notes(string)] [SHOWTOTal] [ATOMize] [XOVER(passthru) XOVERAll] [STDERRLEFT(passthru) STDERRRIGHT(passthru)]
marksample touse, novarlist
tempvar all
generate `all' = 1
label define all 1 "All", modify
label values `all' all
local num_varlist : word count `varlist'
local num_statlist : word count `statlist'
local num_formatlist : word count `formatlist'
// local num_optionlist : word count `optionlist'
local match `optionlist'
local num_optionlist = 0
while regexm(`"`match'"' /* " */, "(.*)${optdelimit}") {
local ++num_optionlist
local match = regexs(1)
}
if `num_varlist' != `num_statlist' {
display as error "Variables and Statistics Requested Don't Match!!"
exit
}
if `num_varlist' != `num_formatlist' {
display as error "Variables and Formats Don't match!!"
exit
}
display as text "`num_varlist' variables: " as result "`varlist'" as text "."
display as text "`num_statlist' stats: " as result "`statlist'" as text "."
display as text "`num_formatlist' formats: " as result "`formatlist'" as text "."
display as text "`num_optionlist' options: " as result `"`optionlist'"' /* " */ as text "."
if "`flip'"=="flip" {
if "`stderrposition'" == "" {
local stderrposition "below"
}
if "`over'" == "" {
local over "`all'"
}
else {
if "`allcol'"=="allcol" {
local over "`all' `over'"
}
}
local headerstats ""
foreach i of local over {
local headerstats "`headerstats' proportion"
}
createdir `using'
local subtabledir "`r(dirname)'"
local originalfile "`r(filename)'"
local flipped_filelist ""
if "`header'"=="" {
// We never pass showtotal to big_header when the table is flipped.
// big_header `over' `using' if `touse' [`weight'`exp'], statlist(`headerstats') `standarderror' `append' `replace' subgroup(`subgroup') `allsgroup'
// local myst "showtotal"
// local fixed_optionlist : list optionlist - myst
// local fixed_optionlist = regexr(`"`optionlist'"' /* " */, "showtotal", "")
uniquefilename using "`subtabledir'V_HEADER.txt"
local tableoutputfilename "`r(filename)'"
if "`title'" != "" {
file open myfile using "`tableoutputfilename'", write all replace
file write myfile "`title'" _newline
file close myfile
}
big_header `over' using "`tableoutputfilename'" if `touse' [`weight'`exp'], statlist(`headerstats') `standarderror' stderrposition("`stderrposition'") append subgroup(`subgroup') `allsgroup' optionlist( `"`optionlist'"' /* " */) `xover' `xoverall'
local flipped_filelist `"`flipped_filelist' "`tableoutputfilename'""' /* " */
}
else {
if "`title'" != "" {
uniquefilename using "`subtabledir'V_TITLE.txt"
local tableoutputfilename "`r(filename)'"
file open myfile using "`tableoutputfilename'", write all append
file write myfile "`title'" _newline
file close myfile
local flipped_filelist `"`flipped_filelist' "`tableoutputfilename'""' /* " */
}
}
/*
For flipped tables, this is the central loop that the program goes through. It will
actualy generate all the different tables that were originally submitted to stbl.
For the purposes of breaking this up into separate files -- each loop through this
could be considered it's own table.
*/
tokenize `"`optionlist'"' /* " */, parse("${optdelimit}")
forvalues i = 1/`num_varlist' {
local myvar : word `i' of `varlist'
local mystat : word `i' of `statlist'
local myformat : word `i' of `formatlist'
// local myoptionlist : word `i' of `optionlist'
// If default is specified, pass a blank myoptionlist
// if `"`myoptionlist'"' /* " */ == `"default"' {
// local myoptionlist ""
// }
local myoptionlist ``i''
local catlbl : variable label `myvar'
/*
Ensure we have a unique filename for the table.
*/
uniquefilename using "`subtabledir'V`myvar'_`mystat'.txt"
local tableoutputfilename "`r(filename)'"
// file open myfile `using', write text append all
file open myfile using "`tableoutputfilename'", write text replace all
file write myfile _newline "`catlbl' (`myvar')"
if "`mystat'"=="proportion" | "`mystat'"=="obs" {
file write myfile " `mystat'" _newline
}
file close myfile
// big_row `myvar' `using' if `touse' [`weight'`exp'], statlist(`mystat') formatlist(`myformat') name("`catlbl'") `round' `standarderror' over(`over') subgroup(`subgroup') `allsgroup' `missing' `regex' `rxreplacement' flip `showtotal'
if "`rowtitle'"=="" {
big_row `myvar' using "`tableoutputfilename'" if `touse' [`weight'`exp'], statlist(`mystat') formatlist(`myformat') name("`catlbl'") `round' `standarderror' over(`over') subgroup(`subgroup') `allsgroup' `missing' `regex' `rxreplacement' flip append `showtotal' optionlist( `"`myoptionlist'"' /* " */ ) stderrposition("`stderrposition'") `stderrleft' `stderrright' `xover' `xoverall'
}
else {
big_row `myvar' using "`tableoutputfilename'" if `touse' [`weight'`exp'], statlist(`mystat') formatlist(`myformat') name(`rowtitle') name2("`catlbl'") `round' `standarderror' over(`over') subgroup(`subgroup') `allsgroup' `missing' `regex' `rxreplacement' flip append `showtotal' optionlist( `"`myoptionlist'"' /* " */ ) stderrposition("`stderrposition'") `stderrleft' `stderrright' `xover' `xoverall'
}
local flipped_filelist `"`flipped_filelist' "`tableoutputfilename'""' /* " */
}
local firstfile ""
capture confirm file `originalfile'
if _rc==0 & "`append'"=="append" {
local firstfile "`originalfile'"
}
inccat `firstfile' `flipped_filelist', to(`originalfile') replace
uniquefilename using "`subtabledir'filelist.txt"
local filelistfilename "`r(filename)'"
file open myfile using "`filelistfilename'", write all replace
foreach l of local flipped_filelist {
file write myfile `"`l'"' /* " */ _newline
}
file close myfile
}
else {
/*
Non Flipped tables -- to generate separate tables, we can't really use the stbl
command recursively. Instead, we probably have to run this command recursively.
*/
if "`stderrposition'"=="" {
local stderrposition "right"
}
if "`atomize'"=="atomize" {
// Atomized non flipped table -- loop through the varlist, statlist and formatlist.
createdir `using'
local subtabledir "`r(dirname)'"
local originalfile "`r(filename)'"
local nonflipped_atom_filelist ""
// local myappend_replace "`append' `replace'"
tokenize `"`optionlist'"' /* " */, parse("${optdelimit}")
forvalues ai = 1/`num_varlist' {
local a_myvar : word `ai' of `varlist'
local a_mystat : word `ai' of `statlist'
local a_myformat : word `ai' of `formatlist'
// local a_myoption : word `ai' of `optionlist'
local a_myoption ``ai''
uniquefilename using "`subtabledir'V`a_myvar'_`a_mystat'.txt"
local tableoutputfilename "`r(filename)'"
// super_table `a_myvar' `using' `if' `in' [`weight'`exp'], over(`over') subgroup(`subgroup') `allsgroup' statlist(`a_mystat') formatlist(`a_myformat') `round' `standarderror' `myappend_replace' `noheader' `missing' `allcol' `noallrow' `regex' `rxreplacement' notes(`notes') `showtotal'
super_table `a_myvar' using "`tableoutputfilename'" `if' `in' [`weight'`exp'], over(`over') subgroup(`subgroup') `allsgroup' statlist(`a_mystat') formatlist(`a_myformat') optionlist( `"`a_myoption'"' /* " */ ) replace `noheader' `missing' `allcol' `noallrow' rowtitle(`rowtitle') notes(`notes') `xover' `xoverall' `stderrleft' `stderrright' stderrposition("`stderrposition'")
// local myappend_replace "append"
local nonflipped_atom_filelist `"`nonflipped_atom_filelist' "`tableoutputfilename'""' /* " */
}
local firstfile ""
capture confirm file `originalfile'
if _rc==0 & "`append'"=="append" {
local firstfile "`originalfile'"
}
inccat `firstfile' `nonflipped_atom_filelist', to(`originalfile') replace
}
else {
// Not atomized
/*
To split up these tables into their individual components, we basically need to create
one file per over and subgroup variable.
*/
createdir `using'
local subtabledir "`r(dirname)'"
local originalfile "`r(filename)'"
local nonflipped_nonatomized_filelist ""
if "`header'"=="" {
/*
We pass the showtotal option to the header when creating non-flipped tables.
The header changes for those, but not for others.
*/
// big_header `varlist' `using' if `touse' [`weight'`exp'], statlist(`statlist') `standarderror' `append' `replace' `showtotal'
uniquefilename using "`subtabledir'O_HEADER.txt"
local tableoutputfilename "`r(filename)'"
if "`title'" != "" {
file open myfile using "`tableoutputfilename'", write all replace
file write myfile "`title'" _newline /* _newline */
file close myfile
}
big_header `varlist' using "`tableoutputfilename'" if `touse' [`weight'`exp'], statlist(`statlist') append `xover' `xoverall' optionlist( `"`optionlist'"' /* " */) stderrposition("`stderrposition'")
local nonflipped_nonatomized_filelist `"`nonflipped_nonatomized_filelist' "`tableoutputfilename'""' /* " */
}
else {
if "`title'" != "" {
uniquefilename using "`subtabledir'O_TITLE.txt"
local tableoutputfilename "`r(filename)'"
file open myfile using "`tableoutputfilename'", write all append
file write myfile "`title'" _newline /* _newline */
file close myfile
local nonflipped_nonatomized_filelist `"`nonflipped_nonatomized_filelist' "`tableoutputfilename'""' /* " */
}
}
if "`allrow'"=="" {
// big_row `varlist' `using' if `touse' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("All") `round' `standarderror' `missing' `regex' `rxreplacement' append `showtotal'
uniquefilename using "`subtabledir'O_ALLROW.txt"
local tableoutputfilename "`r(filename)'"
if "`rowtitle'"=="" {
big_row `varlist' using "`tableoutputfilename'" if `touse' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("All") `missing' `regex' replace `xover' `xoverall' optionlist( `"`optionlist'"' /* " */ ) `stderrleft' `stderrright' stderrposition("`stderrposition'")
}
else {
big_row `varlist' using "`tableoutputfilename'" if `touse' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`rowtitle'") name2("All") `missing' `regex' replace `xover' `xoverall' optionlist( `"`optionlist'"' /* " */ ) `stderrleft' `stderrright' stderrposition("`stderrposition'")
}
local nonflipped_nonatomized_filelist `"`nonflipped_nonatomized_filelist' "`tableoutputfilename'""' /* " */
}
if "`over'" != "" {
if "`subgroup'" == "" {
foreach i of varlist `over' {
local catlbl : variable label `i'
// file open myfile `using', write text append all
uniquefilename using "`subtabledir'O_`i'.txt"
local tableoutputfilename "`r(filename)'"
file open myfile using "`tableoutputfilename'", write text replace all
file write myfile _newline "`catlbl' (`i')" _newline
file close myfile
quietly levelsof `i' if `touse', local(mycats)
local realformat : format `i'
// display _n as text "Variable: " as result "`i'" as text " Format: " as result "`realformat'"
foreach j of local mycats {
local mycatlbl : label (`i') `j'
if substr("`realformat'",1,2) == "%t" {
local mycatlbl : display `realformat' `j'
}
// big_row `varlist' `using' if `touse' & `i'==`j' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`mycatlbl'") `round' `standarderror' `regex' `rxreplacement' append `showtotal'
if "`rowtitle'"=="" {
big_row `varlist' using "`tableoutputfilename'" if `touse' & `i'==`j' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`mycatlbl'") append `xover' `xoverall' optionlist( `"`optionlist'"' /* " */) `stderrleft' `stderrright' stderrposition("`stderrposition'")
}
else {
big_row `varlist' using "`tableoutputfilename'" if `touse' & `i'==`j' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`rowtitle'") name2("`mycatlbl'") append `xover' `xoverall' optionlist( `"`optionlist'"' /* " */) `stderrleft' `stderrright' stderrposition("`stderrposition'")
}
}
local nonflipped_nonatomized_filelist `"`nonflipped_nonatomized_filelist' "`tableoutputfilename'""' /* " */
}
}
else {
foreach i of varlist `over' {
local catlbl : variable label `i'
// file open myfile `using', write text append all
uniquefilename using "`subtabledir'O_`i'.txt"
local tableoutputfilename "`r(filename)'"
file open myfile using "`tableoutputfilename'", write text replace all
file write myfile _newline "`catlbl' (`i')" _newline
file close myfile
quietly levelsof `i' if `touse', local(mycats)
local realformat : format `i'
// display _n as text "Variable: " as result "`i'" as text " Format: " as result "`realformat'"
foreach j of local mycats {
local mycatlbl : label (`i') `j'
if substr("`realformat'",1,2) == "%t" {
local mycatlbl : display `realformat' `j'
}
if "`allsgroup'"=="allsgroup" {
// big_row `varlist' `using' if `touse' & `i'==`j' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`mycatlbl'") name2("All") `round' `standarderror' `regex' `rxreplacement' append `showtotal'
big_row `varlist' using "`tableoutputfilename'" if `touse' & `i'==`j' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`mycatlbl'") name2("All") append `xover' `xoverall' optionlist( `"`optionlist'"' /* " */) `stderrleft' `stderrright' stderrposition("`stderrposition'")
}
foreach k of varlist `subgroup' {
if "`k'" != "`i'" { // As long as they are different variables, we can subgroup!!
quietly levelsof `k' if `touse', local(mysubcats)
foreach l of local mysubcats {
local mysubcatlbl : label (`k') `l'
// big_row `varlist' `using' if `touse' & `i'==`j' & `k'==`l' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`mycatlbl'") name2("`mysubcatlbl'") `round' `regex' `rxreplacement' append `standarderror' `showtotal'
big_row `varlist' using "`tableoutputfilename'" if `touse' & `i'==`j' & `k'==`l' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`mycatlbl'") name2("`mysubcatlbl'") append `xover' `xoverall' optionlist( `"`optionlist'"' /* " */) `stderrleft' `stderrright' stderrposition("`stderrposition'")
}
}
else {
if "`allsgroup'"=="" {
// big_row `varlist' `using' if `touse' & `i'==`j' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`mycatlbl'") `round' `regex' `rxreplacement' append `standarderror' `showtotal'
big_row `varlist' using "`tableoutputfilename'" if `touse' & `i'==`j' [`weight'`exp'], statlist(`statlist') formatlist(`formatlist') name("`mycatlbl'") append `xover' `xoverall' optionlist( `"`optionlist'"' /* " */) `stderrleft' `stderrright' stderrposition("`stderrposition'")
}
}
}
}
local nonflipped_nonatomized_filelist `"`nonflipped_nonatomized_filelist' "`tableoutputfilename'""' /* " */
}
}
}
local firstfile ""
capture confirm file `originalfile'
if _rc==0 & "`append'"=="append" {
local firstfile "`originalfile'"
}
inccat `firstfile' `nonflipped_nonatomized_filelist', to(`originalfile') replace
uniquefilename using "`subtabledir'filelist.txt"
local filelistfilename "`r(filename)'"
file open myfile using "`filelistfilename'", write all replace
foreach l of local nonflipped_nonatomized_filelist {
file write myfile `"`l'"' /* " */ _newline
}
file close myfile
}
}
if "`atomize'"=="" {
// file open myfile `using', write text append all
if `"`notes'"'!=`""' /* " */ {
file open myfile `using', write text append all
file write myfile /* _newline */ `"`notes'"' /* " */ _newline
file close myfile
}
// file write myfile _newline _newline
// file close myfile
}
end
capture program drop big_header
program big_header, rclass
syntax varlist using [if] [in] [pweight], STATlist(namelist) OPTIONList(string) [APPend REPLace] [allover SUBGRoup(varlist) allsgroup] [STANDARDERRor STDERRPOSition(passthru)] [SHOWTOTal] [XOVER(varlist) XOVERAll noNAMEROW noWRITETOfile] [MISSing] [*]
marksample touse, novarlist
tempvar all
generate `all' = 1
label define all 1 "All", modify
label values `all' all
display _newline as text "Header Program"
display as text "Variables: " as result "`varlist'"
display as text "Stats: " as result "`statlist'"
display as text "Options: " as result `"`optionlist'"' /* " */
local minlines = 1
local maxlines = 4
if "`xover'"!="" {
/*
X-over command used, which will recurse the header over the variables in the varlist.
*/
local minlines = 0
local hline0 `""Xover" _tab _tab"'
local hline1 `""Variable" _tab _tab"'
local hline2 `""Label" _tab _tab"'
local hline3 `""Catagories Statistics" _tab _tab"'
local hline4 `""Subgroups" _tab _tab"'
/*
loop through the variables, then the categories of those variables.
*/
foreach xover_i of varlist `xover' {
local hline0 `"`hline0' "`xover_i': ""' /* " */
quietly levelsof `xover_i' if `touse', local(myxovercats)
local realformat : format `xover_i'
// if "`xoverall'"=="xoverall" {
// local hline0 `"`hline0' "All""' /* " */
// big_header `varlist' `using' if `touse' [`weight'`exp'], statlist(`statlist') optionlist( `"`optionlist'"' /* " */) `append' `replace' `allover' subgroup(`subgroup') `allsgroup' `standarderror' `stderrposition' `showtotal' `missing' nonamerow nowritetofile
// forvalues xover_k = 0/4 {
// local hline`xover_k' `"`hline`xover_k'' `r(l`xover_k')'"' /* " */
// }
// }
foreach xover_j of local myxovercats {
local mycatlbl : label (`xover_i') `xover_j'
if substr("`realformat'",1,2) == "%t" {
local mycatlbl : display `realformat' `j'
}
local hline0 `"`hline0' "`mycatlbl'""' /* " */
big_header `varlist' `using' if `touse' & `xover_i'==`xover_j' [`weight'`exp'], statlist(`statlist') optionlist( `"`optionlist'"' /* " */) `append' `replace' `allover' subgroup(`subgroup') `allsgroup' `standarderror' `stderrposition' `showtotal' `missing' nonamerow nowritetofile
forvalues xover_k = 0/4 {
local hline`xover_k' `"`hline`xover_k'' `r(l`xover_k')'"' /* " */
}
}
}
}
else {
if "`subgroup'"=="" {
local subgroup "`all'"
local maxlines = 3
}
else {
local maxlines = 4
}
local rate 0
// local hline1 "_char(9) _char(9)"
// local hline2 "_char(9) _char(9)"
// local hline3 "_char(9) _char(9)"
// local hline4 "_tab _tab"
if "`namerow'"!="nonamerow" {
local hline0 `"_tab _tab"'
local hline1 `""Variable" _tab _tab"'
local hline2 `""Label" _tab _tab"'
local hline3 `""Catagories Statistics" _tab _tab"'
local hline4 `""Subgroups" _tab _tab"'
}
local option_word_count = 1
while "`varlist'" != "" {
tokenize `varlist'
local cur_var `1'
macro shift
local varlist `*'
tokenize `statlist'
local cur_stat `1'
macro shift
local statlist `*'
// local optionlist : list clean optionlist
display _newline as text "Cleaned Option List: " as result `"`optionlist'"' /* " */
tokenize `"`optionlist'"' /* " */, parse("${optdelimit}")
// local cur_option : word `option_word_count' of `optionlist'
local cur_option ``option_word_count''
display as text "Word " as result "`option_word_count'" as text " of the optionlist is " as result `"`cur_option'"' /* " */ as text "."
// local optionlist `"`*'"' /* " */
// local optionlist `*'
local ++option_word_count
local cur_varl : variable label `cur_var'
display _newline _newline as text "Variable: " as result "`cur_var'"
display as text "Variable Label: " as result "`cur_varl'"
display as text "Stat: " as result "`cur_stat'"
display as text "Options: " as result `"`cur_option'"' /* " */
if "`cur_stat'"=="percent" {
local cur_stat "mean"
local percent "percent"
}
else {
local percent ""
}
if "`cur_stat'"=="round" {
local cur_stat "mean"
}
if "`cur_stat'"=="change" {
local cur_stat "mean"
}
// The obs stat uses the proportion header to display all the categories.
if "`cur_stat'"=="obs" {
local cur_stat "proportion"
}
// Percentile
local pctile_num ""
if substr("`cur_stat'",1,6)=="pctile" {
local pctile_num = substr("`cur_stat'",7,.)