-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap_pairs.sas
1719 lines (1532 loc) · 45.8 KB
/
swap_pairs.sas
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
/*
As a work of the United States government, this project is in the public
domain within the United States. Additionally, we waive copyright and related
rights in the work worldwide through the CC0 1.0 Universal public domain
dedication (https://creativecommons.org/publicdomain/zero/1.0/)
*/
%global imrate; *cutoff for block imputation rate;
%global rno; *random number start;
%global s2uniq; *count of selected uniqs by key2;
%global state;
%global allhouse; *count of households;
%global popsize; *count of persons;
%global subject; *number subjected to swapping(-imputes);
%global target; *percentage of households to be swapped;
%global deselect; *number of households to be dropped;
%global p1floor; *maximum percentage of prep=1 dropped (odd);
%global p2floor; *maximum percentage of prep=2 dropped;
%global p3floor; *maximum percentage of prep=3 dropped;
%global tolrnc; *margin for completion of deselect;
%global prob_cutoff1; *probability that an hhsize bin keeps its orig value when noised;
%global prob_cutoff2; *prob_cutoff2-prob_cutoff1 = probability that an hhsize bin decreases by 1 / 1-prob_cutff2. = probability that an hhsize bin increases by 1;
%global pprop1; *probability of a record being in group 1;
* (pseudo_tract=tract);
%global pprop2; *probability of a record being in group 2;
* (pseudo_tract chosen within county);
%include 'header.txt'; *parameter file;
/*************************************************
The target swapping rate determines the percent of records swapped.
The random number is still used in randomly de-selecting records
for swapping, after initially selecting all records.
The p3floor determines the maximum percent of prep=3 records that
can be de-selected. Because we set all records to prep=3, p3floor
must be larger than 1-target swap rate. Practically, we can set
p3floor as 0.99 and leave it there (note, I am not sure if the
code will run correctly if you set any of the floor macros to 1).
The p1floor and p2floor variables are not relevant because we
assign all records prep=3, meaning there are no prep=1 or prep=2
records subject to swapping. The imrate variable is not relevant
because the part of the dynamic record selection based on
imputation rate, among other things, is no longer used. * I propose deleting this comment once we have confirmed that we are confident it is correct
***************************************************/
libname cef '../../data/reid_module/cef/';
libname library '.';
libname me '.'; *everything set to directory
from which the program is run;
**filename orig "&ifile"; *input data;
filename edited "swap00&state..dat"; *output,list of pairs to be swapped;
filename summary "sum00&state..txt"; *record counts;
filename debug "dbug00&state..txt"; *samples of major files;
filename stat "&state.stat.dat";
/* options cc=print; */
options pagesize=300;
options fullstimer;
options linesize=72;
proc printto print=summary new;
run; *init run summary;
data _null_;
file print;
put;
put "the input data file is: &ifile";
put "the state identifier is: &state";
put "the impute cutoff is: &imrate";
put "the random start is: &rno";
put "the target rate is: &target";
put "the floor for prep=1 is: &p1floor";
put "the floor for prep=2 is: &p2floor";
put "the floor for prep=3 is: &p3floor";
put "the proportion for hhsize noise grp 1 is: &prob_cutoff1";
put "the proportion for hhsize noise grp 2 is: &prob_cutoff2 minus &prob_cutoff1";
put "the proportion for pseudogeog grp 1 is: &pprop1";
put "the proportion for pseudogeog grp 2 is: &pprop2";
put;
run;
proc printto;
run;
proc printto print=debug new;
run; *init debug summary;
proc printto;
run;
*macro for reporting dataset size (BRAAM);
%macro HOWMANY (LIB,DSN) ;
%global NUMOBS ;
%let NUMOBS = 0 ;
proc sql noprint ;
select nobs-delobs into :NUMOBS
from dictionary.tables
where libname =%upcase("&LIB") and memname=%upcase("&DSN") ;
quit ;
%mend HOWMANY ;
%put &statenum.;
proc contents data=cef.swap_hcef;
run;
proc print data=cef.swap_hcef(where=(input(tabblkst, best2.) eq 2));
run;
%macro readin;
/*********************************
Load file created in preprocess.sas
for a given state (the state is specified
in header_newinput.txt))
**********************************/
data orig(keep=key);
set cef.swap_hcef(where=(input(tabblkst, BEST2.) eq &statenum.));
county=put(tabblkcou,3.);
tract=put(tabtractce,6.);
block=put(tabblk,4.);
tenure=put(ten,1.);
key=0;
run;
proc sort data=orig out=sorig;
by descending key;
run;
data keylist;
set sorig;
by descending key; *get one of each and count them;
retain kcount (0);
if first.key then kcount=1;
else kcount=kcount+1;
if last.key then output;
run;
proc datasets nolist library=work;
delete sorig orig;
run;
data keylist2(rename=(key=start)); *make formats;
set keylist;
label=_n_; *arbitrarily assign index number;
fmtname="iky_&state";
type="J";
run;
data keylist3(rename=(key=label));
set keylist;
start=_n_;
fmtname="oky_&state";
run;
proc format library=library cntlin=keylist2;
run;
proc format library=library cntlin=keylist3;
run;
* read in all the data;
data orig2 (drop=ikey geo tenure)
key2list (keep=key2)
me.ckey_&state (keep=key geo);
set cef.swap_hcef(keep=control tabblkcou tabtractce tabblk ten household_size tabblkst where=(input(tabblkst, best2.) eq &statenum.));
county=put(tabblkcou,3.);
tract=put(tabtractce,6.);
block=put(tabblk,4.);
tenure=put(ten,1.);
geo=cat(county,tract,block);
ikey=0;
key=input(ikey,8.);
hhbin=household_size;
hhbin_noised=hhbin;
call streaminit(1);
rand1=rand("Uniform");
if rand1>&prob_cutoff1. and rand1<=&prob_cutoff2. then hhbin_noised=hhbin_noised-1;
if rand1>&prob_cutoff2. then hhbin_noised=hhbin_noised+1;
chhbin_noised=put(hhbin_noised,2.);
key2=chhbin_noised;
run;
data orig2;
set orig2;
call streaminit(1);
u=rand("Uniform");
if u<&pprop1 then pgroup=1;
else if u<&pprop1+&pprop2 then pgroup=2;
else pgroup=3;
run;
proc sort data=orig2;
by county tract;
run;
data group1 group2 group3;
set orig2;
call streaminit(1);
randsort=rand("Uniform");
if pgroup=1 then output group1;
if pgroup=2 then output group2;
if pgroup=3 then output group3;
run;
data group1b;
set group1;
pseudo_county=county;
pseudo_tract=tract;
run;
data group2a(keep=county tract randsort);
set group2;
run;
data group3a(keep=county tract randsort);
set group3;
run;
proc sort data=group2a;
by county randsort;
run;
data group2b;
merge group2 group2a(rename=(county=pseudo_county tract=pseudo_tract) drop=randsort);
run;
proc sort data=group3a;
by randsort;
run;
data group3b;
merge group3 group3a(rename=(county=pseudo_county tract=pseudo_tract) drop=randsort);
run;
data orig3;
set group1b group2b group3b;
run;
proc printto print=debug;
run;
proc freq data=keylist;
table kcount/out=klist;
title 'class counts for long key (kcount=1 == uniques)';
run;
*how many uniques (kcount=1) keys with 2 records etc ;
options nodate;
proc print noobs data=orig3(obs=10);
format key oky_&state..;
title 'first 10 records in dataset, key should display in char';
run;
proc print noobs data=me.ckey_&state(obs=10);
format key oky_&state..;
title 'first 10 records in study dataset, key should display in char';
run;
*just a sample to see if it read in correctly and displays ok;
options linesize=72;
proc printto print=summary;
run;
data _null_ ;
set klist(obs=1);
file print;
put ' ' count ' uniques';
title;
run;
*need to analyze with respect to key2, this page faults badly,
could be taken out if you want to skip the analysis;
proc sort data=key2list;
by key2;
run;
data k2list;
set key2list;
by key2; *get one of each and count them;
retain kcount (0);
if first.key2 then kcount=1;
else kcount=kcount+1;
if last.key2 then output;
run;
proc printto print=debug;
run;
*how many uniques (kcount=1) keys with 2 records etc ;
proc freq data=k2list;
table kcount/out=k3list;
title 'class counts short key (kcount=1 == uniques)';
run;
proc printto;
run;
data _null_;
file summary mod;
if _n_=1 then put 'uniqueness distribution for short key';
set k3list(obs=5);
put kcount count;
title;
run;
%HOWMANY(work,orig3);
data _null_;
title;
file summary mod;
put;
put "&NUMOBS read in";
%let allhouse = &NUMOBS;
run;
proc datasets nolist library=work;
delete key2list keylist keylist2 keylist3;
run;
*end bookkeeping;
proc print data=orig2(keep=household_size hhbin rand1 hhbin_noised chhbin_noised);
run;
%mend readin;
%macro impute;
* put data in geographic order;
proc sort data=orig3 out=origsort;
by pseudo_county pseudo_tract block;
run;
proc datasets nolist library=work;
delete orig3;
run;
data withrate;
set origsort end=last;
length uniqkey tuniqr 3;
uniqkey=0;
tuniqr=0;
run;
proc datasets nolist library=work;
delete origsort rates2;
run;
%mend impute;
%macro unique;
proc sort data=withrate out=wrate1;
by key2 pseudo_county pseudo_tract block;
run;
proc datasets nolist library=work;
delete withrate;
run;
data eligibl1(drop=rate)
dimpute (drop=prep key rate);
set wrate1;
by key2 pseudo_county pseudo_tract block;
length prep unique 3;
if first.block and last.block then unique=1;
else unique=0;
ranno=ranuni(&rno);
*SUBJECTS ALL RECORDS TO SWAPPING RATHER THAN MOST SENSITIVE;
prep=3;
output eligibl1;
run;
*some bookkeeping;
proc datasets nolist library=work;
delete wrate1;
run;
%HOWMANY(work,eligibl1);
data _null_;
file summary mod;
title;
put;
put "&NUMOBS records subjected to swapping procedure";
%let subject = &NUMOBS;
run;
* sort the data (backwards) in preparation for partnering, ranno does double
duty by making sure there is no ambiguity in the order;
proc sort data=eligibl1 out=eligibl2;
by descending key2 descending pseudo_county descending pseudo_tract descending prep
descending ranno;
run;
proc datasets nolist library=work;
delete eligibl1;
run;
%mend unique;
%macro estswap;
*take a look at the set of records to be swapped, number swap sequences,
count their length.;
data aseq1(keep=select uflag prep seqno)
preswap;
set eligibl2 end=last;
by descending key2 descending pseudo_county descending pseudo_tract descending prep;
length uflag select inkey sinkey seqno 4;
retain select inkey sinkey;
retain uflag seqno hseqno 0;
if first.key2 then do;
if select>0 then do;
output aseq1;
end;
inkey=1;
select=0;
sinkey=0;
end;
else do;
inkey=inkey+1;
end;
if prep>0 then do;
select=select+1;
sinkey=sinkey+1;
end;
else do;
if select>0 then do;
output aseq1;
select=0;
end;
end;
if last then do;
output aseq1;
end;
if select=1 then hseqno=hseqno+1;
if select>0 then do;
seqno=hseqno;
output preswap;
end; *uflag counts unswappables;
else seqno=0;
if last then call symput('s2uniq',put(uflag,10.));
run;
proc sort data=preswap; *need length of sequence the record belongs to;
by seqno; *and parity;
run;
data aseq1a;
set aseq1;
if (mod(select,2)=0) then odd=0;
else odd=1;
run;
proc sort data=aseq1a;
by seqno;
run;
proc freq noprint data=aseq1a;
table select/out=aseq2;
run;
*estimate the number of swaps currently marked,
bring in the target number, put the difference into
the global variable deselect;
data aseq3;
set aseq2 end=last;
retain csum 0;
if select=1 then count=count-&s2uniq;
if (mod(select,2)=0) then contrib=select*count;
else contrib=(select+1)*count;
csum=csum+contrib;
if last then do;
toswap=&allhouse*⌖
dselect=csum-&allhouse*⌖
output;
if dselect>0 then call symput('deselect',put(dselect,10.));
else call symput('deselect',0);
end;
run;
%HOWMANY(work,preswap);
data _null_;
set aseq3;
prop=(&NUMOBS-&s2uniq)/csum;
title;
file summary mod;
put 'The number of households targeted: ' toswap ' projected: ' csum;
put 'Proportion of proj swap which are select: ' prop;
run;
%mend estswap;
*the next macro, reduce1, isolates the least sensitive population and determines
how much of it should be deselected. a core subpopulation must be left
regardless. the size of the core is determined in part by ,p1floor,. the
amount to be dropped is calculated, tkodd+tkeven, and selection is made
binomially at the sequence level for that fixed size. EG if there are 5 prep=1
selected households in the current sequence and we need to select 25 of the
remaining 100 prep=1, then we will deselect bin(5,1/4) of the 5. The number to
select and the number remaining are incremented down as usual for fixed sample
selection. The procedure is adapted for odd/even situations: where the number
taken is profitably rounded down, do so and increase the probablity for
selection in the remainder. nb if you push off too many, the sample size isnt
reached.;
%macro reduce1;
%let rno3 = 1; *random number start;
*redefine prep, merge in sequence level data;
data preswap2;
merge preswap(keep=seqno prep control key2 county tract pseudo_county pseudo_tract prep
ranno)
aseq1a(keep=seqno select odd);
by seqno;
length oprep 3;
oprep=prep;
rn3=ranuni(&rno3);
run;
*bookkeeping;
proc freq noprint data=preswap2;
table prep*oprep/out=redef;
run;
proc printto print=debug;
run;
proc printto;
run;
proc printto print=summary;
run;
proc print noobs data=redef(where=
((prep=2 and oprep=1) or(prep=3 and oprep in (1,2))));
var prep oprep count;
title 'redefine prep';
run;
proc freq data=preswap2;
table prep*odd/nopercent norow nocol out=initps;
title 'initial data on selected (redefined)';
run;
proc printto;
run;
*only want the prep=1 population, sort includes rn3 to get random order;
proc sort data=preswap2(where=(prep=1)) out=round1;
by seqno rn3;
run;
*need additional sequence level data-how many of this type;
proc freq noprint data=round1;
table seqno/out=p1inseq;
run;
proc sort data=p1inseq;
by seqno;
run;
data idseq;
merge p1inseq(in=in1) round1(keep=seqno select);
by seqno;
if in1;
if first.seqno;
run;
*make an estimate of the contribution from prep=1, this number goes into
fixed sample selection. count is the frequency of prep=1 gotten above;
data idseq2(keep=even1 odd1);
set idseq end=last;
retain even1 odd1 0;
if (mod(select,2)=0) then even1=even1+count;
else odd1=odd1+count+1;
if last then output;
run;
*dselp1 is the residual file, ds1 will go into the drop set, dlist
this is the binomial fixed sample selection;
data dselp1(drop=even1 flag odd odd1 p1 rno2 tktotal
p2 p1a tkodd tkeven take)
ds1(keep=control odd key2 county tract pseudo_county pseudo_tract prep oprep ranno);
retain flag odd1 even1 tkodd tkeven;
if _n_=1 then do;
set idseq2;
tktotal=floor(&p1floor*odd1)+floor(&p1floor*even1);
if &deselect/tktotal<1 then adjustp=&deselect/tktotal;
else adjustp=1;
tkodd=floor(&p1floor*odd1*adjustp);
tkeven=floor(&p1floor*even1*adjustp);
*tkodd tkeven are the target sample sizes;
end;
merge round1 p1inseq(keep=count seqno rename=(count=p1));
by seqno;
rno2 = 1; *random number start;
if first.seqno then do;
if odd=1 then do;
if odd1<1 then do; *this is to ensure a good
p value for ranbin, SAS doesnt
like p=1 (or 0);
tkodd=0;
odd1=1;
end;
if tkodd/odd1>1 then tkodd=odd1;
if tkodd<0 then tkodd=0;
if tkeven<0 then tkeven=0;
p2=tkodd/odd1;
if p2=1 or p2=0 then do;
if p2=1 then take=p1;
if p2=0 then take=0;
end; *finally, ranbin;
else take=ranbin(rno2,p1,p2);
if (mod(take,2)=0 and take>0) then take=take-1; *push off!;
if take>0 then do;
output ds1;
flag=take-1;
tkodd=tkodd-take-1; *bookkeeping for sample selection;
end;
else output dselp1;
odd1=odd1-p1-1;
end; *slightly different strategy for the
even side: divide by 2 multiply by 2;
else do;
p1a=floor(p1/2); *more push off occuring;
if even1<1 then do;
tkeven=0;
even1=1; *still have the pesky ranbin problem;
end;
if tkeven/even1>1 then tkeven=even1;
if tkeven<0 then tkeven=0;
p2=tkeven/even1;
if p1a>0 then do;
if p2=1 or p2=0 then do;
if p2=1 then take=2*p1a;
if p2=0 then take=0;
end; *finally;
else take=2*(ranbin(rno2,p1a,p2));
end;
else take=0;
if take>0 then do;
output ds1;
flag=take-1; *bookkeep;
tkeven=tkeven-take;
end;
else output dselp1;
even1=even1-p1;
end;
end;
else do;
if flag>0 then do;
output ds1;
flag=flag-1;
end;
else output dselp1;
end;
if last.seqno then flag=0;
run;
*evaluate how it went so the main program can determine if it needs
reduce2;
*reassemble selected;
proc datasets nolist lib=work;
delete preswap3;
run;
proc append base=preswap3 data=dselp1(drop=adjustp);
run;
proc append base=preswap3 data=preswap2(where=(prep ne 1) drop=odd);
run;
proc sort data=preswap3;
by seqno;
run;
*evaluate sequences;
proc freq noprint data=preswap3;
table seqno/out=r2select;
run;
data r2seq1a;
set r2select(rename=(count=select));
if (mod(select,2)=0) then odd=0;
else odd=1;
run;
proc sort data=r2seq1a;
by seqno;
run; *random number start;
%let rno3 = 1;
*merge on seq level data;
data preswap4;
merge preswap3(drop=select) r2seq1a(keep=seqno select odd);
by seqno;
rn3=ranuni(&rno3);
run;
proc printto print=debug;
run;
proc freq noprint data=preswap4;
table prep*odd/nopercent norow nocol out=ps4;
title 'after 1st phase';
run;
proc printto;
run;
data _null_; *post the results;
title;
retain sum4 sum0 0;
set ps4(rename=(count=c4) where=(prep=1) keep=count prep);
set initps(rename=(count=c0) where=(prep=1) keep=count prep) end=last;
sum4=sum4+c4;
sum0=sum0+c0;
if last then do;
r1=sum4/sum0;
file summary mod;
put 'fraction of prep=1 remaining: ' r1;
end;
run;
*summarize then calculate how much to go;
proc freq noprint data=r2seq1a;
table select/out=r2seq2;
run;
data r2seq3;
set r2seq2 end=last;
retain csum 0;
if select=1 then count=count-&s2uniq;
if (mod(select,2)=0) then contrib=select*count;
else contrib=(select+1)*count;
csum=csum+contrib;
if last then do;
toswap=&allhouse*⌖
dselect2=csum-&allhouse*⌖
output;
if dselect2>0 then call symput('deselect',put(dselect2,10.));
else call symput('deselect',0);
end;
run; *some more evaluation;
%HOWMANY(work,preswap4);
data _null_;
set r2seq3;
prop=(&NUMOBS-&s2uniq)/csum;
title;
file debug mod;
put 'Proportion of proj swap which are select (round1): ' prop;
run;
proc printto print=debug;
run;
proc print data=r2seq3;
var toswap csum dselect2;
title 'after prep=1 deselection';
run;
proc printto;
run;
proc datasets nolist lib=work;
delete preswap2 preswap3 dlist;
run; *this is the output to the main routine;
proc append base=dlist data=ds1(rename=(prep=nprep));
run;
%mend reduce1;
*just like reduce1;
%macro reduce2;
proc sort data=preswap4(where=(prep=2)) out=round2;
by seqno rn3;
run;
proc freq noprint data=round2;
table seqno/out=p2inseq;
run;
proc sort data=p2inseq;
by seqno;
run;
data idseq3;
merge p2inseq(in=in1) round2(keep=seqno select);
by seqno;
if in1;
if first.seqno;
run;
*make an estimate of the maximum number of drops from
prep=2;
data idseq4(keep=even1 odd1);
set idseq3 end=last;
retain even1 odd1 0;
if (mod(select,2)=0) then even1=even1+count;
else odd1=odd1+count+1;
if last then output;
run;
data dselp2(drop=even1 flag odd odd1 p1 rno2 tkodd tkeven tktotal
take p2 p1a)
ds2(keep=control odd key2 county tract pseudo_county pseudo_tract prep oprep ranno);
retain flag tkodd tkeven odd1 even1 0;
if _n_=1 then do;
set idseq4;
tktotal=floor(&p2floor*odd1+&p2floor*even1);
if &deselect/tktotal<1 then adjustp=&deselect/tktotal;
else adjustp=1;
tkodd=floor(&p2floor*odd1*adjustp);
tkeven=floor(&p2floor*even1*adjustp);
end;
merge round2 p2inseq(keep=count seqno rename=(count=p1));
by seqno;
rno2 = 1; *random number start;
if first.seqno then do;
if odd=1 then do;
if odd1<1 then do; *this is to ensure a good
p value for ranbin, SAS doesnt
like p=1 (or 0). SAS macro also
reads quotes inside of comments;
tkodd=0;
odd1=1;
end;
if tkodd/odd1>1 then tkodd=odd1;
if tkodd<0 then tkodd=0;
p2=tkodd/odd1;
if p2=1 or p2=0 then do;
if p2=1 then take=p1;
if p2=0 then take=0;
end;
else take=ranbin(rno2,p1,p2);
if (mod(take,2)=0 and take>0) then take=take-1;
if take>0 then do;
output ds2;
flag=take-1;
tkodd=tkodd-take-1;
end;
else output dselp2;
odd1=odd1-p1-1;
end;
else do;
p1a=floor(p1/2);
if even1<1 then do;
tkeven=0;
even1=1;
end;
if tkeven/even1>1 then tkeven=even1;
p2=tkeven/even1;
if p1a>0 then do;
if p2=1 or p2=0 then do;
if p2=1 then take=2*p1a;
if p2=0 then take=0;
end;
else take=2*(ranbin(rno2,p1a,p2));
end;
else take=0;
if take>0 then do;
output ds2;
flag=take-1;
tkeven=tkeven-take;
end;
else output dselp2;
even1=even1-p1;
end;
end;
else do;
if flag>0 then do;
output ds2;
flag=flag-1;
end;
else output dselp2;
end;
if last.seqno then flag=0;
run;
proc datasets nolist lib=work;
delete preswap5;
run;
proc append base=preswap5 data=dselp2(drop=adjustp);
run;
proc append base=preswap5 data=preswap4(where=(prep ne 2) drop=odd);
run;
proc sort data=preswap5;
by seqno;
run;
proc freq noprint data=preswap5;
table seqno/out=r3select;
run;
data r3seq1a;
set r3select(rename=(count=select));
if (mod(select,2)=0) then odd=0;
else odd=1;
run;
proc sort data=r3seq1a;
by seqno;
run;
data preswap6;
merge preswap5(drop=select) r3seq1a(keep=seqno select odd);
by seqno;
run;
proc printto print=debug;
run;
proc freq data=preswap6;
table prep*odd/nopercent norow nocol out=ps6;
title 'after phase 2';
run;
proc printto;
run;
data _null_;
retain sum6 sum0 0;
set ps6(rename=(count=c6) where=(prep=2) keep=count prep);
set initps(rename=(count=c0) where=(prep=2) keep=count prep) end=last;
sum6=sum6+c6;
sum0=sum0+c0;
if last then do;
r1=sum6/sum0;
file summary mod;
title;
put 'fraction of prep=2 remaining: ' r1;
end;
run;
proc freq noprint data=r3seq1a;
table select/out=r3seq2;
run;
data r3seq3;
set r3seq2 end=last;
retain csum 0;
if select=1 then count=count-&s2uniq;
if (mod(select,2)=0) then contrib=select*count;
else contrib=(select+1)*count;
csum=csum+contrib;
if last then do;
dselect3=csum-&allhouse*⌖
output;
if dselect3>0 then call symput('deselect',put(dselect3,10.));
else call symput('deselect',0);
end;
run;
%HOWMANY(work,preswap6);
data _null_;
set r3seq3;
prop=(&NUMOBS-&s2uniq)/csum;
title;
file debug mod;
put 'Proportion of proj swap which are select (round2): ' prop;
run;
proc printto print=debug;
run;
proc print data=r3seq3;
var csum dselect3;
title 'after prep=2 deselection';
run;
proc printto;
run;
proc append base=dlist data=ds2(rename=(prep=nprep));
run;
proc datasets nolist lib=work;
delete ds1 ds2 preswap4;
run;
%mend reduce2;
*just like reduce1;
%macro reduce3;
proc sort data=preswap6(where=(prep=3)) out=round3;
by seqno rn3;
run;
proc freq noprint data=round3;
table seqno/out=p3inseq;
run;
proc sort data=p3inseq;
by seqno;
run;
data idseq5;
merge p3inseq(in=in1) round3(keep=seqno select);
by seqno;
if in1;
if first.seqno;
run;
data idseq6(keep=even1 odd1);
set idseq5 end=last;
retain even1 odd1 0;
if (mod(select,2)=0) then even1=even1+count;
else odd1=odd1+count+1;
if last then output;
run;
proc printto print=debug;
run;
proc print data=idseq6;
title 'prep=3 by odd/even inflated for odd partners';
run;
proc printto;
run;
data dselp3(drop=even1 flag odd odd1 p1 rno2 tkodd tkeven tktotal
take p2 p1a)
ds3(keep=control odd key2 county tract pseudo_county pseudo_tract prep oprep ranno);
retain flag tkodd tkeven odd1 even1 0;
if _n_=1 then do;
set idseq6;