%macro applycpr(var,round=1,acsvar=,PctOf= ); %if %superq(acsvar) eq %str() %then %let acsvar=&var; %if %lowcase(%substr(&var,1,3))=%str(pct) and &round=1 %then %let round=.00001; %*--different rounding for pcts--; _cpr=.; if &acsvar._pacs=0 then &acsvar=0; else if &var._p2x then _cpr= &var._c2x/&var._p2x; else _cpr=.; %*---county to PUMA ratio per last available data (census 2k here)-; if _cpr then &acsvar = round(_cpr * &acsvar._pacs,&round); else if &acsvar=. then &acsvar=&var._c2x; %*--we estimate by applying the values ratio to the current puma figure. If no ratio then just carry forward the most recent county figure--; %if %superq(PctOf) ne %str() %then %do; if &PctOf then Pct&acsvar=100*&acsvar / &PctOf; %end; %if &debug=2 %then %do; %put '***debug: ' _cpr = &var._c2x= &var._p2x= &acsvar._pacs= &acsvar=; %end; %mend applycpr; %macro acprPct(var,PctOf); %*---This macro invokes the applycpr macro to estimate a Pct variable and then multiplies that estimated value by the PctOf (Universe) variable to estimate the corresponding count. The var parm is the base name without the leading "Pct"--; %if %superq(PctOf) = %str() %then %put ****acprPct macro must have a 2nd positional parameter specified ****; %let _pctvar=Pct&var; %applycpr(&_pctvar) &var=round( &_pctvar*.01*&PctOf); %mend acprPct; %macro apcprlst(list,_pctof); %*--utiity macro to invoke the applycpr macro for each variable in the parameter list--; %local i vari; %let i=1; %let vari=%scan(&list,1); %do %while (&vari ne %str()); %applycpr(&vari,Pctof=&_pctof) %let i=%eval(&i+1); %let vari=%scan(&list,&i); %end; %mend apcprlst; %macro dodist(tabnm,sfxs,tabtot); %*---do a distribution table: generalization of dohhinc macro, where we make the table and suffixes parms----; %*--we assume parallel arrays of cell counts and Pct-ages corresponding to those counts with tabtot as the denominator--; %global debug tabsize; %local i _pctvar sfx nsfxs; %let tabsize=%lenlst(&sfxs); _checksum=0; _checksum2=0; _checksum3=0; %do pass=1 %to 2; %*--double pass of table. 1st time we estimate the pcts and the cell counts and keep a check sum. On second pass we adjust the pcts and cell counts to insure they add to our control total, TotHHs.-; %do i=1 %to &tabsize; %let sfx=%scan(&sfxs,&i); %if &pass=1 %then %do; %*---Apply cpr method to estimate the cell count---*; %applycpr(&tabnm&sfx) _checksum + &tabnm&sfx; %end; %else %do; %*---2nd pass. We rake the values to match table control total and calculate pct variables---; %if &i=1 %then %str(if _checksum then _rake=&tabtot/_checksum; ); if _checksum then &tabnm&sfx=round(&tabnm&sfx*_rake); if &tabtot then Pct&tabnm&sfx=100*&tabnm&sfx/&tabtot; _checksum2 + &tabnm&sfx; _checksum3 + Pct&tabnm&sfx; %end; %end; %*--do i loop--; %if &debug and &pass=2 %then %do; put "For table &tabnm : " _checksum= _checksum2= _checksum3= _rake= &tabtot=; if round(_checksum3) ne 100 then do; put "***_checksum3 for Pct&tabnm values does not add to 100 as expected *** " _checksum3= ; end; %end; %end; %*--do pass loop--; %mend dodist; %macro dodistp(tabnm,sfxs,tabtot,pctcalc=1); %*---Experimental (as of 2-10-10) variation of the dodist macro, where we estimate the table cells using calculated Pct vars and then apply those estimated pcts to get the cell count estimates. Will this yield more reliable rsults?---; %*----The experiment yielded a fascinating result: dodist and dodistp gave essentially the same estimates!---------------; %*---the pctcalc flag indicates whether or not the Pct variables are already defined (pctcalc=0) or need to be defined by the macro (pctcalc=1). If the latter then new variables are calculated using the naming convention where we assume the existence of the tabtot variable with suffix _c2x, _p2x and _pacs. ---; %*--we assume parallel arrays of cell counts and Pct-ages corresponding to those counts with tabtot as the denominator--; %global debug tabsize temppcts; %local i _pctvar sfx nsfxs cell; %let tabsize=%lenlst(&sfxs); %local _checksim _checksum2 _checksum3; _checksum=0; _checksum2=0; _checksum3=0; %do pass=1 %to 2; %*--double pass of table. 1st time we estimate the pcts and the cell counts and keep a check sum. On second pass we adjust the pcts and cell counts to insure they add to our control total, TotHHs.-; %do i=1 %to &tabsize; %let sfx=%scan(&sfxs,&i); %let cell=&tabnm&sfx; %if &pctcalc %then %do; if Pct&cell._c2x=. and &tabtot._c2x then Pct&cell._c2x=100*&cell._c2x/&tabtot._c2x; if Pct&cell._p2x=. and &tabtot._p2x then Pct&cell._p2x=100*&cell._p2x/&tabtot._p2x; if Pct&cell._pacs=. and &tabtot._pacs then Pct&cell._pacs=100*&cell._pacs/&tabtot._pacs; %let temppcts=&temppcts Pct&cell._c2x Pct&call._p2x Pct&cell._pacs; %end; %if &pass=1 %then %do; %*---Apply cpr method to estimate the cell percentage---*; %acprpct(&cell,&tabtot) _checksum + &cell; %end; %else %do; %*---2nd pass. We rake the values to match table control total and calculate pct variables---; %if &i=1 %then %str(if _checksum then _rake=&tabtot/_checksum; ); if _checksum then &tabnm&sfx=round(&tabnm&sfx*_rake); if &tabtot then Pct&tabnm&sfx=100*&tabnm&sfx/&tabtot; _checksum2 + &tabnm&sfx; _checksum3 + Pct&tabnm&sfx; %end; %end; %*--do i loop--; %if &debug and &pass=2 %then %do; put "For table &tabnm : " _checksum= _checksum2= _checksum3= _rake= &tabtot=; if round(_checksum3) ne 100 then do; put "***_checksum3 for Pct&tabnm values does not add to 100 as expected *** " _checksum3= ; end; %end; %end; %*--do pass loop--; %mend dodistp; %macro alias(name,alias); &alias._c2x=&name._c2x; &alias._p2x=&name._p2x; &alias._pacs=&name._pacs; drop &alias._: ; %mend alias;