-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurveycalibrate.sas
1712 lines (1580 loc) · 50.9 KB
/
surveycalibrate.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
/*--------------------------------------------------------------------
Author: Tony An, Ph.D.
In survey sampling, calibration is commonly used for adjusting weights
to ensure that estimates for covariates in the sample match known
auxiliary information, such as marginal totals from census
data. Calibration can also be used to adjust for unit
nonresponse. This macro for calibration was developed using SAS/STAT
15.1 and SAS/IML software. The macro enables you to input the design
information, the controls for the auxiliary variables, and your
preferred calibration method, including either linear or exponential
method. Because unbounded calibration method can result in extreme
calibration weights, this macro also supports bounded versions of both
linear and exponential calibration methods. The macro creates
calibration replication weights according to the sample design and the
specified calibration method.
----------------------------------------------------------------------
DISCLAIMER:
THIS INFORMATION IS PROVIDED BY THE AUTHOR. AS A SERVICE
TO ITS USERS. IT IS PROVIDED "AS IS". THERE ARE NO WARRANTIES,
EXPRESSED OR IMPLIED, AS TO MERCHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE REGARDING THE ACCURACY OF THE MATERIALS OR CODE
CONTAINED HEREIN.
---------------------------------------------------------------------*/
%macro SurveyCalibrate(
DATA=, /* Input data set name */
OUT=, /* Output data set name */
/* Calibration parameters */
METHOD=, /* LINEAR | EXPONENTIAL | TRUNLINEAR | LOGIT */
WEIGHT=, /* Original weight variable */
CALWT=, /* Calibration weight variable, default CalWt */
CONTROLVAR=,/* Auxiliary control variables for calibration */
CTRLTOTAL=, /* Marginal totals for CONTROLVAR */
EPS=, /* Convergence criterion for stopping iteration, default=0.01 */
MAXITER=, /* Maximum number of iteration, default=25 */
LOWER=, /* Lower bound, must be in (0,1) */
UPPER=, /* Upper bound, must be bigger than 1 or . */
NOINT=, /* Do not keep sum of sampling weights unchanged */
/* Replication parameters */
NOREPWT=, /* Request no replicate weights */
VARMETHOD=, /* BRR | JK | BOOTSTRAP, default is JK */
REPS=, /* Number of replicates for bootstrap or brr */
CLUSTER=, /* Cluster variables */
STRATA=, /* Strata variables */
SEED=, /* Random seed */
FAY=, /* Fay coefficient for BRR varmethod */
RATE=, /* FPC for bootstrap replicate weights */
OUTJKCOEFS=,/* OUTJKCOEFS data set */
REPWEIGHTS= /* Replicate weight variables */
);
/* ********** initilization *********** */
%let switchToExp=0;
%let negtiveCalW=0;
%let lowerBSearchNeeded=0;
%let upperBSearchNeeded=0;
%let maxIterReached=0;
%let success=1;
%let ceateIntercept=1;
%let creatRepWt=1;
%let calRepWt=1;
%let badrep=0;
%let lowestbound=0.001;
ods graphics off;
%if &data eq %then %do;
%put ERROR: No SAS data set is specified.;
%let success=0;
%goto EXIT;
%end;
%if &out eq %then %do;
%put ERROR: You must provide an out= data set name.;
%let success=0;
%goto EXIT;
%end;
%if &weight eq %then %do;
%put ERROR: No weight variable is specified.;
%let success=0;
%goto EXIT;
%end;
%if &controlvar eq %then %do;
%put ERROR: You must provide CONTROLVAR= variables.;
%let success=0;
%goto EXIT;
%end;
%if &ctrltotal eq %then %do;
%put ERROR: You must provide CTRLTOTAL= totals.;
%let success=0;
%goto EXIT;
%end;
%let nctrlvar=%sysfunc(countw(&controlvar, %str( )));
%let nctr=%sysfunc(countw(&ctrltotal, %str( )));
%if &nctrlvar ne &nctr %then %do;
%put ERROR: The number of CONTROLVAR= variables &nctrlvar and the number of CTRLTOTAL= totals &nctr do not match.;
%let success=0;
%goto EXIT;
%end;
%if &calwt eq %then %let calwt=Cal_&weight;
%if &eps eq %then %let eps=0.01;
%if ((&eps<=0) | &eps>=1) %then %do;
%put ERROR: The eps=&eps is outside the range of (0,1).;
%let success=0;
%goto EXIT;
%end;
%if &method eq %then %do;
%let method=NONE;
%end;
%else %do;
%let method=%upcase(&method);
%if ((&method^=LINEAR) & (&method^=EXPONENTIAL) &
(&method^=TRUNLINEAR) & (&method^=LOGIT)
& (&method^=NONE)) %then %do;
%put ERROR: method=&method is invalid.;
%let success=0;
%goto EXIT;
%end;
%end;
%if &norepwt ne %then %do;
%if ((%upcase(&norepwt)^=TRUE) & (%upcase(&norepwt)^=FALSE)) %then %do;
%put ERROR: NOREPWT=&norepwt is invalid macro parameter, it only takes keywords TRUE or FALSE.;
%let creatRepWt=0;
%let success=0;
%goto EXIT;
%end;
%if (%upcase(&norepwt)=TRUE) %then %do;
%let calRepWt=0;
%let creatRepWt=0;
%end;
%end;
%if &maxiter ne %then %do;
%if (&maxiter<2) %then %do;
%put ERROR: MAXITER=&maxiter is too small.;
%let success=0;
%goto EXIT;
%end;
%end;
%if &maxiter eq %then %let maxiter=50;
%let lowerPrivided=0;
%if &lower eq %then %let lower=&lowestbound;
%else %do;
%let lowerPrivided=1;
%if ((&lower>=1) | (&lower<=0)) %then %do;
%put ERROR: The lower=&lower is outside the range of (0,1).;
%let success=0;
%goto EXIT;
%end;
%end;
%if (&lowerPrivided=0 & ((&method=TRUNLINEAR)|(&method=LOGIT)))
%then %let lowerBSearchNeeded=1;
%let upperPrivided=0;
%if &upper eq %then %let upper=.;
%else %do;
%let upperPrivided=1;
%if (&upper<1) %then %do;
%if (&upper ^=. ) %then %do;
%put ERROR: The upper=&upper is smaller than 1.;
%let success=0;
%goto EXIT;
%end;
%end;
%end;
%if (&upperPrivided=0 & ((&method=TRUNLINEAR)|(&method=LOGIT)))
%then %do;
%let upperBSearchNeeded=1;
/* get initial value for the upperbound from data */
ods listing close;
proc surveymeans data=&data min max;
var &weight;
ods output statistics=_weightminmax_;
run;
data _weightminmax_;
set _weightminmax_;
_maxminratio_=max/min;
call symputx('upper', _maxminratio_);
run;
%end;
%if ((&method=NONE)|(&method=LINEAR)|(&method=EXPONENTIAL))
%then %do;
%let lower=1e-4;
%let upper=.;
%end;
%if (%upcase(&noint)=TRUE) %then %let ceateIntercept=0;
%if ((&calRepWt=1)&(&creatRepWt=1)) %then %do;
%if ((&varmethod ne ) & (&repweights eq ))
%then %let varmethod=%upcase(&varmethod);
%if (&varmethod eq ) %then %let varmethod=JK;
%if ((&varmethod^=BRR) & (&varmethod^=JK) &
(&varmethod^=JACKKNIFE) &
(&varmethod^=BOOTSTRAP)) %then %do;
%put ERROR: varmethod=&varmethod is invalid.;
%let creatRepWt=0;
%let success=0;
%goto EXIT;
%end;
%end;
/* remove missing values, creat intercept */
data _obs_&data; set &data;
_obs_=_n_;
data _temp_&data; set _obs_&data;
%do i=1 %to &nctrlvar;
if (missing(%scan(&controlvar, &i))=1) then delete;
%end;
%if (&ceateIntercept=1) %then %do;
_intercept_=1;
%end;
run;
/* get the total weights under clean data */
%if (&ceateIntercept=1) %then %do;
ods listing close;
proc surveymeans data=_temp_&data sumwgt;
var _intercept_;
weight &weight;
ods output statistics=_temp_sumwgt_;
run;
data _temp_sumwgt_; set _temp_sumwgt_;
call symputx('totalSumWt', sumwgt);
run;
/* add intercept to the control */
%let controlvar=%str(_intercept_ &controlvar);
%let ctrltotal=%str(&totalSumWt &ctrltotal);
/* %put contrvars=&controlvar;
%put ctrltotal=&ctrltotal; */
%end;
/* for replication method */
%if (&calRepWt=1) %then %do;
%let repwtPrefix=RepWt_;
%if &repweights ne %then %do;
%let creatRepWt=0;
%let repwtPrefix=&repweights;
%end;
%end;
/* Create replicate weights */
ods listing close;
%if (&creatRepWt=1) %then %do;
%if ((&varmethod=JK)|(&varmethod=JACKKNIFE)) %then %do;
%if (&outjkcoefs ne ) %then %let outjkcoefs=%str(outjkcoefs=&outjkcoefs);
proc surveymeans data=_temp_&data varmethod=jk(&outjkcoefs outweights=_temp_outrepwts);
%end;
%if (&varmethod=BRR) %then %do;
%if (&fay ne ) %then %let fay=%str(fay=&fay);
proc surveymeans data=_temp_&data varmethod=brr(&fay outweights=_temp_outrepwts);
%end;
%if (&varmethod=BOOTSTRAP) %then %do;
%if (&rate ne ) %then %let rate=%str(r=&rate);
%if (&seed ne ) %then %let seed=%str(seed=&seed);
%if (&reps ne ) %then %let reps=%str(reps=&reps);
proc surveymeans data=_temp_&data &rate
varmethod=bootstrap(&reps &seed outweights=_temp_outrepwts);
%end;
%if &strata ne %then %do;
strata &strata;
%end;
%if &cluster ne %then %do;
cluster &cluster;
%end;
weight &weight;
var _obs_;
ods output VarianceEstimation=_temp_varestsummary;
run;
%if ((&syserr ne 0) & (&syserr ne 4)) %then %do;
%let success=0;
%goto EXIT;
%end;
data _temp_varestsummary; set _temp_varestsummary;
if (label1 ^= 'Number of Replicates') then delete;
call symputx('nReps', cValue1);
run;
ods listing close;
data _temp_&data; merge _temp_&data _temp_outrepwts;
run;
%end;
/* user provided replicate weights */
%if ((&creatRepWt=0) & (&calRepWt=1)) %then %do;
proc surveymeans data=_temp_&data varmethod=&varmethod;
var _obs_;
repweights &repweights:;
weight &weight;
ods output VarianceEstimation=_temp_varestsummary;
run;
%if ((&syserr ne 0) & (&syserr ne 4)) %then %do;
%let success=0;
%goto EXIT;
%end;
data _temp_varestsummary; set _temp_varestsummary;
if (label1 ^= 'Number of Replicates') then delete;
call symputx('nReps', cValue1);
run;
%end;
OPTIONS errors=0 MERGENOBY=NOWARN ;
%if ((&method=TRUNLINEAR)|(&method=LOGIT))
%then %do;
%if ((&upperBSearchNeeded=1) & (&lowerBSearchNeeded=1))
%then %goto LU_&method;
%if ((&upperBSearchNeeded=1) & (&lowerBSearchNeeded=0))
%then %goto U_&method;
%if ((&upperBSearchNeeded=0) & (&lowerBSearchNeeded=1))
%then %goto L_&method;
%if ((&upperBSearchNeeded=0) & (&lowerBSearchNeeded=0))
%then %goto &method;
%end;
%else %goto &method;
/* method= is not specified */
%NONE:
proc iml;
switchToExp=0;
negtiveCalW=0;
use _temp_&data;;
read all var {&weight} into w;
read all var {&controlVar} into x;
close _temp_&data;
T={&ctrltotal}`;
wx=w#x;
beta=ginv(X`*wx)*(T-X`*w);
CalW=w+w#(X*beta);
minCalWValue=min(CalW);
*print minCalWValue;
if (minCalWValue<0)then
negtiveCalW=1;
start expodist(v) global(w);
weps = 1e-4;
verybig= weps**(log(weps)-1);
f=0;
do i = 1 to nrow(w);
if (w[i] > weps) then
vdw = v[i]/w[i];
else
vdw = v[i]/weps;
if (vdw > weps) then
fi = 1 + vdw**(log(vdw) - 1);
else
fi = verybig;
f=f+fi;
end;
/* y = (J(1, nrow(w), 1))+ (v/w`)##((log(v/w`)-J(1, nrow(w), 1))); */
return(f);
finish expodist;
/* if have negative weights, use exponential */
if (negtiveCalW=1) then
do;
switchToExp=1;
j=J(nrow(T), 1, 0) ;
constrain=x`||j|| T;
w0 = w;
optn = {0 0};
blc = constrain;
l=&lower; u=.;
lbound=l*w`|| {. .};
ubound=u*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "expodist", w0, optn, blc);
Calw=v`;
minCalWValue=min(CalW);
end;
create _tmpnewwt_ from CalW[colname="&calwt"];
append from CalW;
call symputx('negtiveCalW', negtiveCalW);
call symputx('switchToExp', switchToExp);
quit;
%goto CALREPWTS_LINEAR;
/* end NONE method */
/* LINEAR method */
%LINEAR:
proc iml;
negtiveCalW=0;
use _temp_&data;;
read all var {&weight} into w;
read all var {&controlVar} into x;
close _temp_&data;
T={&ctrltotal}`;
wx=w#x;
beta=ginv(X`*wx)*(T-X`*w);
CalW=w+w#(X*beta);
minCalWValue=min(CalW);
if (minCalWValue<0)then
negtiveCalW=1;
create _tmpnewwt_ from CalW[colname="&calwt"];
append from CalW;
call symputx('negtiveCalW', negtiveCalW);
quit;
%goto CALREPWTS_LINEAR;
/* end of LINEAR method */
/* TRUNLINEAR method */
%TRUNLINEAR:
proc iml;
use _temp_&data;; read all var {&weight} into w;
*print w;
read all var {&controlVar} into x;
*print x;
close _temp_&data;
T={&ctrltotal}`;
*print T;
j=J(nrow(T), 1, 0) ;
constrain=x`||j|| T;
w0 = w;
optn = {0 0};
blc = constrain;
start dist(v) global(w);
y = (v/w`-J(1, nrow(w), 1))`;
f=sum(w#(y##2));
return(f);
finish dist;
start fitwithbound(ll, uu) global(w, optn, constrain);
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w, optn, blc);
return (rc);
finish fitwithbound;
*multiplier=(sqrt(5)-1)/2;
multiplier=0.5;
ll=&lower; uu=&upper;
*print ll uu ;
/* initial */
rc=fitwithbound(ll, uu);
/* if specified both bounds, make it or break it */
if (rc<0) then do;
success=0;
call symputx('success', success);
end;
else do;
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w, optn, blc);
Calw=v`;
create _tmpnewwt_ from CalW[colname="&calwt"];
append from CalW;
end;
%goto CALREPWTS_TRUNLINEAR;
/* end of truncatelinear */
/*U_TRUNLINEAR method needs to search upper bound */
%U_TRUNLINEAR:
proc iml;
use _temp_&data;; read all var {&weight} into w;
*print w;
read all var {&controlVar} into x;
*print x;
close _temp_&data;
T={&ctrltotal}`;
*print T;
j=J(nrow(T), 1, 0) ;
constrain=x`||j|| T;
maxIterReached=0;
w0 = w;
optn = {0 0};
blc = constrain;
start dist(v) global(w);
y = (v/w`-J(1, nrow(w), 1))`;
f=sum(w#(y##2));
return(f);
finish dist;
start fitwithbound(ll, uu) global(w, optn, constrain);
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w, optn, blc);
return (rc);
finish fitwithbound;
*multiplier=(sqrt(5)-1)/2;
multiplier=0.5;
ll=&lower; uu=&upper;
*print "initial ll and uu=" ll uu ;
/* initial */
rc=fitwithbound(ll, uu);
/* only lower bound is specified, search the upper one */
success=1;
**** search for upper bound ****;
**** default bound is l=0 u=max/min ****;
initialL=&lower; initialU=&upper; finalrc=0;
lastuu=uu;
lastrc=rc;
absolutebound=1;
if (rc<=0) then
do;
uu=(1+multiplier)*lastuu;
absolutebound=absolutebound<>lastuu;
end;
if (rc>0) then
uu=multiplier*lastuu<>absolutebound;
continue=1;
iter=0;
do while (continue=1);
rc=fitwithbound(ll, uu);
if (rc<=0) then absolutebound=absolutebound<>uu;
temp=uu;
max=uu<>lastuu;
min=uu><lastuu;
mim=min<>absolutebound;
diff=max-min;
if (rc<0 & lastrc<0) then
uu=(1+multiplier)*max;
/* *(max-absolutebound)*multiplier; */
if (rc>0 & lastrc>0) then
uu=min-multiplier*(min-absolutebound);
if (rc>0 & lastrc<0) then
uu=min+(1-multiplier)*diff;
if (rc<0 & lastrc>0) then
uu=min+multiplier*diff;
/* always go above absolutebound */
uu=uu<>absolutebound;
lastuu=temp;
lastrc=rc;
if (diff<&eps & rc>0) then
do;
uu=lastuu;
continue=0;
end;
if (diff=0 & rc<0) then do;
ll=lastll;
continue=0;
success=0;
end;
iter=iter+1;
*print iter rc lastrc ll uu lastuu min max absolutebound diff;
if (iter>&maxiter & continue=1) then do;
continue=0;
maxIterReached=1;
success=0;
end;
end; /* end of do while (continue=1); for looking for upper bound */
if (success=1) then do;
*print "The lower bound=" ll "Upper bound=" uu;
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w0, optn, blc);
calw=v`;
create _tmpnewwt_ from CalW[colname="&calwt"];
append from CalW;
end;
call symputx('lower', ll);
call symputx('upper', uu);
call symputx('printiter', iter);
call symputx('maxIterReached', maxIterReached);
call symputx('success', success);
%goto CALREPWTS_TRUNLINEAR;
/* end of U_TRUNLINEAR */
/* L_TRUNLINEAR method needs to search lower bound */
%L_TRUNLINEAR:
proc iml;
use _temp_&data;; read all var {&weight} into w;
*print w;
read all var {&controlVar} into x;
*print x;
close _temp_&data;
T={&ctrltotal}`;
*print T;
j=J(nrow(T), 1, 0) ;
constrain=x`||j|| T;
maxIterReached=0;
w0 = w;
optn = {0 0};
blc = constrain;
start dist(v) global(w);
y = (v/w`-J(1, nrow(w), 1))`;
f=sum(w#(y##2));
return(f);
finish dist;
start fitwithbound(ll, uu) global(w, optn, constrain);
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w, optn, blc);
return (rc);
finish fitwithbound;
*multiplier=(sqrt(5)-1)/2;
multiplier=0.5;
ll=&lower; uu=&upper;
*print ll uu ;
/* initial */
rc=fitwithbound(ll, uu);
/* only upper bound is specified, search for a lower one */
success=1;
**** search for lower bound ****;
**** default bound is &lowestbound u=max/min ****;
initialL=&lower; initialU=&upper; finalrc=0;
lastll=ll;
lastrc=rc;
absolutebound=1;
max=ll<>lastll;
min=ll><lastll;
/* since the initial l&lowestbound if LOWER= is not specified,
if we cannot get a solution, then no need to go any
further, just quit */
if (rc<=0) then success=0;
if (rc>0) then ll=lastll+multiplier*(absolutebound-lastll);
continue=1;
iter=0;
do while (continue=1 & success=1);
*print "before the next iteration......";
*print iter rc lastrc min max uu absolutebound;
rc=fitwithbound(ll, uu);
if (rc<=0) then absolutebound=absolutebound><ll;
temp=ll;
max=ll<>lastll;
min=ll><lastll;
*print lastll ll min max;
diff=max-min;
if (rc<0 & lastrc<0) then
ll=&lowestbound+multiplier*(min-&lowestbound);
if (rc>0 & lastrc>0) then
ll=max+multiplier*(absolutebound-max);
if (rc>0 & lastrc<0) then
ll=min+multiplier*diff;
if (rc<0 & lastrc>0) then
ll=min+(1-multiplier)*diff;
/* always go under absolutebound */
ll=ll><absolutebound;
*print "new rc ll min max diff" rc ll min max diff;
lastll=temp;
lastrc=rc;
*print "going to the next loop" rc lastrc lastll ll;
if (diff<&eps & rc>0) then do;
ll=lastll;
continue=0;
end;
if (diff=0 & rc<0) then do;
ll=lastll;
continue=0;
success=0;
end;
iter=iter+1;
if (iter>&maxiter & continue=1) then do;
continue=0;
maxIterReached=1;
success=0;
end;
end; /* end while (continue=1); searching for lower bound */
if (success=1) then do;
*print "The lower bound=" ll "Upper bound=" uu;
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w0, optn, blc);
calw=v`;
create _tmpnewwt_ from CalW[colname="&calwt"];
append from CalW;
end;
call symputx('lower', ll);
call symputx('upper', uu);
call symputx('printiter', iter);
call symputx('maxIterReached', maxIterReached);
call symputx('success', success);
%goto CALREPWTS_TRUNLINEAR;
/* end of L_TRUNLINEAR */
/* need to search both upper and lower bounds */
%LU_TRUNLINEAR:
proc iml;
use _temp_&data;; read all var {&weight} into w;
*print w;
read all var {&controlVar} into x;
*print x;
close _temp_&data;
T={&ctrltotal}`;
*print T;
j=J(nrow(T), 1, 0) ;
constrain=x`||j|| T;
w0 = w;
optn = {0 0};
blc = constrain;
maxIterReached=0;
start dist(v) global(w);
y = (v/w`-J(1, nrow(w), 1))`;
f=sum(w#(y##2));
return(f);
finish dist;
start fitwithbound(ll, uu) global(w, optn, constrain);
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w, optn, blc);
return (rc);
finish fitwithbound;
*multiplier=(sqrt(5)-1)/2;
multiplier=0.5;
ll=&lower; uu=&upper;
*print ll uu ;
/* initial */
rc=fitwithbound(ll, uu);
success=1;
/* search the upper bound first */
**** search for upper bound ****;
**** default bound is l=0 u=max/min ****;
initialL=&lower; initialU=&upper; finalrc=0;
lastuu=uu;
lastrc=rc;
absolutebound=1;
if (rc<=0) then
do;
uu=(1+multiplier)*lastuu;
absolutebound=absolutebound<>lastuu;
end;
if (rc>0) then
do;
uu=multiplier*lastuu;
end;
continue=1;
iter=0;
do while (continue=1);
rc=fitwithbound(ll, uu);
if (rc<=0) then do;
absolutebound=absolutebound<>uu;
end;
temp=uu;
max=uu<>lastuu;
min=uu><lastuu;
diff=max-min;
if (rc<0 & lastrc<0) then
uu=(1+multiplier)*max;
/* max+(max-absolutebound)*multiplier; */
if (rc>0 & lastrc>0) then
uu=min-multiplier*(min-absolutebound);
if (rc>0 & lastrc<0) then
uu=min+(1-multiplier)*diff;
if (rc<0 & lastrc>0) then
uu=min+multiplier*diff;
/* always go above absolutebound */
uu=uu<>absolutebound;
lastuu=temp;
lastrc=rc;
*print iter rc lastrc ll uu diff;
if (abs(diff)<&eps & rc>0) then
do;
uu=lastuu;
continue=0;
end;
iter=iter+1;
*print continue iter;
if (iter>&maxiter & continue=1) then do;
continue=0;
maxIterReached=1;
success=0;
end;
end; /* end of do while (continue=1); looking for upper bound */
/* now the upper bound has been set for default lower=0 */
/* now search for a lower bound */
printiterl=iter;
if (success=1) then do;
**** search for lower bound ****;
lastll=ll;
lastrc=rc;
absolutebound=1;
ll=lastll+multiplier*(absolutebound-lastll); /* lastll should be 0.01 and rc>0 */
continue=1;
do while (continue=1);
rc=fitwithbound(ll, uu);
if (rc<=0) then absolutebound=absolutebound><ll;
temp=ll;
max=ll<>lastll;
min=ll><lastll;
diff=max-min;
if (rc<0 & lastrc<0) then
ll=&lowestbound+multiplier*(min-&lowestbound);
if (rc>0 & lastrc>0) then
ll=max+multiplier*(absolutebound-max);
if (rc>0 & lastrc<0) then
ll=min+multiplier*diff;
if (rc<0 & lastrc>0) then
ll=min+(1-multiplier)*diff;
/* always go under absolutebound */
ll=ll><absolutebound;
lastll=temp;
lastrc=rc;
*print iter rc lastrc ll uu diff;
if (diff<&eps & rc>0) then do;
ll=lastll;
continue=0;
end;
if (diff=0 & rc<0) then do;
ll=lastll;
continue=0;
success=0;
end;
iter=iter+1;
if (iter>&maxiter+printiterl & continue=1) then do;
/* since lower=0 is okay, this almost never happen */
continue=0;
maxIterReached=1;
success=0;
end;
end; /* end while (continue=1); searching for lower bound */
end; /* end of search lower end */
if (success=1) then do;
*print "The lower bound=" ll "Upper bound=" uu;
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w0, optn, blc);
calw=v`;
create _tmpnewwt_ from CalW[colname="&calwt"];
append from CalW;
end;
call symputx('lower', ll);
call symputx('upper', uu);
call symputx('printiter', iter);
call symputx('maxIterReached', maxIterReached);
call symputx('success', success);
%goto CALREPWTS_TRUNLINEAR;
/* end of LU_TRUNLINEAR */
/* EXPONENTIAL method */
%EXPONENTIAL:
proc iml;
use _temp_&data;;
read all var {&weight} into w;
read all var {&controlVar} into x;
close _temp_&data;
T={&ctrltotal}`;
j=J(nrow(T), 1, 0) ;
constrain=x`||j|| T;
w0 = w;
optn = {0 0};
blc = constrain;
start dist(v) global(w);
weps = 1e-4;
verybig= weps**(log(weps)-1);
f=0;
do i = 1 to nrow(w);
if (w[i] > weps) then
vdw = v[i]/w[i];
else
vdw = v[i]/weps;
if (vdw > weps) then
fi = 1 + vdw**(log(vdw) - 1);
else
fi = verybig;
f=f+fi;
end;
/* y = (J(1, nrow(w), 1))+ (v/w`)##((log(v/w`)-J(1, nrow(w), 1)));
*print y;
f=sum(y); */
return(f);
finish dist;
l=&lower; u=&upper;
lbound=l*w`|| {. .};
ubound=u*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w0, optn, blc);
Calw=v`;
create _tmpnewwt_ from CalW[colname="&calwt"];
append from CalW;
%goto CALREPWTS_LOGIT;
/* end of EXPONENTIAL method */
/* LOGIT method */
%LOGIT:
proc iml;
use _temp_&data;;
read all var {&weight} into w;
read all var {&controlVar} into x;
close _temp_&data;
T={&ctrltotal}`;
j=J(nrow(T), 1, 0) ;
constrain=x`||j|| T;
optn = {0 0};
blc = constrain;
start dist(v) global(w);
weps = 1e-4;
verybig= weps**(log(weps)-1);
f=0;
do i = 1 to nrow(w);
if (w[i] > weps) then
vdw = v[i]/w[i];
else
vdw = v[i]/weps;
if (vdw > weps) then
fi = 1 + vdw**(log(vdw) - 1);
else
fi = verybig;
f=f+fi;
end;
/*y = (J(1, nrow(w), 1))+ (v/w`)##((log(v/w`)-J(1, nrow(w), 1)));
*print y;
f=sum(y); */
return(f);
finish dist;
w0=w;
success=1;
ll=&lower; uu=&upper;
lbound=ll*w`|| {. .};
ubound=uu*w`|| {. .};
bound=lbound//ubound;
blc=bound//constrain;
call nlpqn(rc, v, "dist", w0, optn, blc);
*print "for LOGIT method" ll uu rc;
if (rc<0) then success=0;
if (success=1) then do;
Calw=v`;
create _tmpnewwt_ from CalW[colname="&calwt"];
append from CalW;
end;
call symputx('lower', ll);
call symputx('upper', uu);
call symputx('success', success);
%goto CALREPWTS_LOGIT;
/* end of LOGIT method */
/* L_LOGIT method need to search lower bound */
%L_LOGIT:
proc iml;
use _temp_&data;;