*--------------------------------------------------------------; * Estimates the mean (per unit), total, or proportion, for a ; * simple random sample from a finite population. Data may be ; * in raw form, or in the form of summary statistics. ; *--------------------------------------------------------------; %macro est_srs(sample=,setup=,npop=,n=,fpc=,response=,counts=, phat=,ybar=,var=,type=,param=); %if %length(&npop) = 0 %then %let npop = %str(npop); %if %length(&n) = 0 %then %let n = %str(n); %if %index(%upcase(¶m),PRO) > 0 %then %let parm = %str(Proportion); %if %index(%upcase(¶m),MEAN) > 0 %then %let parm = %str(Mean); %if %index(%upcase(¶m),TOTAL) > 0 %then %let parm = %str(Total); %if %index(%upcase(&type),SUMMARY) > 0 %then %do; /*--------------------------------------------------------*/ /* estimates population parameters from summary data */ /*--------------------------------------------------------*/ %let vprint2 = %str(); %let vprint = %str(); data est_; %if %length(&setup) > 0 %then %do; set &setup; %end; %if %length(&sample) > 0 %then %do; set &sample; %end; n_ = &n; %if %length(&ybar) > 0 %then %do; %let response = %str(&ybar); %let cnts = %str( (Mean)); ybar_ = &ybar; s2_ = &var; %end; %if %length(&phat) > 0 %then %do; %let response = %str(&phat); %let cnts = %str( (Proportion)); ybar_ = &phat; s2_ = &n*ybar_*(1-ybar_)/(&n - 1); %end; %if %length(&counts) > 0 %then %do; %let response = %str(&counts); %let cnts = %str( (Count)); ybar_ = &counts/&n; s2_ = &n*ybar_*(1-ybar_)/(&n - 1); %end; data est_; set est_; mu_hat_ = ybar_; %end; /*-----------------------------------------------------*/ /* estimates population parameters from raw data */ /*-----------------------------------------------------*/ %else %do; %if %length(&sample) = 0 %then %let sample = %str(sample); %let vprint = %str(s2_); %let vprint2 = %str( label s2_ = "s^2*(&response)" ); %let cnts = %str(); proc means data = &sample noprint; var &response; output out = est_ mean = ybar_ var = s2_ n = n_; data est_; %if %length(&setup) > 0 %then %do; merge &setup est_; %end; %else %do; set est_; %end; mu_hat_ = ybar_; %end; %if %length(&fpc) > 0 %then %do; var_mu_ = s2_/n_; std_mu_ = sqrt(var_mu_); bnd_mu_ = 2*std_mu_; keep s2_ mu_hat_ std_mu_ bnd_mu_ n_; %end; %else %do; fpc_ = (&npop-n_)/&npop; var_mu_ = fpc_*s2_/n_; std_mu_ = sqrt(var_mu_); bnd_mu_ = 2*std_mu_; tau_hat_ = &npop*ybar_; std_tau_ = &npop*std_mu_; bnd_tau_ = 2*std_tau_; keep s2_ mu_hat_ tau_hat_ std_mu_ std_tau_ bnd_mu_ bnd_tau_ n_; %end; /*--------------------------------------------------------*/ /* Print results by ¶m code. */ /*--------------------------------------------------------*/ %if %index(%upcase(¶m),MEAN) > 0 | %index(%upcase(¶m),PROP) > 0 %then %do; proc print data = est_ noobs split = '*'; title1 "Estimate of the Population &parm"; title2 "Simple Random Sampling Design"; title3 "Response Variable = &response &cnts"; label mu_hat_ = 'Estimate'; label std_mu_ = 'Standard*Error'; label bnd_mu_ = 'Bound'; label n_ = 'Sample*Size'; &vprint2; var mu_hat_ std_mu_ bnd_mu_ &vprint n_; %end; %if %index(%upcase(¶m),TOTAL) > 0 %then %do; proc print data = est_ noobs split = '*'; title1 "Estimate of the Population &parm"; title2 "Simple Random Sampling Design"; title3 "Response Variable = &response &cnts"; label tau_hat_ = 'Estimate'; label std_tau_ = 'Standard*Error'; label bnd_tau_ = 'Bound'; label n_ = 'Sample*Size'; &vprint2; var tau_hat_ std_tau_ bnd_tau_ &vprint n_; %end; run; title; %mend est_srs;