*--------------------------------------------------------------; * Combined ratio estimate of population mean and total, for ; * a stratified random sample with auxiliary variable X. ; *--------------------------------------------------------------; %macro c_ratio(sample=,response=,strata=,param=,setup=, npop=,n=,mu_x=,tau_x=,x=,fpc=,wts=); %if %length(&sample) = 0 %then %let sample = %str(sample); %if %length(&strata) = 0 %then %let strata = %str(strata); %if %length(&setup) = 0 %then %let setup = %str(setup); %if %length(&x) = 0 %then %let x = %str(x); %if %length(&tau_x) > 0 %then %let mu_x = %str(&tau_x/&npop); %else %put %str(*** Neither MU_X nor TAU_X was defined ***); proc sort data = &setup; by &strata; proc sort data = &sample; by &strata; %if %length(&npop) > 0 %then %do; proc means data = &setup noprint; var &npop; output out = ntot_ sum = ntotal_; data ntot_; set ntot_; call symput('ntot',trim(left(ntotal_))); run; %end; data est_; set &sample( keep = &x &response &strata); xy_ = &x*&response; proc sort data = est_; by &strata; proc means data = est_ noprint; by &strata; var &x &response xy_; output out = c_out_ mean = xbari_ ybari_ sum = sumxi_ sumyi_ sumxyi_ css = ssxxi_ ssyyi_ n = n1_ n2_ ni_; data c_out_; merge c_out_ &setup; by &strata; ssxyi_ = sumxyi_ - sumxi_*sumyi_/ni_; %if %length(&npop) = 0 %then %do; wts_ = &wts; %end; %else %do; wts_ = &npop/&ntot; %end; yy_ = wts_*ybari_; xx_ = wts_*xbari_; mu_temp_ = wts_*(&mu_x); proc means data = c_out_ noprint; var xx_ yy_ mu_temp_; output out = combr_ sum = totx_ toty_ tu_x_; data combr_; set combr_; mu_xpop_ = tu_x_; rc_ = toty_/totx_; mu_hat_ = rc_*mu_xpop_; data final_; if _n_ = 1 then set combr_; set c_out_; sr2_ = (ssyyi_ + rc_**2*ssxxi_ - 2*rc_*ssxyi_)/(ni_-1); %if %length(&npop) > 0 %then %do; fpc_ = (&npop-ni_)/&npop; %end; %else %do; fpc_ = 1; %end; %if %length(&fpc) > 0 %then %do; fpc_ = 1; %end; %if %length(&npop) = 0 %then %do; wts_ = &wts; %end; %else %do; wts_ = &npop/&ntot; %end; vterm_ = (wts_)**2*fpc_/ni_*sr2_; rterm_ = vterm_/mu_xpop_**2; proc means data = final_ noprint; var vterm_ rterm_; output out = est_cr_ sum = vnumer_ rnumer_; data est_cr_; merge est_cr_ combr_; std_mu_ = sqrt(vnumer_); bnd_mu_ = 2*std_mu_; std_r_ = sqrt(rnumer_); bnd_r_ = 2*std_r_; %if %index(%upcase(¶m),TOTAL) > 0 %then %do; tau_hat_ = &ntot*mu_hat_; std_tau_ = &ntot*sqrt(vnumer_); bnd_tau_ = 2*std_tau_; %end; drop vnumer_; proc print data = c_out_ noobs split='*'; title1 'Actual Sample Sizes'; title2 'Excludes Missing Data, If Any'; label ni_ = 'n(i)'; var &strata ni_; %if %index(%upcase(¶m),MEAN) > 0 %then %do; proc print data = est_cr_ noobs split='*'; title1 "Combined Ratio Estimate of Population Mean"; title2 "Stratified Random Sampling Design"; title3 "Response Variable = &response"; title4 "(Auxiliary Variable = &x)"; label mu_hat_ = 'Estimate'; label std_mu_ = 'Standard*Error'; label bnd_mu_ = 'Bound'; label rc_ = 'r(c)'; var mu_hat_ std_mu_ bnd_mu_ rc_; %end; %if %index(%upcase(¶m),TOTAL) > 0 %then %do; proc print data = est_cr_ noobs split='*'; title1 "Combined Ratio Estimate of Population Total"; title2 "Stratified Random Sampling Design"; title3 "Response Variable = &response"; title4 "(Auxiliary Variable = &x)"; label tau_hat_ = 'Estimate'; label std_tau_ = 'Standard*Error'; label bnd_tau_ = 'Bound'; label rc_ = 'r(c)'; var tau_hat_ std_tau_ bnd_tau_ rc_; %end; %if %index(%upcase(¶m),R) > 0 %then %do; proc print data = est_cr_ noobs split='*'; title1 "Combined Ratio Estimate"; title2 "Stratified Random Sampling Design"; title3 "Response Variable = &response"; title4 "(Auxiliary Variable = &x)"; label rc_ = 'r(combined)'; label std_r_ = 'Standard*Error'; label bnd_r_ = 'Bound'; var rc_ std_r_ bnd_r_; %end; run; title; %mend c_ratio;