-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcfout.ado
1497 lines (1276 loc) · 35.5 KB
/
cfout.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
*! v1 by Ryan Knight 10may2011
*! version 2.0.0 Matthew White 26aug2014
pr cfout, rclass
vers 10.1
/* ---------------------------------------------------------------------- */
/* check input for errors */
cap cfout_syntax 2 `0'
if _rc {
cap cfout_syntax 1 `0'
if !_rc {
* Do not suppress warning messages.
cfout_syntax 1 `0'
}
else {
cfout_syntax 2 `0'
/*NOTREACHED*/
}
}
* Check the ID in the master data.
loc id : list uniq id
check_id `id', data("the master data")
* Check -lower- and -upper-.
if "`lower'" != "" & "`upper'" != "" {
* cscript 18
di as err "options lower and upper are mutually exclusive"
ex 198
}
* Check -strcomp()-.
if `:length loc strcomp' ///
parse_cmd_opt strcomp, syntax(, *): `strcomp'
* Check -numcomp()-.
if `:length loc numcomp' ///
parse_cmd_opt numcomp, syntax(, *): `numcomp'
* Define `cfvars', the list of variables to compare.
loc cfvars : list uniq varlist
* Remove `id' from `cfvars'.
if "`:list cfvars & id'" == "" ///
loc warnid 0
else {
loc cfvars : list cfvars - id
loc warnid 1
}
* Parse -saving()-.
if `:length loc saving' {
tempfile propsdta
parse_saving `using', id(`id') cfvars(`cfvars') ///
propsdta(`propsdta'): `saving'
loc keepmaster `s(keepmaster)'
loc keepusing `s(keepusing)'
loc saving_args "`s(save_diffs)'"
}
* Define `numvarsm'.
qui ds `cfvars', has(t numeric)
* "m" suffix for "master": "numvarsm" for "numeric variables master."
loc numvarsm `r(varlist)'
* Define ID locals.
qui ds `id', has(t numeric)
loc idnumm `r(varlist)'
foreach var of loc id {
loc idtypes `idtypes' `:type `var''
loc idformats `idformats' `:form `var''
loc idvallabs "`idvallabs' "`:val lab `var''""
loc varlab st_varlabel(st_local("var"))
mata: st_local("idvarlabs", st_local("idvarlabs") + ///
sprintf("%f:%s", strlen(`varlab'), `varlab'))
}
if "`nopreserve'" == "" ///
preserve
keep `id' `cfvars' `keepmaster'
sort `id'
qui lab dir
loc labnames `r(names)'
tempfile tempmaster
qui sa `tempmaster', o
qui u `using', clear
* Check -id()-.
foreach var of loc id {
cap conf var `var', exact
if _rc {
* cscript 20
di as err "variable `var' not found in using data" _n ///
"(error in option {bf:id()})"
ex 111
}
}
check_id `id', data("the using data")
* Check that each ID variable is numeric in both datasets or
* string in both datasets.
qui ds `id', has(t numeric)
* "u" suffix for "using": "idnumu" for "ID numeric using."
loc idnumu `r(varlist)'
if !`:list idnumm === idnumu' {
foreach var of loc id {
if `:list var in idnumm' + `:list var in idnumu' == 1 {
* cscript 21
loc typem : word `:list posof "`var'" in id' of `idtypes'
loc typeu : type `var'
di as err "option id(): variable `var' is " ///
"`typem' in master but `typeu' in using data"
ex 106
}
}
}
/* check input for errors */
/* ---------------------------------------------------------------------- */
* Error messages stop here; warnings start.
if `warnid' ///
di as txt "note: ID variables will not be compared."
* Variables not in the using data
unab all : _all
loc varonlym : list cfvars - all
if "`varonlym'" != "" {
p
di "note: the following variables are not in the using data:"
di as res "`varonlym'
di "{p_end}"
loc cfvars : list cfvars - varonlym
loc numvarsm : list numvarsm - varonlym
}
* Return stored result.
ret loc varonlym `varonlym'
* Variables that are numeric in one dataset and string in the other
qui ds `cfvars', has(t numeric)
loc numvarsu `r(varlist)'
loc numonlym : list numvarsm - numvarsu
loc numonlyu : list numvarsu - numvarsm
loc difftype : list numonlym | numonlyu
if "`difftype'" != "" {
p
di "note: the following variables are numeric in one dataset and"
di "string in the other and will not be compared:"
di as res "`difftype'
di "{p_end}"
loc cfvars : list cfvars - difftype
loc numvarsm : list numvarsm - difftype
}
loc numvars `numvarsm'
* Return stored result.
ret loc difftype `difftype'
* Implement -nostring-.
if "`nostring'" != "" ///
loc cfvars `numvars'
* Implement -nonumeric-.
if "`nonumeric'" != "" ///
loc cfvars : list cfvars - numvars
keep `id' `cfvars' `keepusing'
sort `id'
if "`keepusing'" != "" {
tempfile tempusing
qui sa `tempusing'
}
* Use temporary variable names to prevent name conflicts with
* `cfvars' in the master data.
foreach var of loc cfvars {
tempvar cftemp
ren `var' `cftemp'
loc cftemps : list cftemps | cftemp
}
* Merge, using the value labels and ID metadata from the master data.
* Drop shared value labels, including orphans in the master.
foreach lab of loc labnames {
cap lab drop `lab'
}
* Remove ID characteristics from the using data.
foreach var of loc id {
loc chars : char `var'[]
foreach char of loc chars {
char `var'[`char']
}
}
* Merge.
tempvar merge
qui merge `id' using `tempmaster', uniq keep(`cfvars') _merge(`merge')
* Use the ID metadata from the master data.
foreach var of loc id {
gettoken format idformats : idformats
gettoken lab idvallabs : idvallabs
form `var' `format'
cap conf numeric var `var'
if !_rc ///
lab val `var' `lab'
}
mata: attach_varlabs("id", "idvarlabs")
* Observations in only one dataset
foreach data in master using {
* "ab" for "abbreviation"
loc ab = substr("`data'", 1, 1)
loc result = cond("`data'" == "master", 2, 1)
qui cou if `merge' == `result'
* Return stored result.
ret sca Nonly`ab' = r(N)
if `return(Nonly`ab')' & "`nomatch'" == "" {
di as txt "note: the following observations are only in " ///
"the `data' data:"
sort `id'
li `id' if `merge' == `result', ab(32) noo
di
}
}
qui keep if `merge' == 3
loc nmerged = _N
* Implement string comparison options.
forv i = 1/`:list sizeof cfvars' {
loc var : word `i' of `cfvars'
loc temp : word `i' of `cftemps'
cap conf str var `var'
if !_rc {
qui cfsetstr `var' `temp', ///
`lower' `upper' `nopunct' ///
strcomp(`strcomp') caller(`=_caller()')
}
}
* Parse -numcomp()-.
gettoken numcomp_cmd rest : numcomp, p(", ")
gettoken comma numcomp_opts : rest, p(", ")
if `:length loc comma' ///
mata: assert(st_local("comma") == ",")
else ///
assert !`:length loc numcomp_opts'
if !`:length loc saving' {
mata: cfout("discrep", "alldiff", "cfvars", "cftemps", ///
"numcomp_cmd", "numcomp_opts", `=_caller()', "`dropdiff'" != "")
}
else {
save_diffs, tempmaster(`tempmaster') tempusing(`tempusing') id(`id') ///
cfvars(`cfvars') cftemps(`cftemps') `saving_args' ///
numcomp_cmd(`numcomp_cmd') numcomp_opts(`numcomp_opts') ///
caller(`=_caller()') `dropdiff'
loc discrep = r(discrep)
loc alldiff `r(alldiff)'
}
* Variables different on every observation
if "`alldiff'" != "" {
p
di "note: the following variables differ on every observation" _c
if "`dropdiff'" != "" ///
di " and will not be included" _c
di ":"
di as res "`alldiff'"
}
if "`dropdiff'" != "" ///
loc cfvars : list cfvars - alldiff
* Return stored result.
ret loc alldiff `alldiff'
* Return stored results.
ret loc varlist `cfvars'
ret sca N = `nmerged' * `:list sizeof cfvars'
ret sca discrep = `discrep'
* Display summary.
display_summary `return(discrep)' `return(N)'
* Display warning messages.
if `warnid' | "`return(varonlym)'`return(difftype)'" != "" | ///
"`dropdiff'" != "" & "`return(alldiff)'" != "" {
di as txt "note: not all variables specified are included."
}
if "`nomatch'" == "" {
if return(Nonlym) {
p
di "note: not all observations were compared;"
di "there are observations only in the master data."
di "{p_end}"
}
if return(Nonlyu) {
p
di "note: not all observations were compared;"
di "there are observations only in the using data."
di "{p_end}"
}
}
* Ensure that if -saving()- and -nopreserve- are both specified,
* the differences dataset is left in memory.
if `:length loc saving' & "`nopreserve'" != "" {
* Drop value label orphans,
* which are not saved in the differences dataset.
qui lab dir
foreach lab in `r(names)' {
qui ds, has(vallab `lab')
if "`r(varlist)'" == "" ///
lab drop `lab'
}
assert !c(changed)
}
end
/* -------------------------------------------------------------------------- */
/* error message programs */
pr assert_is_opt
mata: st_local("name", (regexm(st_local("0"), "^(.*)\(\)$") ? ///
regexs(1) : st_local("0")))
cap conf name `name'
if `:list sizeof name' > 1 | _rc ///
err 198
end
pr error_overlap
syntax anything(name=overlap id=overlap), opt1(str) opt2(str) [what(str)]
* Parse `overlap'.
gettoken overlap rest : overlap
if !`:length loc overlap' | `:length loc rest' ///
err 198
* Parse -opt*()-.
forv i = 1/2 {
loc 0 "`opt`i''"
syntax anything(name=opt`i'), [SUBopt]
loc temp : subinstr loc opt`i' "(" "", cou(loc count)
if !`count' ///
loc opt`i' `opt`i''()
loc sub`i' = "`subopt'" != ""
}
if "`what'" != "" ///
di as err "`what' " _c
loc options = cond(`sub1' & `sub2', "sub", "") + "options"
di as err `"`overlap' cannot be specified to "' ///
"both `options' `opt1' and `opt2'"
if !(`sub1' & `sub2') ///
ex 198
end
pr error_saving
syntax anything(name=rc id="return code"), [SUBopt(str)]
if "`subopt'" != "" {
assert_is_opt `subopt'
di as err "invalid `subopt' suboption"
}
di as err "invalid saving() option"
ex `rc'
end
pr warn_deprecated
syntax anything(name=old), [new(str asis)]
assert_is_opt `old'
if !`:length loc new' ///
di as txt "note: option {cmd:`old'} is deprecated and will be ignored."
else {
loc 0 "`new'"
syntax anything(name=new), [SUBopt]
gettoken new rest : new
if `:length loc rest' ///
err 198
loc option = cond("`subopt'" != "", "suboption", "option")
di as txt "note: option {cmd:`old'} is deprecated; " ///
"use `option' {cmd:`new'} instead."
}
end
/* error message programs */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* parse user input */
pr cfout_syntax
gettoken version 0 : 0
* Check that `0' satisfies version `version' syntax.
if `version' == 1 {
#d ;
syntax [varlist] using,
/* main */
id(varname)
/* string comparison */
[Lower Upper NOPunct]
/* other */
[NAme(str) Format(str) ALTid(varname) replace NOString NOMATch]
;
#d cr
di as txt "note: you are using old {cmd:cfout} syntax; " ///
"see {helpb cfout} for new syntax."
if `"`name'"' == "" ///
loc name discrepancy report.csv
else ///
warn_deprecated name(), new("saving()")
if "`replace'" != "" ///
warn_deprecated replace, new("saving(,replace)", sub)
loc saving "`"`name'"', csv `replace'"
if "`format'" != "" ///
warn_deprecated format()
if "`altid'" != "" ///
warn_deprecated altid()
di as txt "note: option {cmd:dropdiff} is implied."
loc dropdiff dropdiff
}
else if `version' == 2 {
#d ;
syntax [varlist] using,
/* main */
id(varlist)
/* string comparison */
[Lower Upper NOPunct STRComp(str asis)]
/* other */
[SAving(str asis) NUMComp(str asis) NOString NONUMeric DROPDiff
NOMATch NOPreserve]
;
#d cr
}
else {
err 198
}
mata: st_local("names", invtokens(st_dir("local", "macro", "*")'))
foreach name of loc names {
c_local `name' "``name''"
}
end
pr check_id
syntax varlist, data(str)
cap isid `varlist', missok
if _rc {
* cscript 15
* "nid" for "number of IDs"
loc nid : list sizeof varlist
di as err "option id(): " plural(`nid', "variable") " `varlist' " ///
plural(`nid', "does", "do") " not uniquely identify " ///
"the observations in `data'"
ex 459
}
if c(stata_version) >= 13 {
qui ds `varlist', has(t strL)
if "`r(varlist)'" != "" {
* cscript 16
loc nothe = regexr("`data'", "^the ", "")
di as err "option id(): `nothe':"
_nostrl error : `r(varlist)'
/*NOTREACHED*/
}
}
end
* Syntax: parse_cmd_opt option_name, syntax(): command
* Parse an option named option_name that takes a command as its argument,
* checking that it matches the syntax specified to option -syntax()-.
pr parse_cmd_opt
_on_colon_parse `0'
loc 0 "`s(before)'"
loc command "`s(after)'"
syntax name(name=opt), [syntax(str)]
gettoken cmdname 0 : command, p(", ")
cap conf name `cmdname'
if `:list sizeof cmdname' > 1 | _rc {
* cscript 62
di as err "invalid command name"
di as err "(error in option {bf:`opt'()})"
ex 198
}
cap noi syntax `syntax'
if _rc {
* cscript 50
di as err "(error in option {bf:`opt'()})"
ex `=_rc'
}
end
pr notes_count, rclass
syntax [varlist]
loc N 0
foreach var of loc varlist {
loc note0 : char `var'[note0]
if "`note0'" != "" ///
loc N = max(`N', `note0')
}
ret sca N = `N'
end
pr parse_saving, sclass
_on_colon_parse `0'
loc 0 "`s(before)'"
syntax using, id(varlist) cfvars(varlist) propsdta(str)
loc 0 "`s(after)'"
loc temp `using'
cap noi syntax anything(name=fn id=filename equalok everything), ///
[Variable(name) MASterval(name) USingval(name) All(name) All2 ///
KEEPMASter(varlist) KEEPUSing(str asis) Properties(str asis) LAbval ///
csv replace]
loc using `temp'
if _rc {
error_saving `=_rc'
/*NOTREACHED*/
}
* Parse `fn'.
gettoken fn rest : fn
if `:length loc rest' {
* cscript 101
di as err "invalid filename"
error_saving 198
/*NOTREACHED*/
}
* Add a file extension to `fn' if necessary.
mata: if (pathsuffix(st_local("fn")) == "") ///
st_local("fn", st_local("fn") + ///
(st_local("csv") != "" ? ".csv" : ".dta"));;
* Check `fn' and -replace-.
cap conf new f `"`fn'"'
if ("`replace'" == "" & _rc) | ("`replace'" != "" & !inlist(_rc, 0, 602)) {
* cscript 8
cap noi conf new f `"`fn'"'
error_saving `=_rc'
/*NOTREACHED*/
}
* Check -all()- and -all-.
if "`all'" != "" & "`all2'" != "" {
* cscript 55
di as err "suboptions all() and all are mutually exclusive"
error_saving 198
/*NOTREACHED*/
}
if `:length loc properties' {
notes_count `cfvars'
loc notesN = r(N)
}
* Parse -keepusing()-.
if `:length loc keepusing' | `:length loc properties' {
preserve
qui d `using'
if r(N) ///
qui u `using' in 1, clear
else
qui u `using', clear
if `:length loc keepusing' {
cap noi unab keepusing : `keepusing'
if _rc {
* cscript 73
error_saving `=_rc', sub(keepusing())
/*NOTREACHED*/
}
}
if `:length loc properties' {
notes_count `cfvars'
loc notesN = max(`notesN', r(N))
}
restore
}
* Parse -keepmaster()- and -keepusing()-.
foreach list in keepmaster keepusing {
loc `list' : list uniq `list'
loc `list' : list `list' - id
}
* Default variable names
if "`variable'" == "" ///
loc variable Question
if "`masterval'" == "" ///
loc masterval Master
if "`usingval'" == "" ///
loc usingval Using
if "`all2'" != "" ///
loc all diff
* Parse -properties()-.
if `:length loc properties' {
parse_saving_properties, saving(`propsdta') cfvars(`cfvars') ///
variable(`variable') notes_count(`notesN'): `properties'
qui d using `propsdta', varl
loc propvars `r(varlist)'
loc propvars : list propvars - variable
}
* Check variable names.
loc properties `propvars'
loc opts variable masterval usingval all keepmaster keepusing properties
while `:list sizeof opts' {
gettoken opt1 opts : opts
foreach opt2 of loc opts {
loc overlap : list `opt1' & `opt2'
if "`overlap'" != "" {
* cscript 29
gettoken first : overlap
error_overlap `first', what(variable) ///
opt1(`opt1', sub) opt2(`opt2', sub)
error_saving 198
/*NOTREACHED*/
}
}
loc overlap : list id & `opt1'
if "`overlap'" != "" {
* cscript 29
gettoken first : overlap
error_overlap `first', what(variable) ///
opt1(id) opt2("saving(,`opt1'())", sub)
/*NOTREACHED*/
}
}
sret loc keepmaster `keepmaster'
sret loc keepusing `keepusing'
* Arguments for -save_diffs-
if `:length loc properties' ///
loc propsdta_opt propsdta(`propsdta')
loc args fn(`"`fn'"') variable(`variable') ///
masterval(`masterval') usingval(`usingval') all(`all') ///
keepmaster(`keepmaster') keepusing(`keepusing') `propsdta_opt' ///
`labval' `csv' `replace'
sret loc save_diffs "`args'"
end
pr parse_saving_properties
_on_colon_parse `0'
loc 0 "`s(before)'"
syntax, saving(str) cfvars(varlist) variable(name) notes_count(integer)
loc 0 ", `s(after)'"
cap noi syntax, [Type(name) Type2 Format(name) Format2 ///
VALLabel(name) VALLabel2 VARLabel(name) VARLabel2 ///
Char(namelist) CHARStub(name) Notes(str) NOTESStub(name)]
loc sub sub(properties())
if _rc {
error_saving `=_rc', `sub'
/*NOTREACHED*/
}
foreach opt in type format vallabel varlabel {
if "``opt''" != "" & "``opt'2'" != "" {
* cscript 83
di as err "suboptions `opt'() and `opt' are mutually exclusive"
error_saving 198, `sub'
/*NOTREACHED*/
}
* Default names
if "``opt'2'" != "" ///
loc `opt' `opt'
}
* Parse -char()- and -charstub()-.
if "`charstub'" == "" ///
loc charstub char_
loc char : list uniq char
foreach c of loc char {
loc charvar `charstub'`c'
loc charvars : list charvars | charvar
cap conf name `charvar'
if _rc {
* cscript 85
di as err "suboptions char(), charstub(): `charvar' invalid name"
error_saving `=_rc', `sub'
/*NOTREACHED*/
}
}
* Parse -notes()- and -notesstub()-.
if "`notesstub'" == "" ///
loc notesstub note
loc _all _all
if `:list _all in notes' {
if `notes_count' {
numlist "1/`notes_count'"
loc notes_all `r(numlist)'
}
while `:list _all in notes' {
loc notes : list notes - _all
}
}
if "`notes'" != "" {
cap noi numlist "`notes'", min(1) int r(>0)
if _rc {
* cscript 88
di as err "suboption notes() invalid"
error_saving `=_rc', `sub'
/*NOTREACHED*/
}
loc notes `r(numlist)'
loc notes : list uniq notes
}
loc notes : list notes | notes_all
if "`notes'" != "" {
numlist "`notes'", sort
loc notes `r(numlist)'
loc notes : list retok notes
loc notes " `notes'"
loc notevars : subinstr loc notes " " " `notesstub'", all
loc notes `notes'
loc notevars `notevars'
foreach var of loc notevars {
cap conf name `var'
if _rc {
* cscript 88
di as err "suboptions notes(), notesstub(): `var' invalid name"
error_saving `=_rc', `sub'
/*NOTREACHED*/
}
}
}
* Check variable names.
loc tempchar `char'
loc char `charvars'
loc tempnotes `notes'
loc notes `notevars'
loc opts type format vallabel varlabel char notes
while `:list sizeof opts' {
gettoken opt1 opts : opts
foreach opt2 of loc opts {
loc overlap : list `opt1' & `opt2'
if "`overlap'" != "" {
* cscript 29
gettoken first : overlap
error_overlap `first', what(variable) ///
opt1(`opt1', sub) opt2(`opt2', sub)
error_saving 198, `sub'
/*NOTREACHED*/
}
}
if `:list variable in `opt1'' {
* cscript 29
error_overlap `variable', what(variable) opt1(variable, sub) ///
opt2("properties(`opt1'())", sub)
error_saving 198
/*NOTREACHED*/
}
}
loc char `tempchar'
loc notes `tempnotes'
preserve
mata: load_props("cfvars", "variable", "type", "format", "vallabel", ///
"varlabel", "char", "charstub", "notes", "notesstub")
sort `variable'
qui sa `"`saving'"'
end
/* parse user input */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* display programs */
pr p
di as txt "{p 0 4 2}"
end
pr display_summary
args discrep N
loc line1a "Number of differences: "
loc line1b `discrep'
loc line2a "Number of values compared: "
loc line2b `N'
loc line3a "Percent differences: "
loc line3b = strofreal(100 * `discrep' / `N', "%9.3f") + "%"
loc linelen = max(strlen("`line1a'`line1b'"), ///
strlen("`line2a'`line2b'"), strlen("`line3a'`line3b'"))
loc col _col(3)
#d ;
di _n
`col' "{hline `linelen'}" _n
`col' as txt "`line1a'" as res "`line1b'" _n
`col' as txt "`line2a'" as res "`line2b'" _n
`col' as txt "`line3a'" as res "`line3b'" _n
`col' "{hline `linelen'}"
;
#d cr
end
/* display programs */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* string comparison */
pr cfsetstr
syntax varlist(min=2 max=2), caller(real) ///
[lower upper NOPUNCT strcomp(str asis)]
foreach var of loc varlist {
if "`lower'`upper'" != "" {
qui replace `var' = `lower'`upper'(`var')
}
if "`nopunct'" != "" {
foreach c in ! ? "'" {
qui replace `var' = subinstr(`var', "`c'", "", .)
}
foreach c in . , -- / ; : ( ) {
qui replace `var' = subinstr(`var', "`c'", " ", .)
}
qui replace `var' = itrim(strtrim(`var'))
}
}
if `:length loc strcomp' {
gettoken cmd opts : strcomp, p(", ")
cap noi vers `caller': `cmd' `varlist'`opts'
if _rc {
* cscript 52
di as err "(error in option {bf:strcomp()})"
ex `=_rc'
}
}
end
/* string comparison */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* save differences dataset */
pr save_diffs, rclass
#d ;
syntax,
/* main */
tempmaster(str) [tempusing(str)] id(varlist)
[cfvars(varlist) cftemps(varlist)]
/* -saving()- arguments */
fn(str) variable(name) masterval(name) usingval(name) [all(name)
keepmaster(namelist) keepusing(namelist) propsdta(str)
labval csv replace]
/* other */
caller(real) [numcomp_cmd(name) numcomp_opts(str asis) dropdiff]
;
#d cr
* Index `id'.
* "ididx" for "ID index"
tempvar ididx
gen double `ididx' = _n
qui compress `ididx'
preserve
keep `id' `ididx'
sort `ididx'
tempfile idmap
qui sa `idmap'
restore
drop `id'
#d ;
mata: cfout(
/* output */ "discrep", "alldiff",
/* comparison variables */ "cfvars", "cftemps",
/* other */
"numcomp_cmd", "numcomp_opts", `caller', "`dropdiff'" != "",
/* -id()- */ "ididx",
/* new variable names */ "variable", "masterval", "usingval", "all",
/* other */ "`labval'" != "");
#d cr
tempvar order
gen double `order' = _n
ret sca discrep = `discrep'
tempvar merge
* Merge back in the ID variables.
sort `ididx'
qui merge `ididx' using `idmap', uniqus _merge(`merge')
qui drop if `merge' == 2
drop `ididx' `merge'
* -saving(, keepmaster() keepusing())-
foreach data in master using {
if "`keep`data''" != "" {
sort `id'
qui merge `id' using `temp`data'', uniqus ///
keep(`keep`data'') _merge(`merge')
qui drop if `merge' == 2
drop `merge'
}
}
* -saving(, properties())-
if !`:length loc propsdta' ///
loc propvars `variable'
else {
qui d using `"`propsdta'"', varl
loc propvars `r(varlist)'
sort `variable'
qui merge `variable' using `"`propsdta'"', uniqus _merge(`merge')
qui drop if `merge' == 2
drop `merge'
}
* Sort so that within `id', Question remains sorted by
* the original variable order.
sort `id' `order'
drop `order'
order `id' `keepmaster' `keepusing' `propvars' `all' `masterval' `usingval'
if "`csv'" == "" {
* Remove the dataset's label and characteristics.
lab data
loc chars : char _dta[]
foreach char of loc chars {
char _dta[`char']
}
qui compress
qui sa `"`fn'"', `replace'
}
else {
qui ds, has(t numeric)
loc numvars `r(varlist)'
if "`numvars'" != "" {
foreach var of loc numvars {
lab val `var'
}
form `numvars' %24.0g
}
qui outsheet using `"`fn'"', c `replace'
}
ret loc alldiff `alldiff'
end
/* save differences dataset */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */