*--------------------------------------------------------------; * Estimates the mean, total or proportion from a stratified ; * random sample. Data may be in raw or summarized form. ; *--------------------------------------------------------------; %macro est_strs(sample=,setup=,npop=,fpc=,wts=,strata=,response=, counts=,phat=,ybar=,var=,n=,param=,type=); %if %length(&strata) = 0 %then %let strata = %str(strata); %if %length(&n) = 0 %then %let n = %str(n); %if %length(&sample) > 0 %then %do; proc sort data = &sample; by &strata; %end; %if %length(&setup) > 0 %then %do; proc sort data = &setup; by &strata; %end; /*----------------------------------------------------*/ /* estimates population parameters from summary data */ /*----------------------------------------------------*/ %if %index(%upcase(&type),SUMMARY) > 0 %then %do; %if %length(&setup) > 0 %then %do; %if %length(&sample) > 0 %then %do; data est_; merge &setup &sample; by &strata; %end; %else %do; data est_; set &setup; %end; %end; %else %do; data est_; set &sample; %end; ni_ = &n; %if %length(&counts) > 0 %then %do; %let cnts = %str( (Counts)); %let response = %str(&counts); ybar_ = &counts/ni_; var_ = ni_*ybar_*(1-ybar_)/(ni_ - 1); %end; %if %length(&phat) > 0 %then %do; %let cnts = %str( (Proportions)); %let response = %str(&phat); ybar_ = &phat; var_ = ni_*ybar_*(1-ybar_)/(ni_ - 1); %end; %if %length(&ybar) > 0 %then %do; %let cnts = %str( (Means)); %let response = %str(&ybar); ybar_ = &ybar; var_ = &var; %end; %end; /*----------------------------------------------------------*/ /* estimates population parameters from raw data */ /*----------------------------------------------------------*/ %else %do; %if %length(&sample) = 0 %then %let sample = %str(sample); %if %length(&setup) = 0 %then %let setup = %str(setup); %let cnts = %str(); proc sort data = &sample; by &strata; proc means data = &sample noprint; by &strata; var &response; output out = est_ mean = ybar_ var = var_ n = ni_; proc sort data = &setup; by &strata; data est_; merge &setup est_ ; by &strata; proc print data = est_ noobs split='*'; title1 'Actual Sample Sizes'; title2 '(Excludes Missing Data, If Any)'; label ni_ = 'n(i)'; var &strata ni_; %end; %if %length(&npop) > 0 %then %do; data est_; set est_ end = eof_; ntot_ + &npop; if eof_ = 1 then do; call symput('ntot',trim(left(ntot_))); end; run; data est_; set est_ end = eof_; fpc_ = (&npop-ni_)/&npop; wts_ = &npop/&ntot; %end; %else %do; data est_; set est_ end = eof_; fpc_ = 1; wts_ = &wts; %end; tersum_ = wts_*ybar_; terssq_ = wts_*wts_*fpc_*var_/ni_; sum_ + tersum_; ssq_ + terssq_; if eof_ = 1 then do; mu_hat_ = sum_; std_mu_ = sqrt(ssq_); bnd_mu_ = 2*std_mu_; %if %length(&npop) > 0 %then %do; tau_hat_ = &ntot*mu_hat_; std_tau_ = &ntot*std_mu_; bnd_tau_ = &ntot*bnd_mu_; %end; output; end; drop sum_ ssq_ tersum_ terssq_; %if %index(%upcase(¶m),MEAN) > 0 %then %do; proc print data = est_ noobs split = '*'; label mu_hat_ = 'Estimate'; label std_mu_ = 'Standard*Error'; label bnd_mu_ = 'Bound'; title1 "Estimate of the Population Mean"; title2 "Stratified Random Sampling Design"; title3 "Response Variable = &response &cnts"; var mu_hat_ std_mu_ bnd_mu_; %end; %if %index(%upcase(¶m),PROP) > 0 %then %do; proc print data = est_ noobs split = '*'; label mu_hat_ = 'Estimate'; label std_mu_ = 'Standard*Error'; label bnd_mu_ = 'Bound'; title1 "Estimate of the Population Proportion"; title2 "Stratified Random Sampling Design"; title3 "Response Variable = &response &cnts"; var mu_hat_ std_mu_ bnd_mu_; %end; %if %index(%upcase(¶m),TOTAL) > 0 %then %do; proc print data = est_ noobs split = '*'; label tau_hat_ = 'Estimate'; label std_tau_ = 'Standard*Error'; label bnd_tau_ = 'Bound'; title1 "Estimate of the Population Total"; title2 "Stratified Random Sampling Design"; title3 "Response Variable = &response &cnts"; var tau_hat_ std_tau_ bnd_tau_; %end; run; title; %mend est_strs;