-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcfout.do
2366 lines (2155 loc) · 48.8 KB
/
cfout.do
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
* -cfout- cscript
* -version- intentionally omitted for -cscript-.
* 1 to execute profile.do after completion; 0 not to.
local profile 1
* 1 to test -readreplace- example (89); 0 not to.
local readreplace 1
/* -------------------------------------------------------------------------- */
/* initialize */
* Check the parameters.
assert inlist(`profile', 0, 1)
* Set the working directory to the cfout directory.
c cfout
cd cscript
cap log close cfout
log using cfout, name(cfout) s replace
di "`c(username)'"
di "`:environment computername'"
clear
if c(stata_version) >= 11 ///
clear matrix
clear mata
set varabbrev off
set type float
vers 10.1: set seed 335610938
set more off
cd ..
adopath ++ `"`c(pwd)'"'
adopath ++ `"`c(pwd)'/cfby"'
* Make sure to install
* -matawarn- (https://github.com/matthew-white/matawarn) and
* -compdta- (https://github.com/matthew-white/compdta),
* adding them to a system directory or your ado-path.
cd cscript
timer clear 1
timer on 1
* Preserve select globals.
loc FASTCDPATH : copy glo FASTCDPATH
cscript cfout adofile cfout
* Check that Mata issues no warning messages about the source code.
if c(stata_version) >= 13 {
matawarn cfout.ado
assert !r(warn)
cscript
}
* Restore globals.
glo FASTCDPATH : copy loc FASTCDPATH
cd tests
loc dirs : dir . dir *
foreach dir of loc dirs {
foreach pat in *.csv diff*.dta gen*.dta {
loc files : dir "`dir'" file "`pat'"
foreach file of loc files {
erase "`dir'/`file'"
}
}
* Create generated datasets.
cap conf f "`dir'/gen.do"
if !_rc {
cd "`dir'"
do gen
cd ..
}
}
/* -------------------------------------------------------------------------- */
/* basic */
* Test 1
cd 1
u firstEntry, clear
cfout region-no_good_at_all using secondEntry, id(uniqueid)
assert r(N) == 15000
assert r(discrep) == 44
loc varlist `r(varlist)'
unab unab : region-no_good_at_all
assert `:list varlist == unab'
cd ..
* Test 22
cd 22
u 1, clear
cfout gender using 2, id(id)
assert r(N) == 998
assert r(discrep) == 2
assert r(Nonlym) == 1
assert r(Nonlyu) == 1
cd ..
* Test 23
cd 23
loc 1not2 region
loc 2not1 no_good_at_all
u firstEntry, clear
unab 1 : _all
d using secondEntry, varl
loc 2 `r(varlist)'
assert `:list 1 == 2'
drop `2not1'
sa gen1
u secondEntry, clear
drop `1not2'
sa gen2
u gen1
loc id uniqueid
ds `id', not
loc noid `r(varlist)'
foreach varlist in "`noid'" _all {
cfout `varlist' using gen2, id(uniqueid)
loc r_varlist `r(varlist)'
unab expected : `varlist'
loc expected : list expected - id
loc expected : list expected - 1not2
assert `:list r_varlist == expected'
loc varonlym `r(varonlym)'
assert `:list varonlym == 1not2'
}
cd ..
* Test 24
cd 24
* Normal
u firstEntry, clear
unab varlist : region-no_good_at_all
cfout `varlist' using secondEntry, id(uniqueid)
loc r_varlist `r(varlist)'
assert `:list r_varlist == varlist'
* -tostring- the master version.
loc tostring region no_good_at_all
assert `:list tostring in varlist'
conf numeric var `tostring'
tostring `tostring', replace
conf str var `tostring'
cfout `varlist' using secondEntry, id(uniqueid)
loc difftype `r(difftype)'
assert `:list difftype == tostring'
loc r_varlist `r(varlist)'
loc expected : list varlist - difftype
assert `:list r_varlist == expected'
* -tostring- the using version.
u secondEntry, clear
conf numeric var `tostring'
tostring `tostring', replace
conf str var `tostring'
tempfile 2
sa `2', replace
u firstEntry, clear
cfout `varlist' using `2', id(uniqueid)
loc difftype `r(difftype)'
assert `:list difftype == tostring'
loc r_varlist `r(varlist)'
loc expected : list varlist - difftype
assert `:list r_varlist == expected'
cd ..
* Test 25
cd 25
u firstEntry, clear
forv i = 1/2 {
loc ab region-no_good_at_all
loc varlist : di _dup(`i') "`ab' "
cfout `varlist' using secondEntry, id(uniqueid)
assert r(N) == 15000
assert r(discrep) == 44
loc r_varlist `r(varlist)'
unab unab : `ab'
assert `:list r_varlist == unab'
}
cd ..
* Test 26
cd 26
u gen1, clear
foreach saving in "" saving(diff) {
cfout one x using gen2, id(id) dropdiff `saving'
assert r(N) == 1000
assert r(discrep) == 0
assert "`r(varlist)'" == "one"
assert "`r(alldiff)'" == "x"
}
u diff, clear
assert !_N
cd ..
* Test 44
cd 26
cap erase diff.dta
u gen1, clear
foreach saving in "" saving(diff) {
cfout one x using gen2, id(id) `saving'
assert r(N) == 2000
assert r(discrep) == 1000
assert "`r(varlist)'" == "one x"
assert "`r(alldiff)'" == "x"
}
u diff, clear
assert _N == 1000
cd ..
* Test 46
cd 46
u gen1, clear
#d ;
loc tests "
"" 6 3 "n s"
nostring 3 1 n
nonumeric 3 2 s
"nostring nonumeric" 0 0 ""
";
#d cr
while `:list sizeof tests' {
gettoken opts tests : tests
gettoken N tests : tests
gettoken discrep tests : tests
gettoken varlist tests : tests
u gen1, clear
foreach saving in "" "saving(diff, replace)" {
cfout n s using gen2, id(id) `opts' `saving'
assert r(N) == `N'
assert r(discrep) == `discrep'
assert "`r(varlist)'" == "`varlist'"
}
u diff, clear
assert _N == `discrep'
}
* -nostring- for all string variables
u gen1, clear
tostring _all, replace
unab all : _all
conf str var `all'
preserve
u gen2, clear
tostring _all, replace
conf str var `all'
sa gen2_str
restore
cfout using gen2_str, id(id) nostring
assert r(N) == 0
assert r(discrep) == 0
* -nonumeric- for all numeric variables
u gen1, clear
destring, replace
conf numeric var `all'
preserve
u gen2, clear
destring, replace
conf numeric var `all'
sa gen2_num
restore
cfout using gen2_num, id(id) nonumeric
assert r(N) == 0
assert r(discrep) == 0
cd ..
* Test 47
cd 47
u gen1, clear
pr test47, rclass
_on_colon_parse `0'
loc 0 "`s(before)'"
loc cfout "`s(after)'"
syntax, [NUMeric STRing]
loc 0 "`cfout'"
syntax varlist, [SAving(str asis) *]
if "`numeric'`string'" == "" ///
err 198
if "`numeric'" != "" & "`string'" != "" ///
err 198
cfout `varlist' using gen2, id(id) saving(diff, `saving' replace) `options'
ret add
preserve
u diff, clear
conf `numeric'`string' var Master Using
end
test47, num: n
test47, num: n, nonumeric
test47, str: s
test47, num: s, nostring
test47, str: n s
test47, str: n s, nonumeric
test47, num: n s, nostring
test47, num: n s, nonumeric nostring
test47, str: s_alldiff
test47, num: s_alldiff, dropdiff
test47, str: n s_alldiff
test47, num: n s_alldiff, dropdiff
assert "`r(alldiff)'" == "s_alldiff"
test47, str: n, saving(labval)
test47, str: n, saving(labval) nonumeric
test47, str: n s_alldiff, saving(labval)
test47, str: n s_alldiff, saving(labval) dropdiff
cd ..
* Test 48
cd 48
u 1, clear
lab data "Master label"
assert "`:data lab'" == "Master label"
note: Master note
preserve
u 2, clear
lab data "Using label"
note: Using note
sa gen2
restore
cfout gender using gen2, id(id) saving(diff)
u diff, clear
assert "`:data lab'" == ""
assert "`:char _dta[]'" == ""
cd ..
* Test 49
cd 49
u 1, clear
cfout gender using 2, id(id) saving(diff)
compdta 1
cfout gender using 2, id(id) saving(diff, replace) nopre
compdta diff
cd ..
* Test 63
cd 63
pr nc_default
syntax varlist(min=2 max=2), Generate(name)
gettoken var1 var2 : varlist
gen `generate' = `var1' != `var2'
end
pr nc_same
syntax varlist(min=2 max=2), Generate(name)
gen `generate' = 0
end
pr nc_diff
syntax varlist(min=2 max=2), Generate(name)
gen `generate' = 1
end
pr nc_range_tenth
syntax varlist(min=2 max=2), Generate(name)
gettoken var1 var2 : varlist
gen `generate' = abs(`var1' - `var2') > .1
end
pr nc_range
syntax varlist(min=2 max=2), Generate(name) Range(real)
gettoken var1 var2 : varlist
gen `generate' = abs(`var1' - `var2') > `range'
end
pr nc_two
syntax varlist(min=2 max=2), Generate(name)
gen `generate' = 2
end
pr nc_miss
syntax varlist(min=2 max=2), Generate(name)
gen `generate' = .
end
#d ;
loc progdiscrep "
"" 6
nc_default 6
nc_same 0
nc_diff 7
nc_range_tenth 3
"nc_range, range(.1)" 3
"nc_range, range(.2)" 1
nc_default, 6
"nc_default ," 6
"nc_range , range(.1)" 3
nc_two 7
nc_miss 7
";
#d cr
while `:list sizeof progdiscrep' {
gettoken program progdiscrep : progdiscrep
gettoken discrep progdiscrep : progdiscrep
di as res "`program'"
u gen1, clear
cfout x using gen2, id(id) numcomp(`program') ///
saving(diff, all replace) nopre
assert r(N) == 7
assert r(discrep) == `discrep'
assert inlist(diff, 0, 1)
qui cou if diff
assert r(N) == `discrep'
}
cd ..
* Test 69
cd 69
u 1, clear
keep in 1/10
sa gen1_10
drop in 1/L
sa gen1_0
u 2, clear
keep in 1/10
sa gen2_10
drop in 1/L
sa gen2_0
foreach saving in "" "saving(diff, replace)" {
u gen1_0, clear
cfout gender using gen2_10, id(id) `saving'
assert "`r(varlist)'" == "gender"
assert !r(N)
assert !r(discrep)
assert "`r(alldiff)'" == ""
u gen1_10, clear
cfout gender using gen2_0, id(id) `saving'
assert "`r(varlist)'" == "gender"
assert !r(N)
assert !r(discrep)
assert "`r(alldiff)'" == ""
u gen1_0, clear
cfout gender using gen2_0, id(id) `saving'
assert "`r(varlist)'" == "gender"
assert !r(N)
assert !r(discrep)
assert "`r(alldiff)'" == ""
}
u diff, clear
assert "`:type Master'" == "byte"
cd ..
* Test 70
cd 70
u 1, clear
lab de orphan 1 1
sa gen1, o
cfout gender using 2, id(id) saving(diff)
rcof "noi compdta 1" == 9
compdta gen1
cfout gender using 2, id(id) saving(diff, replace) nopre
compdta diff
cd ..
* Test 80
cd 80
u 2, clear
assert _N > 2
drop in 1
sa gen2
u 1, clear
drop in L
cfout gender using gen2, id(id) saving(diff)
assert r(N)
assert r(Nonlym) == 1
assert r(Nonlyu) == 1
cfout gender using gen2, id(id) saving(diff_nomatch) nomatch
compdta diff diff_nomatch
* Show warning messages about both variables and observations not compared.
cfout using gen2, id(id) saving(diff, replace)
cd ..
* Test 89
loc curdir "`c(pwd)'"
cd ../../help/example/2.0.0
do gen
cap erase diffs.dta
clear
* 1
use firstEntry
cfout region-no_good_at_all using secondEntry, id(uniqueid)
* 2
cfout region-no_good_at_all using secondEntry, id(uniqueid) saving(diffs)
use diffs
* 3
use firstEntry
cfout region-no_good_at_all using secondEntry, id(uniqueid) saving(diffs, ///
variable(varname) masterval(master_value) usingval(using_value) replace)
use diffs
* 4
use firstEntry
cfout region-no_good_at_all using secondEntry, id(uniqueid) ///
saving(diffs, keepmaster(deo) replace)
use diffs
* 5
use firstEntry
cfout region-no_good_at_all using secondEntry, id(uniqueid) ///
saving(diffs, all replace)
use diffs
count if diff
* 6
use firstEntry
cfout region-no_good_at_all using secondEntry, id(uniqueid) ///
saving(diffs, properties(type) replace)
use diffs
generate isstrvar = strmatch(type, "str*")
* 7
use firstEntry, clear
cfout region-no_good_at_all using secondEntry, id(uniqueid) ///
saving(diffs, properties(type(storage_type)) replace)
use diffs
* 8
erase diffs.dta
use firstEntry
rename deo deo1
cfout region-no_good_at_all using secondEntry, id(uniqueid) saving(diffs, all keepmaster(deo1) keepusing(deo))
use diffs, clear
rename deo deo2
generate swap = deo1 > deo2
generate t = deo1 if swap
replace deo1 = deo2 if swap
replace deo2 = t if swap
drop swap t
bysort deo*: generate total = _N
by deo*: egen total_diff = total(diff)
by deo*: generate error_rate = 100 * total_diff / total
format error_rate %9.2f
sort deo*
egen tag = tag(deo*)
list deo* total_diff total error_rate if tag, abbreviate(32) noobs
* 9
if `readreplace' {
clear
use firstEntry
readreplace using correctedValues.csv, id(uniqueid) variable(question) value(correctvalue)
cfout region-no_good_at_all using firstEntry, id(uniqueid) saving(diff1, all keepusing(deo))
cfout region-no_good_at_all using secondEntry, id(uniqueid) saving(diff2, all keepusing(deo))
use diff1, clear
append using diff2
bysort deo: generate total = _N
by deo: egen total_diff = total(diff)
by deo: generate error_rate = 100 * total_diff / total
format error_rate %9.2f
sort deo
egen tag = tag(deo)
list deo total_diff total error_rate if tag, abbreviate(32) noobs
erase diff1.dta
erase diff2.dta
}
* Remarks for options strcomp() and numcomp()
erase diffs.dta
clear
* -strcomp()-
use firstEntry
cfout firstname using secondEntry, id(uniqueid)
cfout firstname using secondEntry, id(uniqueid) nopunct
program remove_brackets
syntax varlist(min=2 max=2 string)
foreach var of local varlist {
replace `var' = subinstr(`var', "[", "", .)
replace `var' = subinstr(`var', "]", "", .)
}
end
cfout firstname using secondEntry, id(uniqueid) nopunct strcomp(remove_brackets)
program fromto
syntax varlist(min=2 max=2 string), from(string) to(string)
foreach var of local varlist {
replace `var' = "`to'" if `var' == "`from'"
}
end
cfout firstname using secondEntry, id(uniqueid) nopunct strcomp(fromto, from("Lilly") to("Lily"))
* -numcomp()-
use firstEntry
cfout age using secondEntry, id(uniqueid)
program range5
syntax varlist(min=2 max=2 numeric), generate(name)
gettoken var1 varlist : varlist
gettoken var2 : varlist
generate `generate' = abs(`var1' - `var2') > 5
end
cfout age using secondEntry, id(uniqueid) numcomp(range5)
program range
syntax varlist(min=2 max=2 numeric), generate(name) d(real)
gettoken var1 varlist : varlist
gettoken var2 : varlist
generate `generate' = abs(`var1' - `var2') > `d'
end
cfout age using secondEntry, id(uniqueid) numcomp(range, d(5))
use firstEntry
cfout firstname using secondEntry, id(uniqueid) nopunct strcomp(remove_brackets)
cfout firstname using secondEntry, id(uniqueid) nopunct saving(diffs)
use diffs
remove_brackets Master Using
drop if Master == Using
display _N
* Finish up
erase diffs.dta
cd "`curdir'"
* Test 90
cd 90
pr nc_is_diff
syntax varlist(min=2 max=2 numeric), Generate(name)
gettoken var1 var2 : varlist
gen `generate' = `var1' != `var2'
end
u firstEntry, clear
cfout region-firstname using secondEntry, id(uniqueid) saving(diffs)
cfout region-firstname using secondEntry, id(uniqueid) saving(diffs_nc) ///
numcomp(nc_is_diff)
compdta diffs diffs_nc
cd ..
* Test 91
cd 91
pr nc_sort_rev
syntax varlist(min=2 max=2), Generate(name)
gettoken var1 var2 : varlist
gsort -`var1' -`var2'
gen `generate' = `var1' != `var2'
end
pr nc_jumble
syntax varlist(min=2 max=2), Generate(name)
gettoken var1 var2 : varlist
tempvar u
gen `u' = runiform()
sort `u'
gen `generate' = `var1' != `var2'
end
u firstEntry, clear
cfout region-no_good_at_all using secondEntry, id(uniqueid) saving(diffs)
loc ctr 0
foreach numcomp in nc_sort_rev `:di _dup(25) "nc_jumble "' {
loc ++ctr
di "numcomp(`numcomp')"
cfout region-no_good_at_all using secondEntry, ///
id(uniqueid) numcomp(`numcomp') saving(diffs_numcomp, replace)
compdta diffs_numcomp diffs
}
assert `ctr' == 26
cd ..
* Test 92
cd 92
pr sc_same92
syntax varlist(min=2 max=2)
gettoken var1 var2 : varlist
replace `var1' = `var2'
end
pr sc_diff92
syntax varlist(min=2 max=2)
gettoken var1 var2 : varlist
assert strlen(`var1') < c(maxstrvarlen)
replace `var1' = `var2' + "x"
end
pr sc_caller
loc cmd = cond(_caller() < 11, "sc_same92", "sc_diff92")
`cmd' `0'
end
pr nc_same92
syntax varlist(min=2 max=2), Generate(name)
gen `generate' = 0
end
pr nc_diff92
syntax varlist(min=2 max=2), Generate(name)
gen `generate' = 1
end
pr nc_caller
loc cmd = cond(_caller() < 11, "nc_same92", "nc_diff92")
`cmd' `0'
end
if c(stata_version) >= 11 {
u firstEntry, clear
cfout firstname using secondEntry, id(uniqueid) ///
saving(diff_sc_same) strcomp(sc_same92)
assert !r(discrep)
cfout firstname using secondEntry, id(uniqueid) ///
saving(diff_sc_diff) strcomp(sc_diff92)
assert r(discrep) == r(N)
assert r(N)
foreach caller in 10.1 11 {
vers `caller': cfout firstname using secondEntry, id(uniqueid) ///
saving(diff_sc_caller, replace) strcomp(sc_caller)
compdta diff_sc_caller diff_sc_`=cond(`caller' < 11, "same", "diff")'
}
cfout gender using secondEntry, id(uniqueid) ///
saving(diff_nc_same) numcomp(nc_same92)
assert !r(discrep)
cfout gender using secondEntry, id(uniqueid) ///
saving(diff_nc_diff) numcomp(nc_diff92)
assert r(discrep) == r(N)
assert r(N)
foreach caller in 10.1 11 {
vers `caller': cfout gender using secondEntry, id(uniqueid) ///
saving(diff_nc_caller, replace) numcomp(nc_caller)
compdta diff_nc_caller diff_nc_`=cond(`caller' < 11, "same", "diff")'
}
}
cd ..
* Test 93
cd 93
u 1, clear
cfout gender using 2, id(id) saving(diffs)
loc N = r(N)
assert r(discrep)
copy 1.dta gen1.dta
foreach using in 1 gen1 {
u 1, clear
cfout gender using `using', id(id) saving(diffs_self, replace)
assert r(N) == `N'
assert !r(discrep)
u diffs, clear
drop in 1/L
recast str1 Question
recast byte Master Using
compdta diffs_self
}
cd ..
* Test 94
cd 94
u 2, clear
foreach var of var id gender {
loc post`var' : di _dup(`=c(namelen) - strlen("`var'")') "x"
ren `var' `var'`post`var''
}
sa gen2
u 1, clear
cfout gender using 2, id(id) saving(diffs)
ren id id`postid'
ren gender gender`postgender'
cfout gender`postgender' using gen2, id(id`postid') saving(diffs_long) nopre
assert _N
ren id`postid' id
replace Question = "gender" if Question == "gender`postgender'"
compress Question
compdta diffs
cd ..
* Test 98
cd 98
u 1, clear
cfout gender using 2, id(id) saving(diff_12)
u 2, clear
cfout gender using 1, id(id) saving(diff_21) nopre
ren Master t
ren Using Master
ren t Using
loc lab : var lab Master
lab var Master "`:var lab Using'"
lab var Using "`lab'"
move Master Using
compdta diff_12
cd ..
* Test 99
cd 99
if c(stata_version) >= 13 {
u 2, clear
tostring gender, replace
assert "`:type gender'" != "strL"
sa gen2_str
recast strL gender
sa gen2_strL
u 1, clear
tostring gender, replace
assert "`:type gender'" != "strL"
cfout gender using gen2_strL, id(id)
recast strL gender
cfout gender using gen2_strL, id(id)
cfout gender using gen2_str, id(id)
}
cd ..
* Test 102
cd 102
if c(stata_version) >= 13 {
u gen1, clear
cfout s using gen2, id(id)
cfout s using gen2, id(id) saving(diff) nopre
assert _N == 1
assert Master == "x"
assert Using == char(0)
}
cd ..
* Test 103
cd 103
u firstEntry, clear
cfby region-no_good_at_all using secondEntry, id(uniqueid) by(deo)
mat cfby_q = r(q)
mat cfby_e = r(e)
ren deo deo1
cfout region-no_good_at_all using secondEntry, ///
id(uniqueid) saving(diffs, all keepmaster(deo1) keepusing(deo) replace)
u diffs, clear
ren deo deo2
gen swap = deo1 > deo2
gen t = deo1 if swap
replace deo1 = deo2 if swap
replace deo2 = t if swap
drop swap t
bys deo*: gen total = _N
by deo*: egen total_diff = total(diff)
by deo*: gen error_rate = total_diff / total
by deo*: keep if _n == 1
mata:
mats = "cfby_q", "cfby_e"
for (i = 1; i <= length(mats); i++) {
X = st_matrix(mats[i])
assert(rows(X) == cols(X))
len = rows(X)
X = vec(X)
X = J(rows(X), 2, .), X
row = 0
for (j = 1; j <= len; j++) {
for (k = 1; k <= len; k++) {
row++
X[row, 1] = min((j, k))
X[row, 2] = max((j, k))
}
}
X = uniqrows(X)
assert(rows(X) == st_nobs())
assert(X[,1] == st_data(., "deo1"))
assert(X[,2] == st_data(., "deo2"))
st_store(., st_addvar("double", mats[i]), X[,3])
}
end
assert cfby_q == total
assert cfby_e == total_diff
cd ..
/* -------------------------------------------------------------------------- */
/* id() */
* Test 27
cd 27
u firstEntry, clear
foreach vars in region-no_good_at_all "region-no_good_at_all uniqueid" {
cfout `vars' using secondEntry, id(uniqueid)
assert r(N) == 15000
assert r(discrep) == 44
loc varlist `r(varlist)'
unab unab : region-no_good_at_all
assert `:list varlist == unab'
}
cd ..
* Test 30
cd 30
u gen1, clear
cfout gender using gen2, id(id1 id2) saving(diff)
compdta diff gen_diff
cd ..
* Test 31
cd 30
cap mkdir ../31
loc dtas : dir . file "gen*.dta"
foreach dta of loc dtas {
u "`dta'", clear
d, varl
loc sort `r(sortlist)'
tostring id2, replace
conf numeric var id1
conf str var id2
if "`sort'" != "" ///
sort `sort'
sa "../31/`dta'"
}
cd ../31
u gen1, clear
cfout gender using gen2, id(id1 id2) saving(diff)
compdta diff gen_diff
cd ..
* Test 34
cd 11
u 1, clear
cfout gender using 2, id(id id) saving(diff34)
compdta diff34 expected/diff
cd ..
cd 30
u gen1, clear
cfout gender using gen2, id(id1 id2 id1 id2) saving(diff34)
compdta diff34 gen_diff
cd ..
* Test 95
cd 95
u 2, clear
tostring id, replace
sa gen2
u 1, clear
cfout gender using 2, id(id) saving(diffs)
tostring id, replace
cfout gender using gen2, id(id) saving(diffs_str) nopre
d, varl
loc sort `r(sortlist)'
destring id, replace
isid `sort'
sort `sort'
form id %8.0g
char id[tostring]
char id[destring]
compdta diffs
cd ..
* Test 96
cd 96
u 2, clear
pr longid
mata: st_sstore(., st_addvar("str`c(maxstrvarlen)'", "x"), ///
J(st_nobs(), 1, c("maxstrvarlen") * "x"))
tostring id, replace
char id[tostring]
replace id = id + substr(x, 1, strlen(x) - strlen(id))
assert strlen(id) == c(maxstrvarlen)
end
longid
sa gen2
u 1, clear
cfout gender using 2, id(id) saving(diffs)
longid
cfout gender using gen2, id(id) saving(diffs_str) nopre
d, varl
loc sort `r(sortlist)'
destring id, replace ignore(x)
isid `sort'
sort `sort'
form id %8.0g
char id[destring]
compdta diffs
cd ..
* Test 97
cd 97
* Numeric
u 1, clear
cfout gender using 2, id(id) saving(diff) nopre
levelsof id
assert "`r(levels)'" == "72 138"
u 2, clear
pr missid_num
cou if id == 72
assert r(N)
cou if id == .
assert !r(N)
replace id = . if id == 72
cou if id == 138
assert r(N)
cou if id == .a
assert !r(N)
replace id = .a if id == 138
end
missid_num
sa gen2
u 1, clear
missid_num
cfout gender using gen2, id(id) saving(diff_miss_num) nopre
cou if id == .
assert r(N)
cou if id == .a
assert r(N)
replace id = 72 if id == .
replace id = 138 if id == .a
isid id