x cd /pub/data/sf12000/Tools; %let pgm=aggsf1_moslds; filename pgm "&pgm..sas"; /*----------------------------------------------------------------------------------------------------------------- This code aggregates sf12000 data to Missouri SLDs - State Legislative Districts. Creates the SAS datasets: sf1.mosenateph: Missouri state senate districts - P and H tables. sf1.mosenatepct: Missouri state senate districts - PCT tables sf1.mohouseph: Missouri state house districts - P and H tables. sf1.mohousepct: Missouri state house districts - PCT tables Coded by John Blodgett, OSEDA, U. of Missouri, blodgettj@umsystem.edu Under contract with the Missouri Census Data Center Revision History: 7.27.01: Coding begins. 8.03.01: Rerun after mohousepct got damaged. ------------------------------------------------------------------------------------------------------*/ libname sf1 '/pub/data/sf12000'; libname user '/SAS_temp/user'; *<===Optional. Permissions on this directory limit who can use it; title 'sf12000 Aggregation Run: To Missouri State Legislative Districts: aggsf1_moslds'; options msglevel=i fullstimer; %macro atoI(list,x); %*--utility macro to take a list of variables containing a wild card character ("x", by defaut) and replacing it with the letters a, b, c, d, e, f, g, h, and I in order. To generate the lists used in sf1 for the race/hispanic tables.--; %if &x eq %then %let x=x; %*--default wild card character is "x"--; %sysfunc(translate(&list,a,&x)) %sysfunc(translate(&list,b,&x)) %sysfunc(translate(&list,c,&x)) %sysfunc(translate(&list,d,&x)) %sysfunc(translate(&list,e,&x)) %sysfunc(translate(&list,f,&x)) %sysfunc(translate(&list,g,&x)) %sysfunc(translate(&list,h,&x)) %sysfunc(translate(&list,I,&x)) %mend atoI; %let geos80=county cousubfp placefp tract; *<--geocode making up summary level 080---; data agginsf1(compress=yes); length sumlev $3 sldcode $3; merge sf1.mogeos(where=(sumlev='101' and pop100) in=keep) sf1.mophng; by geo_id; if keep; *where sumlev='101'; *<---blocks (moved to where option in merge stmt)---; *---Output once for each district type--; keep sumlev sldcode _numeric_; sumlev='610'; sldcode=sldu; output; sumlev='620'; sldcode=sldl; output; run; proc datasets library=user; *<--agginsf1 may be too big to sort, so we just create an index for it-; modify agginsf1; index create geos=(sumlev sldcode); quit; options compress=yes; *<---to get the aggregated output set to be compressed--; %agg(aggin=agginsf1,aggout=aggoutsf1, aggby=sumlev sldcode, aggvars=pop100 hu100 p1i1--h16Ii19 IntPtLon IntPtLat AreaSQMI, naggvars=3065, means= IntPtLon IntPtLat p17i1 p33i1 p17ai1 p17bi1 p17ci1 p17di1 p17ei1 p17fi1 p17gi1 p17hi1 p17Ii1 p33ai1 p33bi1 p33ci1 p33di1 p33ei1 p33fi1 p33gi1 p33hi1 p33Ii1 h12i1 h12i2 h12i3 %atoI(h12xi1 h12xi2 h12xi3) , mweights=p1i1 p1i1 p15i1 p31i1 p15ai1 p15bi1 p15ci1 p15di1 p15ei1 p15fi1 p15gi1 p15hi1 p15Ii1 p31ai1 p31bi1 p31ci1 p31di1 p31ei1 p31fi1 p31gi1 p31hi1 p31Ii1 h4i1 h4i2 h4i3 %atoI(h15xi1 h15xi2 h15xi10) , debug=1, agglvls=1) run; data sf1.mosenateph(label='Missouri State Senate Districts P & H Tables' drop=sldl house compress=yes) sf1.mohouseph(label='Missouri State House Disticts, P & H tables' drop=sldu senate compress=yes); length sumlev $3 geocode sldu senate sldl house $3 areaname $40 ; set aggoutsf1; *by sumlev sldcode; geocode=sldcode; if sumlev='610' then do; sldu=sldcode; senate=sldcode; areaname='State Senate District '||left(senate); end; else do; sldl=sldcode; house=sldcode; areaname='State House District '||left(house); end; drop _lvl_ _nag_ sldcode; label senate='State Senate District' house='State House District'; format intptlon 11.6 intptlat 10.6; *---Calculate all the age medians from the corresponding distributions. Note that we do not have explicit distrib tables for total age, but have to create the totals by summing male and female counts in each case.--; array _p12t {23} _temporary_; array _p12w {23} _temporary_ (5, 5, 5, 3, 2,1,1,3,5,5,5,5,5,5,5,2,3,2,3,5,5,5,5); option NOmprint; %macro domedian(r); %*--r is the race code for the table. We do the same processing on corresponding p13 and p12 tables depending on the race code. When r is blank we are working with the unqualified total-pop tables--; array _p12&r.m p12&r.i3-p12&r.i25; array _p12&r.f p12&r.i27-p12&r.i49; do _i_ = 1 to 23; _p12t{_i_}=_p12&r.m{_i_} + _p12&r.f{_i_}; end; %*--Example: when r=a then the first macro invocation says to calculate the est median variable p13ai1 by using the data distribution values in array _p12t (data from table p12a, summed over sex) and using the array _p12w of constant "interval widths" data. The second median invocation repeats this process but assigns result to p13ai2 and uses the _p12am data distribution array which is the set of male counts. And the 3rd invocation creates p13ai3 as the female median by working with the array of females counts in _p12af.---; %median(mdn=p13&r.i1,intvals=_p12t,intsize=_p12w,samestep=1) ; %*--medians for total pop-; %median(mdn=p13&r.i2,intvals=_p12&r.m,intsize=_p12w,samestep=1) ; %*--medians for males-; %median(mdn=p13&r.i3,intvals=_p12&r.f,intsize=_p12w,samestep=1) ; %*--medians for females-; %mend domedian; %domedian() ;*--Assigns p13i1, p13i2 and p13i3 using data in distribution array p12-; %domedian(a) ;*--Assigns p13ai1, p13ai2 and p13ai3 using data in distribution array p12a-; %domedian(b) ;*--etc.--; %domedian(c) %domedian(d) %domedian(e) %domedian(f) %domedian(g) %domedian(h) %domedian(I) format geocode house senate sldl sldu $char3.; *<-----contain leading blanks!!!!!!----; drop _haf -- _slot2; *--temp vars used by median macro--; if sumlev='610' then output sf1.mosenateph; else output sf1.mohouseph; run; *---verify that the total pops agg to state total---; proc means data=sf1.mosenateph(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mosenateph'; run; proc means data=sf1.mohouseph(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mohouseph'; run; *-------------------------------------------------------------------------------------------------------------*; *---Part 2: Allocate the pct tables to the districts. This involves allocating with disaggregation from 080 sumlevs-; *---begin by generating a split tract (sumlev 080) to leg. district corr. list---*; data blks1(compress=no) ; set sf1.mogeos(keep=sumlev &geos80 sldu sldl pop100); where sumlev='101' and pop100; sumlev='610'; sldcode=sldu; output; sumlev='620'; sldcode=sldl; output; drop sldu sldl; run; proc sort; by sumlev &geos80 sldcode; run; *--This corrwt utility macro turns this into a correlation list relating sumlev/level 080 groups to districts. There are really two separate corr lists here because of the use of sumlev. A typical output obs created here will have values for all the g1 and g2 codes (each unique combination), pop100 (containing the total population of the intersection) and afact, the "allocation factor", with a value between 0 and 1 indicating the decimal portion of the sumlev 080 area that falls within the sldcode (state leg district) area. ---; %corrwt(setin=blks1,setout=corrlsts, g1=sumlev &geos80, g2=sldcode, weight=pop100,keepwt=1) run; proc sort; by &geos80 sumlev sldcode ; run; *---merge the correlation list with the 080 level summaries in the pct sf1 data set--; data merged; merge corrlsts (in=inclist rename=(sumlev=sumlevC) drop=pop100 ) sf1.mopct(in=insf1 where=(sumlev='080' and pop100) keep=sumlev &geos80 pop100 hu100 pct1i1 -- pct17Ii75); by &geos80; if inclist; sumlev=sumlevC; drop sumlevC; *--the sumlev on the sf1 data set overrides the one on clist, so we fix it-; if not insf1 then do; _nbad+1; if _nbad le 20 then do; put '***No sf1 data for ' &geos80 ; delete; end; else stop; drop _nbad; end; run; proc sort data=merged; by sumlev sldcode; run; *----Aggregate the pct table data by the districts----; %agg(aggin=merged,aggout=aggoutpct, aggby=sumlev sldcode , facvar=afact, aggvars=pop100 hu100 pct1i1 -- pct17Ii75, naggvars=1920, agglvls=1) run; data sf1.mosenatepct(label='Missouri Senate Districts PCT Tables (est)' drop=sldl house compress=yes) sf1.mohousepct(label='Missouri House Districts PCT Tables (est)' drop=sldu senate compress=yes); length sumlev $3 geocode sldl sldu senate house $3 areaname $40 ; set aggoutpct; geocode=sldcode; if sumlev='610' then do; sldu=sldcode; senate=sldcode; areaname='State Senate District '||left(senate); end; else do; sldl=sldcode; house=sldcode; areaname='State House District '||left(house); end; drop _lvl_ _nag_ sldcode; format geocode house senate sldl sldu $char3.; *<-----contain leading blanks!!!!!!----; label senate='State Senate District' house='State House District'; if sumlev='620' then output sf1.mohousepct; else output sf1.mosenatepct; run; proc means data=sf1.mosenatepct (keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mosenatepct'; run; proc means data=sf1.mohousepct(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mohousepct'; run;