-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgom_cod_2021.v1.sas
1295 lines (1162 loc) · 30.5 KB
/
gom_cod_2021.v1.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
*******************************************************************
Custom weight trimming and domain estimation program
for use with MRIP public-use datasets (trip,catch,size)
This program applies two general approaches to sample weight trimming
for estimating landings and length frequencies under two scenarios:
Scenarios:
1) Landings (no.) estimate fixed
2) Landings (no.) estimate allowed to change
Trimming Methods:
a.) Empirical MSE trimming
b.) Two NAEP median/percentile trimming methods
- 99th percentile with trimmed weight redistribution (99)
- 95th percentile with trimmed weight redistribution (95)
Results (1a.,1b.,2a.,2b.) are compiled into the landings_compare and
lfreq_compare datasets in work library
v1 developed for request related to 2020w3-2021w2 Atlantic Cod
landings and length frequency estimates for Gulf of Maine domain (10/2021)
jfoster
*******************************************************************;
***************************************
From John Foster to Scott Steinback October 18, 2021.
We are using 2b p95
****************************************;
*Location MRIP public-use datasets: trip, catch, size;
libname pub "C:\Users\john.foster\Desktop\SSC prep\pub";
*proportion of largest weight that will be trimmed and
redistributed to other within-domian observations in
each loop pass;
%let trim_step=0.01;
*Path for dataset and pdf output with comparison figures;
%let pdf_path=C:\Users\john.foster\Desktop\GOM Cod\;
libname out "&pdf_path.";
%macro dat_in;
data trip;
set
%do y=2020 %to 2020;
%do w=1 %to 6;
pub.trip_&y.&w.
%end;
%end;
%do y=2021 %to 2021;
%do w=1 %to 3;
pub.trip_&y.&w.
%end;
%end;
;
run;
data catch;
set
%do y=2020 %to 2020;
%do w=1 %to 6;
pub.catch_&y.&w.
%end;
%end;
%do y=2021 %to 2021;
%do w=1 %to 3;
pub.catch_&y.&w.
%end;
%end;
;
run;
data size;
set
%do y=2020 %to 2020;
%do w=1 %to 6;
pub.size_&y.&w.
%end;
%end;
%do y=2021 %to 2021;
%do w=1 %to 3;
pub.size_&y.&w.
%end;
%end;
;
run;
%mend dat_in;
%dat_in
*This definition will need to be updated before final run - jf 10.12.2021;
%macro domain_def;
length area_s $4.;
if sub_reg in (5 6 7) or st in (9 44) then area_s='GBS';
if st in (23 33) then area_s='GOM';
if st=25 then do;
area_s='GBS';
if 3<=intsite<=5 then area_s='GOM';
if 8<=intsite<=313 then area_s='GOM';
if 318<=intsite<=326 then area_s='GOM';
if intsite=336 then area_s='GOM';
if 346<=intsite<=359 then area_s='GOM';
if intsite=388 then area_s='GOM';
if intsite=399 then area_s='GOM';
if 426<=intsite<=427 then area_s='GOM';
if intsite=447 then area_s='GOM';
if intsite=449 then area_s='GOM';
if 495<=intsite<=499 then area_s='GOM';
if 555<=intsite<=567 then area_s='GOM';
if 606<=intsite<=607 then area_s='GOM';
if 642<=intsite<=666 then area_s='GOM';
if 668<=intsite<=685 then area_s='GOM';
if 697<=intsite<=706 then area_s='GOM';
if intsite=712 then area_s='GOM';
if 719<=intsite<=723 then area_s='GOM';
if 730<=intsite<=734 then area_s='GOM';
if 737<=intsite<=740 then area_s='GOM';
if intsite=757 then area_s='GOM';
if 760<=intsite<=763 then area_s='GOM';
if 769<=intsite<=770 then area_s='GOM';
if 781<=intsite<=782 then area_s='GOM';
if intsite=784 then area_s='GOM';
if 788<=intsite<=791 then area_s='GOM';
if 793<=intsite<=808 then area_s='GOM';
if 823<=intsite<=829 then area_s='GOM';
if intsite=834 then area_s='GOM';
if 838<=intsite<=846 then area_s='GOM';
if 848<=intsite<=850 then area_s='GOM';
if 853<=intsite<=864 then area_s='GOM';
if 866<=intsite<=876 then area_s='GOM';
if 878<=intsite<=884 then area_s='GOM';
if 888<=intsite<=890 then area_s='GOM';
if 909<=intsite<=918 then area_s='GOM';
if intsite=924 then area_s='GOM';
if intsite=956 then area_s='GOM';
if intsite=1210 then area_s='GOM';
if intsite=1214 then area_s='GOM';
if intsite=1327 then area_s='GOM';
if intsite=1349 then area_s='GOM';
if 1484<=intsite<=1505 then area_s='GOM';
if 1656<=intsite<=1721 then area_s='GOM';
if intsite=1799 then area_s='GOM';
if intsite=1807 then area_s='GOM';
if intsite=1825 then area_s='GOM';
if intsite=1830 then area_s='GOM';
if intsite=1854 then area_s='GOM';
if intsite=3122 then area_s='GOM';
if intsite=3125 then area_s='GOM';
if 3226<=intsite<=3227 then area_s='GOM';
if intsite=3230 then area_s='GOM';
end;
%mend domain_def;
data trip;
set trip;
%domain_def;
run;
proc sort data=trip;
by year wave strat_id psu_id id_code;
run;
proc sort data=catch;
by year wave strat_id psu_id id_code;
run;
proc sort data=size;
by year wave strat_id psu_id id_code;
run;
data catch;
merge catch(in=c) trip(keep=year wave strat_id psu_id id_code area_s);
by year wave strat_id psu_id id_code;
if c;
run;
data size;
merge size(in=s) trip(keep=year wave strat_id psu_id id_code area_s);
by year wave strat_id psu_id id_code;
if s;
run;
data size;
set size;
lngth_cm=floor(lngth/10);
dom1=0;
landing=1;
if (year=2020 and wave>2) and common="ATLANTIC COD" and area_s="GOM" then dom1=1;
if (year=2021 and wave<3) and common="ATLANTIC COD" and area_s="GOM" then dom1=1;
run;
data catch;
set catch;
dom1=0;
if (year=2020 and wave>2) and common="ATLANTIC COD" and area_s="GOM" then dom1=1;
if (year=2021 and wave<3) and common="ATLANTIC COD" and area_s="GOM" then dom1=1;
run;
data catch2;
set catch;
*subsetting to fishing year of interest and sub_reg 4,5 - aligns with design strata;
if (year=2020 and wave>2) or (year=2021 and wave<3) and
sub_reg in (4 5);
run;
ods graphics off;
proc surveymeans data=catch2 sum varsum cvsum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_catch;
domain dom1;
var landing wgt_ab1;
ods output domain=dom1_domain_catch;
run;
data size2;
set size;
*subsetting to fishing year of interest and sub_reg 4,5 - aligns with design strata;
if (year=2020 and wave>2) or (year=2021 and wave<3) and
sub_reg in (4 5);
run;
ods graphics off;
proc surveymeans data=size2 sum varsum cvsum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_size;
domain dom1;
var landing wgt;
ods output domain=dom1_domain;
run;
******************************************
1. Weight trimming methods keeping the
A+B1 landings (no.) estimate constant
******************************************;
*1.a. Empirical MSE approach to weight trimming
this method considers the trade-off between
precision gain and increased estimated bias as
outlier weights are trimmed incrementally.
trimming stops when eMSE is minimized;
proc sql noprint;
select sum into: sum_start
from dom1_domain
where dom1=1
;
select varsum into: var_start
from dom1_domain
where dom1=1
;
select cvsum into: cv_start
from dom1_domain
where dom1=1
;
quit;
%let bias_start=0;
%put &sum_start. &var_start. &cv_start. &bias_start.;
%let eMSE=%eval(&var_start.+%sysfunc(abs(&bias_start.**2)));
%put Start eMSE=&eMSE.;
%macro trim_loop;
%let loop_flag=0;
data size2_tmp;
set size2;
run;
%let loop_i=1;
%do %while(&loop_flag=0);
%let loop_flag=1;
proc sql;
create table size2_tmp as
select *,max(wp_size) as wp_max
from size2_tmp
group by dom1
;
quit;
data size2_tmp;
set size2_tmp;
max_w=0;
nmax_w=1;
trim_wp=0;
if dom1=1 and wp_size=wp_max then do;
max_w=1;
nmax_w=0;
wp_size_pre=wp_size;
wp_size=wp_size*(1-&trim_step.);
trim_wp=wp_size_pre-wp_size;
end;
run;
proc sql;
create table size2_tmp as
select *,sum(trim_wp) as sum_trim
,sum(nmax_w) as sum_nmax
from size2_tmp
group by dom1
;
quit;
data size2_tmp;
set size2_tmp;
if dom1=1 and nmax_w=1 then do;
wp_size_pre=wp_size;
wp_size=wp_size + (sum_trim/sum_nmax);
end;
*need to drop all the sql calculated fields;
drop wp_max sum_trim sum_nmax;
run;
ods graphics off;
proc surveymeans data=size2_tmp sum varsum cvsum plots=(none);
strata strat_id;
cluster psu_id;
weight wp_size;
domain dom1;
var wgt;
ods output domain=dom1_domain_tmp;
run;
proc sql noprint;
select sum into: sum_tmp
from dom1_domain_tmp
where dom1=1
;
select varsum into: var_tmp
from dom1_domain_tmp
where dom1=1
;
select cvsum into: cv_tmp
from dom1_domain_tmp
where dom1=1
;
quit;
%let bias=%eval(&sum_start.-&sum_tmp.);
%put bias=&bias.;
%if &loop_i.=1 %then %do;
%let eMSE_tmp=%eval(&var_tmp.+%sysfunc(abs(&bias.**2)));
%put &eMSE. &eMSE_tmp.;
%if &eMSE_tmp.>&eMSE. %then %do;
%put Loop Iteration &loop_i.;
%put Trimming did not improve eMSE;
%put Stopping trimming loop;
%let loop_flag=1;
%end;
%if &eMSE_tmp.<&eMSE. %then %do;
%put Loop Iteration &loop_i.;
%put Trimming improved eMSE;
%put Continuing trimming loop;
%let loop_flag=0;
%let sum_tmp_pre=&sum_tmp.;
%let var_tmp_pre=&var_tmp.;
%let cv_tmp_pre=&cv_tmp.;
%let eMSE_tmp_pre=&eMSE_tmp.;
%end;
%end;
%if &loop_i.>1 %then %do;
%let eMSE_tmp=%eval(&var_tmp.+%sysfunc(abs(&bias.**2)));
%put &eMSE_tmp_pre. &eMSE_tmp.;
%if &eMSE_tmp.>&eMSE_tmp_pre. %then %do;
%put Loop Iteration &loop_i.;
%put Trimming did not improve eMSE;
%put Stopping trimming loop;
%let loop_flag=1;
%end;
%if &eMSE_tmp.<&eMSE_tmp_pre. %then %do;
%put Loop Iteration &loop_i.;
%put Trimming improved eMSE;
%put Continuing trimming loop;
%let loop_flag=0;
%let sum_tmp_pre=&sum_tmp.;
%let var_tmp_pre=&var_tmp.;
%let cv_tmp_pre=&cv_tmp.;
%let eMSE_tmp_pre=&eMSE_tmp.;
%end;
%end;
%let loop_i=%eval(&loop_i.+1);
%if &loop_i.=10 %then %do;
%let loop_flag=1;
%end;
%end;
data size2_trim;
set size2_tmp;
wp_size_trim=wp_size;
if wp_size_pre^=. then wp_size_trim=wp_size_pre;
run;
%mend trim_loop;
%trim_loop;
*calculating landings (no., kg) and length frequencies using the new weights;
ods graphics off;
proc surveymeans data=size2_trim sum cvsum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_size_trim;
domain dom1;
var landing wgt;
ods output domain=landings_est_1a;
run;
ods graphics off;
proc surveymeans data=size2_trim mean sum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_size_trim;
domain dom1;
class lngth_cm;
var lngth_cm;
ods output domain=lfreq_est_1a;
run;
*1.b. Simpler NAEP style trimming approach
Variations of this method are used by the
National Assessment of Educational Progress (NAEP) program
run by the National Center for Education Statistics
;
proc sort data=size2;
by dom1;
run;
proc univariate data=size2;
by dom1;
var wp_size;
output out=wp_size_pct max=wp_max p99=wp_p99 p95=wp_p95
p90=wp_p90 median=wp_p50;
run;
data wp_size_pct;
set wp_size_pct;
if dom1=1;
run;
data size2_naep;
merge size2 wp_size_pct;
by dom1;
run;
data size2_naep;
set size2_naep;
*trimming at two levels: weights over 99th or 95th percentiles;
p99_flag=1;
p95_flag=1;
trim_99=0;
trim_95=0;
if dom1=1 then do;
if wp_size>wp_p99 and wp_size>10*wp_p50 then do;
p99_flag=0;
wp_size_99=wp_p99;
trim_99 = wp_size - wp_p99;
end;
if wp_size>wp_p95 and wp_size>10*wp_p50 then do;
p95_flag=0;
wp_size_95=wp_p95;
trim_95 = wp_size - wp_p95;
end;
end;
run;
proc sql;
create table size2_naep as
select *,sum(trim_99) as sum_trim_99
,sum(trim_95) as sum_trim_95
,sum(p99_flag) as sum_p99_flag
,sum(p95_flag) as sum_p95_flag
,sum(wp_size) as sum_wp_size
from size2_naep
group by dom1
;
quit;
data size2_naep;
set size2_naep;
if dom1=1 then do;
*the trimmed weight from the outlier weights is redistributed to the other records
in the wp_size_99 and wp_size_95 fields.;
if p99_flag=1 then do;
wp_size_99 = min(wp_size + (sum_trim_99/sum_p99_flag),wp_p99);
end;
if p95_flag=1 then do;
wp_size_95 = min(wp_size + (sum_trim_95/sum_p95_flag),wp_p95);
end;
end;
run;
proc sql;
create table size2_naep as
select * ,sum(wp_size_99) as sum_wp_size_99
,sum(wp_size_95) as sum_wp_size_95
from size2_naep
group by dom1
;
quit;
*This step is calibrating the total of the new weights (after trimming and redistribution) to the original totals;
data size2_naep;
set size2_naep;
if dom1=1 then do;
wp_size_99 = wp_size_99 * (sum_wp_size/sum_wp_size_99);
wp_size_95= wp_size_95 * (sum_wp_size/sum_wp_size_95);
end;
run;
*Calculating landings (no.,kg) and length frequencies using the new sample weights;
ods graphics off;
proc surveymeans data=size2_naep sum cvsum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_size_99;
domain dom1;
var landing wgt;
ods output domain=landings_est_1b_99;
run;
ods graphics off;
proc surveymeans data=size2_naep mean sum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_size_99;
domain dom1;
class lngth_cm;
var lngth_cm;
ods output domain=lfreq_est_1b_99;
run;
ods graphics off;
proc surveymeans data=size2_naep sum cvsum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_size_95;
domain dom1;
var landing wgt;
ods output domain=landings_est_1b_95;
run;
ods graphics off;
proc surveymeans data=size2_naep mean sum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_size_95;
domain dom1;
class lngth_cm;
var lngth_cm;
ods output domain=lfreq_est_1b_95;
run;
******************************************
2. Methods that allow both the A+B1
landings in numbers and weight to vary
******************************************;
*2.a. Empirical MSE approach to applied to landings
in numbers;
*creating new catch dataset with only one record per unique angler-trip (id_code).
need recode and deduplicate for trips with catch from multiple species and trips with no
cod catch;
data catch2;
set catch;
*subsetting to fishing year of interest and sub_reg 4,5 - aligns with design strata;
if (year=2020 and wave>2) or (year=2021 and wave<3) and
sub_reg in (4 5);
if common^="ATLANTIC COD" then do;
common="ZZZZZZZZZZ";
landing=0;
wgt_ab1=0;
end;
run;
proc sort data=catch2;
by strat_id psu_id id_code common;
run;
proc sort data=catch2 nodupkey;
by strat_id psu_id id_code;
run;
data catch2;
set catch2;
dom1_c=0;
if (year=2020 and wave>2) and common="ATLANTIC COD" and area_s="GOM" and mode_fx in ("7" "4" "5") then dom1_c=1;
if (year=2021 and wave<3) and common="ATLANTIC COD" and area_s="GOM" and mode_fx in ("7" "4" "5") then dom1_c=1;
run;
ods graphics off;
proc surveymeans data=catch2 sum varsum cvsum plots=(none);
strata strat_id;
cluster psu_id;
weight wp_catch;
domain dom1_c;
var landing wgt_ab1;
ods output domain=dom1_domain_catch;
run;
%let catch_var=wgt_ab1; *can use landing or wgt_ab1;
data dom1_domain_catch;
set dom1_domain_catch;
if dom1_c=1;
if varname="&catch_var.";
run;
proc sql noprint;
select sum into: sum_start
from dom1_domain_catch
where dom1_c=1
;
select varsum into: var_start
from dom1_domain_catch
where dom1_c=1
;
select cvsum into: cv_start
from dom1_domain_catch
where dom1_c=1
;
quit;
%let bias_start=0;
%put &sum_start. &var_start. &cv_start. &bias_start.;
%let eMSE=%eval(&var_start.+%sysfunc(abs(&bias_start.**2)));
%put Start eMSE=&eMSE.;
%macro trim_loop_c;
%let loop_flag=0;
data catch2_tmp;
set catch2;
run;
%let loop_i=1;
%do %while(&loop_flag=0);
%let loop_flag=1;
proc sql;
create table catch2_tmp as
select *,max(wp_catch) as wp_max
from catch2_tmp
group by dom1_c
;
quit;
data catch2_tmp;
set catch2_tmp;
max_w=0;
nmax_w=1;
trim_wp=0;
if dom1_c=1 and wp_catch=wp_max then do;
max_w=1;
nmax_w=0;
wp_catch_pre=wp_catch;
wp_catch=wp_catch*(1-&trim_step.);
trim_wp=wp_catch_pre-wp_catch;
end;
run;
proc sql;
create table catch2_tmp as
select *,sum(trim_wp) as sum_trim
,sum(nmax_w) as sum_nmax
from catch2_tmp
group by dom1_c
;
quit;
data catch2_tmp;
set catch2_tmp;
if dom1_c=1 and nmax_w=1 then do;
wp_catch_pre=wp_catch;
wp_catch=wp_catch + (sum_trim/sum_nmax);
end;
*need to drop all the sql calculated fields;
drop wp_max sum_trim sum_nmax;
run;
ods graphics off;
proc surveymeans data=catch2_tmp sum varsum cvsum plots=(none);
strata strat_id;
cluster psu_id;
weight wp_catch;
domain dom1_c;
var &catch_var.;
ods output domain=dom1_domain_catch_tmp;
run;
proc sql noprint;
select sum into: sum_tmp
from dom1_domain_catch_tmp
where dom1_c=1
;
select varsum into: var_tmp
from dom1_domain_catch_tmp
where dom1_c=1
;
select cvsum into: cv_tmp
from dom1_domain_catch_tmp
where dom1_c=1
;
quit;
%let bias=%eval(&sum_start.-&sum_tmp.);
%put Running on &catch_var.;
%if &loop_i.=1 %then %do;
%let eMSE_tmp=%eval(&var_tmp.+%sysfunc(abs(&bias.**2)));
%put &eMSE. &eMSE_tmp.;
%if &eMSE_tmp.>&eMSE. %then %do;
%put Loop Iteration &loop_i.;
%put Trimming did not improve eMSE;
%put Stopping trimming loop;
%let loop_flag=1;
%end;
%if &eMSE_tmp.<&eMSE. %then %do;
%put Loop Iteration &loop_i.;
%put Trimming improved eMSE;
%put Continuing trimming loop;
%let loop_flag=0;
%let sum_tmp_pre=&sum_tmp.;
%let var_tmp_pre=&var_tmp.;
%let cv_tmp_pre=&cv_tmp.;
%let eMSE_tmp_pre=&eMSE_tmp.;
%end;
%end;
%if &loop_i.>1 %then %do;
%let eMSE_tmp=%eval(&var_tmp.+%sysfunc(abs(&bias.**2)));
%put &eMSE_tmp_pre. &eMSE_tmp.;
%if &eMSE_tmp.>&eMSE_tmp_pre. %then %do;
%put Loop Iteration &loop_i.;
%put Trimming did not improve eMSE;
%put Stopping trimming loop;
%let loop_flag=1;
%end;
%if &eMSE_tmp.<&eMSE_tmp_pre. %then %do;
%put Loop Iteration &loop_i.;
%put Trimming improved eMSE;
%put Continuing trimming loop;
%let loop_flag=0;
%let sum_tmp_pre=&sum_tmp.;
%let var_tmp_pre=&var_tmp.;
%let cv_tmp_pre=&cv_tmp.;
%let eMSE_tmp_pre=&eMSE_tmp.;
%end;
%end;
%let loop_i=%eval(&loop_i.+1);
%end;
data catch2_trim;
set catch2_tmp;
wp_catch_trim=wp_catch;
if wp_catch_pre^=. then wp_catch_trim=wp_catch_pre;
run;
%mend trim_loop_c;
%trim_loop_c;
ods graphics off;
proc surveymeans data=catch2_trim sum cvsum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_catch_trim;
domain dom1_c;
var landing wgt_ab1;
ods output domain=new_catch_domain;
run;
data landings_est_2a;
set new_catch_domain;
run;
data new_catch_domain;
set new_catch_domain;
if dom1_c=1;
if varname="landing";
rename sum=landing;
run;
*now updating weights in size dataset to calculate length frequencies;
proc sort data=catch2_trim;
by strat_id psu_id id_code;
run;
proc sort data=size2;
by strat_id psu_id id_code;
run;
data size2_ctrim;
merge size2(in=s)
catch2_trim(keep=strat_id psu_id id_code wp_catch_trim dom1_c)
;
by strat_id psu_id id_code;
if s;
run;
proc sort data=size2_ctrim;
by dom1_c;
run;
data size2_ctrim;
merge size2_ctrim(in=s drop=landing)
new_catch_domain(keep=dom1_c landing);
by dom1_c;
if s;
run;
data size2_ctrim;
set size2_ctrim;
ac_flag=0;
if common="ATLANTIC COD" then ac_flag=1;
run;
proc sql;
create table size2_ctrim as
select * ,sum(wp_catch_trim*ac_flag) as landing_pre
from size2_ctrim
group by dom1_c
;
quit;
data size2_ctrim;
set size2_ctrim;
wp_size_ctrim = wp_catch_trim * (landing/landing_pre);
run;
ods graphics off;
proc surveymeans data=size2_ctrim mean sum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_size_ctrim;
domain common*dom1_c;
class lngth_cm;
var lngth_cm;
ods output domain=lfreq_est_2a;
run;
*********************************************************************************************************
*************************This is the one we want ********************************************************;
*2.b. Simpler NAEP style trimming approach
Applying method to wp_catch weights in catch datasets first.
Then recalculating wp_size weights in size datasets using
results of trimmed wp_catch weights.
;
proc sort data=catch2;
by dom1;
run;
proc univariate data=catch2;
by dom1;
var wp_catch;
output out=wp_catch_pct max=wp_max p99=wp_p99 p95=wp_p95
p90=wp_p90 median=wp_p50;
run;
data wp_catch_pct;
set wp_catch_pct;
if dom1=1;
run;
data catch2_naep;
merge catch2 wp_catch_pct;
by dom1;
run;
data catch2_naep;
set catch2_naep;
p99_flag=1;
p95_flag=1;
trim_99=0;
trim_95=0;
if dom1=1 then do;
*the trimmed weight from the outlier weights is redistributed to the other records
in the wp_size_99 and wp_size_95 fields.
the trimmed weight is not redistributed in the 99a and 95a fields.;
if wp_catch>wp_p99 and wp_catch>10*wp_p50 then do;
p99_flag=0;
wp_catch_99=wp_p99;
trim_99 = wp_catch - wp_p99;
end;
if wp_catch>wp_p95 and wp_catch>10*wp_p50 then do;
p95_flag=0;
wp_catch_95=wp_p95;
trim_95 = wp_catch - wp_p95;
end;
end;
run;
proc sql;
create table catch2_naep as
select *,sum(trim_99) as sum_trim_99
,sum(trim_95) as sum_trim_95
,sum(p99_flag) as sum_p99_flag
,sum(p95_flag) as sum_p95_flag
,sum(wp_catch) as sum_wp_catch
from catch2_naep
group by dom1
;
quit;
data catch2_naep;
set catch2_naep;
if dom1=1 then do;
if p99_flag=1 then do;
wp_catch_99 = min(wp_catch + (sum_trim_99/sum_p99_flag),wp_p99);
end;
if p95_flag=1 then do;
wp_catch_95 = min(wp_catch + (sum_trim_95/sum_p95_flag),wp_p95);
end;
end;
run;
proc sql;
create table catch2_naep as
select * ,sum(wp_catch_99) as sum_wp_catch_99
,sum(wp_catch_95) as sum_wp_catch_95
from catch2_naep
group by dom1
;
quit;
data catch2_naep;
set catch2_naep;
if dom1=1 then do;
*calibrating new weight sums to original across all records in
domain;
wp_catch_99 = wp_catch_99 * (sum_wp_catch/sum_wp_catch_99);
wp_catch_95= wp_catch_95 * (sum_wp_catch/sum_wp_catch_95);
end;
run;
ods graphics off;
proc surveymeans data=catch2_naep sum cvsum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_catch_99;
domain common*dom1;
var landing wgt_ab1;
ods output domain=cnaep99_est;
run;
ods graphics off;
proc surveymeans data=catch2_naep sum cvsum plots=(none);
where (year=2020 and wave>2) or (year=2021 and wave<3);
strata strat_id;
cluster psu_id;
weight wp_catch_95;
domain common*dom1;
var landing wgt_ab1;
ods output domain=cnaep95_est;
run;
data landings_est_2b;
length trim_series $35.;
set cnaep99_est(in=c1)
cnaep95_est(in=c2);
if c1 then trim_series="p99 ";
if c2 then trim_series="p95 ";
run;
data cnaep_est;
length trim_series $35.;
set cnaep99_est(in=c1)
cnaep95_est(in=c2);
if c1 then trim_series="p99 ";
if c2 then trim_series="p95 ";
if varname="landing";
rename sum=landing;
drop domainlabel varlabel cvsum stddev varname;
run;
proc transpose data=cnaep_est out=trans_cnaep_est prefix=landing_;
by common dom1;
id trim_series;
run;