/* SAS-callable SUDAAN Code */ 

libname in 'XXX';
data d; set in.or_mjjks; 
u_id = unique_i; 

/* Create some variables */ 

if a1 > 0 or age > 0 or a8 > 0; * a1= sex. a8 =education; 

if source2 = . then source2 = 2;

if a8 = 9 then a8 = .; 

if source2 >= 2 then abs = 1; 
if source2 = 3 then internet = 0; else internet = 1; 

class = a1*100 + a8*10 + age;

/* Creates Response/nonresponse variables */

IF 0 < B1 < 5 then do; B1_P = 1; END; else do B1 = .; B1_P = 0; END; 
IF 0 < B2 < 5 then do; B2_P = 1; END; else do B2 = .; B2_P = 0; END; 
IF 0 < B3 < 5 then do; B3_P = 1; END; else do B3 = .; B3_P = 0; END; 
IF 0 < B4 < 5 then do; B4_P = 1; END; else do B4 = .; B4_P = 0; END;
IF 0 < B5 < 5 then do; B5_P = 1; END; else do B5 = .; B5_P = 0; END; 

IF 0 < C2 < 4 then do; C2_P = 1; END; else do C2 = .; C2_P = 0; END; 
IF 0 < C3 < 4 then do; C3_P = 1; END; else do C3 = .; C3_P = 0; END; 
IF 0 < C4 < 4 then do; C4_P = 1; END; else do C4 = .; C4_P = 0; END; 
IF 0 < C5 < 4 then do; C5_P = 1; END; else do C5 = .; C5_P = 0; END; 
IF 0 < C6 < 5 then do; C6_P = 1; END; else do C6 = .; C6_P = 0; END; 
IF 0 < C7 < 5 then do; C7_P = 1; END; else do C7 = .; C7_P = 0; END; 
IF 0 < C8 < 5 then do; C8_P = 1; END; else do C8 = .; C8_P = 0; END;
IF 0 < C9 < 5 then do; C9_P = 1; END; else do C9 = .; C9_P = 0; END;
IF 0 < C10 < 4 then do; C10_P = 1; END; else do C10 = .; C10_P = 0; END; 
 
IF 0 < E4 < 5 then do; E4_P = 1; END; else do E4 = .; E4_P = 0; END; 
IF 0 < E5 < 5 then do; E5_P = 1; END; else do E5 = .; E5_P = 0; END; 
IF 0 < E6 < 3 then do; E6_P = 1; END; else do E6 = .; E6_P = 0; END; 
IF 0 < E7 < 3 then do; E7_P = 1; END; else do E7 = .; E7_P = 0; END; 
IF 0 < E8 < 3 then do; E8_P = 1; END; else do E8 = .; E8_P = 0; END;

IF 0 < F2 < 3 then do; F2_P = 1; END; else do F2 = .; F2_P = 0; END;
IF F2 = 2 then F2= 4; IF F2> 2 then F2= F2 - 1; *Reordered the answers; 

/* Imputes for missing age, education, and sex values using SUDAAN's */
/* Weighted Sequential hotdeck routine in a chain */

data d; set d;

proc sort data = d; by source2; 

proc impute data=d seed=618573 method=wshd icnum; 
weight _one_;
class age a1 a8;
impby source2;
impvar age;
 impname age="age_imp" ;
impid u_id;
idvar a1 a8 age internet;
output / impute=default filename=wshd1 filetype=sas replace;
run;

proc sort; by source2 age_imp; 

proc impute data=wshd1 seed=618573 method=wshd icnum; 
weight _one_;
class a1 a8;
impby source2 age_imp;
impvar a1 ;
 impname a1="a1_imp" ;
impid u_id;
idvar a1 a8 age_imp internet;
output / impute=default filename=wshd2 filetype=sas replace;

proc sort; by source2 a1_imp age_imp; 

proc impute data=wshd2 seed=618573 method=wshd icnum; 
weight _one_;
* class a1_imp;
impby source2 a1_imp age_imp;
impvar a8 ;
 impname a8="a8_imp" ;
impid u_id;
idvar a1_imp age_imp internet;
output / impute=default filename=wshd3 filetype=sas replace;

data imp; set wshd3; keep u_id age_imp a1_imp a8_imp; run; 

proc sort; by u_id;

proc sort data = d; by u_id; 

/* Create a few more variables */

data d; merge d imp; by u_id; 
IF a8_imp < 7 then newedu = 2; else IF a8_imp = 7 then newedu = 3; else newedu = 4;

w = 1; * weights start at 1; 

rec = (1 - abs); * recruit; 

type = source2; 

data d; set d; 

IF 0 <a14 <= 4 then party = a14; else party = 0; 

IF type = 1 then do; x = 1; z = 1;end; *recruit;
IF type = 2 then do; x = 0; z = -1; end; * mail to internet; 
IF type = 3 then do; x = 0; z = 0; end; * mail; 

/* Main (0) (Compares calibrated estimates for TYPE1 and TYPE2 ) */
 
proc wtadjx data = d adjust = post design = wr maxiter = 50; nest _one_; weight w; 
var B1 B2 B3 B4 b5 B1_P B2_P B3_P B4_P b5_P
C2 C3 C4 C5 C6 C7 C8 C9 C10 C2_P C3_P C4_P C5_P C6_P C7_P C8_P C9_P C10_P
E4 E5 E6 E7 E8 F2 E4_P E5_P E6_P E7_P E8_P F2_P; 

lowerbd 1;
class a1_imp age_imp newedu type party; 
model one = a1_imp*abs age_imp*abs newedu*abs a1_imp*x age_imp*x newedu*x party*x /noint;
calvars a1_imp*abs age_imp*abs newedu*abs a1_imp*z age_imp*z newedu*z party*z /noint;
postwgt
1590387 1520632 362958 542546 522852 507631 542074 632958 2245902 546202 318915 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ; 
idvar varid ; 
 vdiffvar type = (1 2); 

output wtfinal idvar /filename=bothw replace; 
output diffwtz/filename = dIF replace;
output mean t_mean p_mean/filename = test replace; run;

proc print data = dif; 

data test; set test; IF _c1 = 0; p100 = p_mean *100; * Convert from fraction to percent ; 
proc sort data = test; by p100;
proc print; id variable; var p100; run; 

proc sort data = bothw; by varid; 

/* Getting ready for creating 30 sets of jackknife weights */

data d; merge d bothw; by varid; w = wtfinal; drop wtfinal; 

w1 = w*30/29;
w2 = w*30/29;
w3 = w*30/29;
w4 = w*30/29;
w5 = w*30/29;
w6 = w*30/29;
w7 = w*30/29;
w8 = w*30/29;
w9 = w*30/29;
w10 = w*30/29;
w11 = w*30/29;
w12 = w*30/29;
w13 = w*30/29;
w14 = w*30/29;
w15 = w*30/29;
w16 = w*30/29;
w17 = w*30/29;
w18 = w*30/29;
w19 = w*30/29;
w20 = w*30/29;
w21 = w*30/29;
w22 = w*30/29;
w23 = w*30/29;
w24 = w*30/29;
w25 = w*30/29;
w26 = w*30/29;
w27 = w*30/29;
w28 = w*30/29;
w29 = w*30/29;
w30 = w*30/29;

IF jkgroup = 1 then w1 = 0;
IF jkgroup = 2 then w2 = 0;
IF jkgroup = 3 then w3 = 0;
IF jkgroup = 4 then w4 = 0;
IF jkgroup = 5 then w5 = 0;
IF jkgroup = 6 then w6 = 0;
IF jkgroup = 7 then w7 = 0;
IF jkgroup = 8 then w8 = 0;
IF jkgroup = 9 then w9 = 0;
IF jkgroup = 10 then w10 = 0;
IF jkgroup = 11 then w11 = 0;
IF jkgroup = 12 then w12 = 0;
IF jkgroup = 13 then w13 = 0;
IF jkgroup = 14 then w14 = 0;
IF jkgroup = 15 then w15 = 0;
IF jkgroup = 16 then w16 = 0;
IF jkgroup = 17 then w17 = 0;
IF jkgroup = 18 then w18 = 0;
IF jkgroup = 19 then w19 = 0;
IF jkgroup = 20 then w20 = 0;
IF jkgroup = 21 then w21 = 0;
IF jkgroup = 22 then w22 = 0;
IF jkgroup = 23 then w23 = 0;
IF jkgroup = 24 then w24 = 0;
IF jkgroup = 25 then w25 = 0;
IF jkgroup = 26 then w26 = 0;
IF jkgroup = 27 then w27 = 0;
IF jkgroup = 28 then w28 = 0;
IF jkgroup = 29 then w29 = 0;
IF jkgroup = 30 then w30 = 0;

/* 1 */
 
proc wtadjx data = d adjust = post design = wr maxiter = 50; nest _one_; weight w1; * lowerbd 1; 
class a1_imp age_imp newedu party type; 
model one = a1_imp*abs age_imp*abs newedu*abs a1_imp*x age_imp*x newedu*x party*x/noint;
calvars a1_imp*abs age_imp*abs newedu*abs a1_imp*z age_imp*z newedu*z party*z/noint;
postwgt
1590387 1520632 362958 542546 522852 507631 542074 632958 2245902 546202 318915 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 
idvar varid; 
output wtfinal idvar /filename=bothw1 replace; run;

. [repeat for each replicate]
.
.

/* 30 */
 
proc wtadjx data = d adjust = post design = wr maxiter = 50; nest _one_; weight w30; * lowerbd 1; 
class a1_imp age_imp newedu party type; 
model one = a1_imp*abs age_imp*abs newedu*abs a1_imp*x age_imp*x newedu*x party*x/noint;
calvars a1_imp*abs age_imp*abs newedu*abs a1_imp*z age_imp*z newedu*z party*z/noint;
postwgt
1590387 1520632 362958 542546 522852 507631 542074 632958 2245902 546202 318915 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 
idvar varid; 
output wtfinal idvar /filename=bothw30 replace; run;

/* Renames all 31 WTFINALs to wt, wt1, …, wt30 */ 

data w ; set bothw ; wt = wtfinal; drop wtfinal; proc sort; by varid; 
data w1 ; set bothw1 ; wt1 = wtfinal; drop wtfinal; proc sort; by varid; 
data w2 ; set bothw2 ; wt2 = wtfinal; drop wtfinal; proc sort; by varid; 
data w3 ; set bothw3 ; wt3 = wtfinal; drop wtfinal; proc sort; by varid; 
data w4 ; set bothw4 ; wt4 = wtfinal; drop wtfinal; proc sort; by varid; 
data w5 ; set bothw5 ; wt5 = wtfinal; drop wtfinal; proc sort; by varid; 
data w6 ; set bothw6 ; wt6 = wtfinal; drop wtfinal; proc sort; by varid; 
data w7 ; set bothw7 ; wt7 = wtfinal; drop wtfinal; proc sort; by varid; 
data w8 ; set bothw8 ; wt8 = wtfinal; drop wtfinal; proc sort; by varid; 
data w9 ; set bothw9 ; wt9 = wtfinal; drop wtfinal; proc sort; by varid; 
data w10 ; set bothw10 ; wt10 = wtfinal; drop wtfinal; proc sort; by varid; 
data w11 ; set bothw11 ; wt11 = wtfinal; drop wtfinal; proc sort; by varid; 
data w12 ; set bothw12 ; wt12 = wtfinal; drop wtfinal; proc sort; by varid; 
data w13 ; set bothw13 ; wt13 = wtfinal; drop wtfinal; proc sort; by varid; 
data w14 ; set bothw14 ; wt14 = wtfinal; drop wtfinal; proc sort; by varid; 
data w15 ; set bothw15 ; wt15 = wtfinal; drop wtfinal; proc sort; by varid; 
data w16 ; set bothw16 ; wt16 = wtfinal; drop wtfinal; proc sort; by varid; 
data w17 ; set bothw17 ; wt17 = wtfinal; drop wtfinal; proc sort; by varid; 
data w18 ; set bothw18 ; wt18 = wtfinal; drop wtfinal; proc sort; by varid; 
data w19 ; set bothw19 ; wt19 = wtfinal; drop wtfinal; proc sort; by varid; 
data w20 ; set bothw20 ; wt20 = wtfinal; drop wtfinal; proc sort; by varid; 
data w21 ; set bothw21 ; wt21 = wtfinal; drop wtfinal; proc sort; by varid; 
data w22 ; set bothw22 ; wt22 = wtfinal; drop wtfinal; proc sort; by varid; 
data w23 ; set bothw23 ; wt23 = wtfinal; drop wtfinal; proc sort; by varid;
data w24 ; set bothw24 ; wt24 = wtfinal; drop wtfinal; proc sort; by varid; 
data w25 ; set bothw25 ; wt25 = wtfinal; drop wtfinal; proc sort; by varid; 
data w26 ; set bothw26 ; wt26 = wtfinal; drop wtfinal; proc sort; by varid; 
data w27 ; set bothw27 ; wt27 = wtfinal; drop wtfinal; proc sort; by varid; 
data w28 ; set bothw28 ; wt28 = wtfinal; drop wtfinal; proc sort; by varid; 
data w29 ; set bothw29 ; wt29 = wtfinal; drop wtfinal; proc sort; by varid; 
data w30 ; set bothw30 ; wt30 = wtfinal; drop wtfinal; proc sort; by varid; 
 
data dnew; merge d w w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13 w14 w15 w16 w17
  w18 w19 w20 w21 w22 w23 w24 w25 w26 w27 w28 w29 w30; by varid; 

IF wt1 = . then wt1 = 0; 
IF wt2 = . then wt2 = 0; 
IF wt3 = . then wt3 = 0; 
IF wt4 = . then wt4 = 0;
IF wt5 = . then wt5 = 0; 
IF wt6 = . then wt6 = 0; 
IF wt7 = . then wt7 = 0; 
IF wt8 = . then wt8 = 0;
IF wt9 = . then wt9 = 0; 
IF wt10 = . then wt10 = 0; 
IF wt11 = . then wt11 = 0; 
IF wt12 = . then wt12 = 0;
IF wt13 = . then wt13= 0; 
IF wt14 = . then wt14 = 0; 
IF wt15 = . then wt15 = 0; 
IF wt16 = . then wt16 = 0;
IF wt17 = . then wt17 = 0; 
IF wt18 = . then wt18 = 0; 
IF wt19 = . then wt19 = 0; 
IF wt20 = . then wt20 = 0;
IF wt21 = . then wt21 = 0; 
IF wt22 = . then wt22 = 0; 
IF wt23 = . then wt23 = 0; 
IF wt24 = . then wt24 = 0;
IF wt25 = . then wt25 = 0; 
IF wt26 = . then wt26 = 0;
IF wt27 = . then wt27 = 0; 
IF wt28 = . then wt28 = 0; 
IF wt29 = . then wt29 = 0; 
IF wt30 = . then wt30 = 0;


/* Create weights and replicate weights for Mail only (WTABSx) */

wtabs = wt; IF type = 1 then wtabs = 0; 
wtabs1 = wt1; IF type = 1 then wtabs1 = 0;
wtabs2 = wt2; IF type = 1 then wtabs2 = 0; 
wtabs3 = wt3; IF type = 1 then wtabs3 = 0;
wtabs4 = wt4; IF type = 1 then wtabs4 = 0; 
wtabs5 = wt5; IF type = 1 then wtabs5 = 0;
wtabs6 = wt6; IF type = 1 then wtabs6 = 0; 
wtabs7 = wt7; IF type = 1 then wtabs7 = 0;
wtabs8 = wt8; IF type = 1 then wtabs8 = 0; 
wtabs9 = wt9; IF type = 1 then wtabs9 = 0;
wtabs10 = wt10; IF type = 1 then wtabs10 = 0; 
wtabs11 = wt11; IF type = 1 then wtabs11 = 0;
wtabs12 = wt12; IF type = 1 then wtabs12 = 0; 
wtabs13 = wt13; IF type = 1 then wtabs13 = 0;
wtabs14 = wt14; IF type = 1 then wtabs14 = 0; 
wtabs15 = wt15; IF type = 1 then wtabs15 = 0;
wtabs16 = wt16; IF type = 1 then wtabs16 = 0; 
wtabs17 = wt17; IF type = 1 then wtabs17 = 0;
wtabs18 = wt18; IF type = 1 then wtabs18 = 0; 
wtabs19 = wt19; IF type = 1 then wtabs19 = 0;
wtabs20 = wt20; IF type = 1 then wtabs20 = 0; 
wtabs21 = wt21; IF type = 1 then wtabs21 = 0;
wtabs22 = wt22; IF type = 1 then wtabs22 = 0; 
wtabs23 = wt23; IF type = 1 then wtabs23 = 0;
wtabs24 = wt24; IF type = 1 then wtabs24 = 0; 
wtabs25 = wt25; IF type = 1 then wtabs25 = 0;
wtabs26 = wt26; IF type = 1 then wtabs26 = 0; 
wtabs27 = wt27; IF type = 1 then wtabs27 = 0;
wtabs28 = wt28; IF type = 1 then wtabs28 = 0; 
wtabs29 = wt29; IF type = 1 then wtabs29 = 0;
wtabs30 = wt30; IF type = 1 then wtabs30 = 0; 

/* Weights and replicate weights for compositing internet-responding sample */ 

aa = .35; * Compositing Factor; 

IF type = 1 then do; 
wt = wt*aa ; 
wt1 = wt1*aa ; 
wt2 = wt2*aa ; 
wt3 = wt3*aa ; 
wt4 = wt4*aa ; 
wt5 = wt5*aa ; 
wt6 = wt6*aa ; 
wt7 = wt7*aa ; 
wt8 = wt8*aa ; 
wt9 = wt9*aa ; 
wt10 = wt10*aa ; 
wt11 = wt11*aa ; 
wt12 = wt12*aa ; 
wt13 = wt13*aa ; 
wt14 = wt14*aa ; 
wt15 = wt15*aa ; 
wt16 = wt16*aa ; 
wt17 = wt17*aa ; 
wt18 = wt18*aa ; 
wt19 = wt19*aa ; 
wt20 = wt20*aa ; 
wt21 = wt21*aa ; 
wt22 = wt22*aa ; 
wt23 = wt23*aa ; 
wt24 = wt24*aa ; 
wt25 = wt25*aa ; 
wt26 = wt26*aa ; 
wt27 = wt27*aa ; 
wt28 = wt28*aa ; 
wt29 = wt29*aa ; 
wt30 = wt30*aa ; 
end;

IF type = 2 then do; 
wt = wt*( 1 - aa) ; 
wt1 = wt1*( 1 - aa) ; 
wt2 = wt2*( 1 - aa) ; 
wt3 = wt3*( 1 - aa) ; 
wt4 = wt4*( 1 - aa) ; 
wt5 = wt5*( 1 - aa) ; 
wt6 = wt6*( 1 - aa) ; 
wt7 = wt7*( 1 - aa) ; 
wt8 = wt8*( 1 - aa) ; 
wt9 = wt9*( 1 - aa) ; 
wt10 = wt10*( 1 - aa) ; 
wt11 = wt11*( 1 - aa) ; 
wt12 = wt12*( 1 - aa) ; 
wt13 = wt13*( 1 - aa) ; 
wt14 = wt14*( 1 - aa) ; 
wt15 = wt15*( 1 - aa) ; 
wt16 = wt16*( 1 - aa) ; 
wt17 = wt17*( 1 - aa) ; 
wt18 = wt18*( 1 - aa) ; 
wt19 = wt19*( 1 - aa) ; 
wt20 = wt20*( 1 - aa) ; 
wt21 = wt21*( 1 - aa) ; 
wt22 = wt22*( 1 - aa) ; 
wt23 = wt23*( 1 - aa) ; 
wt24 = wt24*( 1 - aa) ; 
wt25 = wt25*( 1 - aa) ; 
wt26 = wt26*( 1 - aa) ; 
wt27 = wt27*( 1 - aa) ; 
wt28 = wt28*( 1 - aa) ; 
wt29 = wt29*( 1 - aa) ; 
wt30 = wt30*( 1 - aa) ; 
end;
 

/* Compare internet respondents using jackknife */

proc descript noprint data = dnew design = jackknife; weight wt; 
jackwgts wt1-wt30/adjjack =.9667; * .9667 29/30 ;
var B1 B2 B3 B4 b5 B1_P B2_P B3_P B4_P b5_P
C2 C3 C4 C5 C6 C7 C8 C9 C10 C2_P C3_P C4_P C5_P C6_P C7_P C8_P C9_P C10_P
E4 E5 E6 E7 E8 F2 E4_P E5_P E6_P E7_P E8_P F2_P; 
 class type; 
output mean semean t_mean p_mean/filename = diff replace;
 diffvar type = (1 2); run; 

 data diff; set diff; IF _one_ = 1; p100 = p_mean*100;
proc sort; by p_mean; 
 proc print; id variable; var p100;

/* Estimates from whole sample using the jackknife */

proc descript noprint data = dnew design = jackknife; weight wt; 
jackwgts wt1-wt30/adjjack =.9667; class type; 
var B1 B2 B3 B4 b5 B1_P B2_P B3_P B4_P B5_P 
C2 C3 C4 C5 C6 C7 C8 C9 C10 C2_P C3_P C4_P C5_P C6_P C7_P C8_P C9_P C10_P
E4 E5 E6 E7 E8 F2 E4_P E5_P E6_P E7_P E8_P F2_P; 
 
output mean semean/ filename = both replace; run;
 proc print; 

data both; set both; IF type = 0; 
 
/* Estimates from mail sample using the jackknife */

proc descript noprint data = dnew design = jackknife; weight wtabs; 
jackwgts wtabs1-wtabs30/adjjack =.9667; 
var B1 B2 B3 B4 b5 B1_P B2_P B3_P B4_P b5_P
C2 C3 C4 C5 C6 C7 C8 C9 C10 C2_P C3_P C4_P C5_P C6_P C7_P C8_P C9_P C10_P
E4 E5 E6 E7 E8 F2 E4_P E5_P E6_P E7_P E8_P F2_P; 
output mean semean/filename = abs replace; 
 run; 

data abs; set abs; IF _one_ = 1; 

data both; set both; bse = semean; drop semean; 

data m; merge both abs; IF _one_ = 1; d = log(bse/semean); 

proc univariate; var d; run; /* Decrease in se due to adding recruits */

/* Comparing Estimates using linearization and the jackknife */

proc wtadjx data = dnew adjust = post design = wr maxiter = 50; nest _one_; weight _one_; 
var B1 B2 B3 B4 b5 B1_P B2_P B3_P B4_P b5_P
C2 C3 C4 C5 C6 C7 C8 C9 C10 C2_P C3_P C4_P C5_P C6_P C7_P C8_P C9_P C10_P
E4 E5 E6 E7 E8 F2 E4_P E5_P E6_P E7_P E8_P F2_P; 

lowerbd 1;
class a1_imp age_imp newedu type party; 
model one = a1_imp*abs age_imp*abs newedu*abs a1_imp*x age_imp*x newedu*x party*x /noint;
calvars a1_imp*abs age_imp*abs newedu*abs a1_imp*z age_imp*z newedu*z party*z /noint;
postwgt
1590387 1520632 362958 542546 522852 507631 542074 632958 2245902 546202 318915 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ; 
idvar varid type c2 a1_imp abs age_imp newedu; 
 vdiffvar type = (1 2); 

output mean se_mean t_mean p_mean/filename=direct replace; run;

data direct; set direct; IF party = 0; IF _c5 = 0; 

proc descript noprint data = dnew design = jackknife; weight wt; 
jackwgts wt1-wt30/adjjack =.9667; 
var B1 B2 B3 B4 b5 B1_P B2_P B3_P B4_P b5_P
C2 C3 C4 C5 C6 C7 C8 C9 C10 C2_P C3_P C4_P C5_P C6_P C7_P C8_P C9_P C10_P
E4 E5 E6 E7 E8 F2 E4_P E5_P E6_P E7_P E8_P F2_P; 
 class type; 
output mean semean t_mean p_mean/filename = diff replace;
 diffvar type = (1 2); run; 

 data diff; set diff; IF _c2 = 1; diff = semean; drop semean; 

data m; merge diff direct; d = log(diff/se_mean); d2 = (diff/se_mean)**2 - 1; 

proc univariate; var d ; run; * Compares jk to lin ;

proc sort data = direct; by p_mean; 
proc print; format p_mean f7.5; run; 
