x cd /pub/data/acs2009/Tools; %let pgm=allocate2trk2010; filename pgm "&pgm..sas"; *---We shall take the 5-year usmcdcprofiles data set for this vintage (i.e. the 2005-2009 5yr data) at the 2000 census tract level and allocate it to get estimates at the 2010 census tract level. To keep things simple we shall drop all the _moe variables and we shall treat medians like means: taking a weighted average of them. John Blodgett Missouri Census Data Center/OSEA Feb. 2016. ---*; *--input data set relating the 2k tracts to 2010 tracts is in this sas data library--; libname blkrel10 '/pub/data/blkrel10' access=readonly; *--the acs2009 data library contains both input and output data.--; libname acs2009 '/pub/data/acs2009'; *--the cnvtlog file is where we document the problems with unmatched census tracts--; filename cnvtlog "/pub/data/acs2009/&pgm._cnvtlog.txt"; *--Summarize the state-by-state converts here. Document problem tracts --; filename meanwts "/pub/data/acs2009/Tools/meanwts.txt"; *--Shows relationship of means and mean weights for these data--; *--Using D. Ward varlist utility macro to get a list of the _moe (Margin Of Error) variables in input datasets --; %let moes=%varlist(*_moe,acs2009.usmcdcprofiles5yr); options compress=yes; *--Only saves about 10 percent, but why not. ---; *---reset the cnvtlog file---; data _null_; file cnvtlog; put "&sysdate &systime reset"; run; *--we write mod (i.e. append) to this file during processing.--; *---We actually have a metadata set for these profile data sets. We access that data set in order to generate the lists of variables that have to be weighted in order to aggregate them. We look for all variables where a "percent_variable" or a WtVar var. is specified. That is the weight variable. If there is a value for Percent_variable then that is the variable that gets weighted. (Our metadata set does not have separate entries for the Pct variables. They are implied.)---; data _null_; set acs2009.variables5yr end=_last; *--read the metadata. Each obs represents 1 numeric variable. Except no Pct vars or _moe vars-; length _means $6000 _meanwts $4000; retain; if percent_variable ne ' ' then do; _means=trimn(_means)||' '||percent_variable; _meanwts=trimn(_meanwts)|| ' '||UnivVar; *--the universe variable is the denominator for the pct-; end; else if wtvar ne ' ' then do; *--we have a variable such as PCI (per capita income) or AvgHHIncome that needs to be weighted-; _means=trimn(_means)||' '||variable_name; _meanwts=trimn(_meanwts)|| ' '||WtVar; *--we only have a value for wtvar for averages and medians--; end; if _last then do; *--Save the variables lists as macro variables that can then be used as SAS code in succeeding step as agg macro parms--; call symput ('_means',trim(_means)); call symput ('_meanwts',trim(_meanwts)); title "Means Variables and Corresponding Weight Variables"; options nocenter nodate; do _i=1 to 600; length _mean _meanwt $32; _mean=scan(_means,_i,' '); _meanwt=scan(_meanwts,_i,' '); if _mean=' ' and _meanwt=' ' then leave; file meanwts ; put _i +1 _mean $32. _meanwt $32.; end; end; run; %put _means=&_means ; %put _meanwts=&_meanwts; title "Unmatched 2000 Census Tracts Encountered"; *---We define a macro to do the allocation for a state. Once we have it debugged for one state we can use our dostates macro to replicate the processing for all the states (or any subset of states of interest). ---; %macro doit(state,stab); %*let state=29; %*let stab=mo; *<===for test runs===; *----We have a set of equivalency files going from the old 2k tract to the 2010 tract based on blkrel10 data. Here we pull the tract level data for the state from the usmcdcprofiles5yr data set and merge it with the equivalancy data set to create aggin. ----*; *---define a view that selects variables and observations for the state and summary level---; data acstracts/view=acstracts; set acs2009.usmcdcprofiles5yr(in=inacs drop=&moes where=(state="&state" and sumlev='140') ); County2k=county; State2k=State; length tract2k $7; *--we did not create a tract variable for this data set. But we can do so now. Need the xxxx.xx format which is our standard way of representing tract codes and is consistent for matching with the equivalency file. So if geoid=14000US01001020100 then we have tract 201 in county 01001 and we want the tract2k value to be 0201.00 --; tract2k=substr(geoid,13,4)||'.'||substr(geoid,17,2); drop county--dateCreated; *--ID variables from input are not kept. The only ones we need are the ones assigned here.--; run; ***proc print data=acstracts(keep=_character_ Totpop obs=25); ***run; data aggin&stab; merge blkrel10.&stab.tr2k_tr10(in=inclist) acstracts(in=inacs) end=_last; by state2k county2k tract2k; if not (inclist and inacs) then do; if inacs and totpop=0 then goto cklast; if inclist and pop2kint=0 then goto cklast; file cnvtlog mod; _badcase+1; if _badcase <= 400 then do; put "&stab tract not matched: " county2k= tract2k= pop2kInt= afact= totpop= inclist= inacs= ; _sumtotpop + totpop; _sumpop2kint_afact + (pop2kint*afact); end; goto cklast; *** delete; end; else output; drop _: ; cklast: if _last then do; file cnvtlog mod; put "Processing " state $state. " at &systime on &sysdate.. " @; if _badcase then put '*** ' _badcase "unmatched block cases encountered (see above for details). ********"/ +3 "Total pop for unmatched acs records was " _sumtotpop ". Total pop in unmatched clist records was " _sumpop2kint_afact 7.0; else put 'All blocks matched.'; end; run; proc sort data=aggin&stab; by state county tract; run; *--sort by the target geos--; *---This is the big step. Invoke %agg and do the specs. Especially difficult is getting all the means and meanwts specified. Note that we are going to treat medians as if they were aggregatable via weigthed averaging even though that is statistical malpractice. It yeilds a value that is associated with the true median but is not really. ---*; %agg(aggin=aggin&stab, aggout=&stab.tracts10, aggby=state county tract, agglvl=1, /* while testing we used agglvl=2 to get county summaries and verified results at county level */ aggvars=_numeric_, means=&_means, meanwts=&_meanwts, facvar=afact, dropvars=afact pop2kint _nag_, debug=0); run; *---Post step to add ID variables ----*; data acs2009.&stab.tracts10(label="usmcdcprofiles5yr data allocated to 2010 census tracts for &stab"); length State $2 County $5 Tract $7 Sumlev $3 Areaname $64 esriid $11 Vintage $4 Period $3; retain Vintage '2009' Period '5yr' SumLev '140'; set &stab.tracts10; Areaname='2010 Census Tract '||tract||', '||put(county,$county.); esriid=county||compress(tract,'.'); *--remove the period from our tract variable: tttt.ss becomes ttttss --; where _lvl_=1; drop _lvl_; run; %mend doit; %*dostates(01 02 05 06 08); *--convert selected states using list of fips codes as positional parm---; %dostates(n=51); *---Use the n= parm to indicate common groups of states. 51 means 50 states and DC---; run;