x cd /pub/data/sf12000/Tools; %let pgm=aggsf1_moslds02; filename pgm "&pgm..sas"; /*----------------------------------------------------------------------------------------------------------------- This code aggregates sf12000 data to Missouri SLDs - State Legislative Districts, AS DEFINED AFTER REDISTRICTING. I.e., that go into effect in 2002 (or late 2001 but the first elections effected are in 2002). We use the block to district correlation files obtained from Ryan Burson and processed by the molegdists2002.sas module in corrlst/Tools. Creates the SAS datasets: sf1.mosenate02ph: Missouri state senate districts - P and H tables. sf1.mosenate02pct: Missouri state senate districts - PCT tables sf1.mohouse02ph: Missouri state house districts - P and H tables. sf1.mohouse02pct: Missouri state house districts - PCT tables sf1.mocd108ph: Missouri 108th Cong. districts - P and H tables. sf1.mocd108pct: Missouri 108th Cong. districts - PCT tables Coded by John Blodgett, OSEDA, U. of Missouri, blodgettj@umsystem.edu Under contract with the Missouri Census Data Center Revision History: 12.31.01: Coding begins. Modeling after aggsf1_moslds.sas 01.07.02: Fixing problem with geocode var on the 3 ph sets. 02.26.02: Added steps to fix problems with geocode on 2 cd108 sets. Ran fix interactively. ------------------------------------------------------------------------------------------------------*/ libname sf1 '/pub/data/sf12000'; *libname corrlst '/pub/data/corrlst' access=readonly; *<---We create the file georef.moblks00 to combine info from various sources, including all the correlation lists we originally were going to have to access.--; libname georef '/pub/data/georef' access=readonly; libname user '/SAS_temp/user'; *<===Optional. Permissions on this directory limit who can use it; title 'sf12000 Aggregation Run: To Missouri State Legislative Districts After Redistricting: aggsf1_moslds02'; 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; set sf1.moph; where sumlev='101'; geo_id_sf1=geo_id; set georef.moblks00(keep=geo_id_sf1 sldu02 sldl02 cd108) key=geo_id_sf1; if _error_ then do; _nerr+1; if _nerr le 20 then put _all_; else if _nerr gt 100 then abort abend; _error_=0; end; drop _nerr; *---Output once for each district type--; keep sumlev sldcode _numeric_; sumlev='610'; sldcode=sldu02; output; sumlev='620'; sldcode=sldl02; output; sumlev='500'; sldcode=cd108; 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.mosenate02ph(label='Missouri State Senate Districts P & H Tables' drop=sldl house cd108 compress=yes) sf1.mohouse02ph(label='Missouri State House Disticts, P & H tables' drop=sldu senate cd108 compress=yes) sf1.mocd108ph(label='Missouri 108th Cong District, P & H Tables' drop=sldu senate sldl house compress=yes); length sumlev $3 geocode $3 cd108 $2 sldu senate sldl house $3 areaname $40 ; set aggoutsf1; *by sumlev sldcode; geocode=sldcode; if sumlev='500' then do; cd108=sldcode; areaname="Cong District " ||cd108||" (108th Congress)"; end; else 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.mosenate02ph; else if sumlev='620' then output sf1.mohouse02ph; else output sf1.mocd108ph; run; *---verify that the total pops agg to state total---; proc means data=sf1.mosenate02ph(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mosenate02ph'; run; proc means data=sf1.mohouse02ph(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mohouse02ph'; run; proc means data=sf1.mocd108ph(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mocd108ph'; run; *-------------------------------------------------------------------------------------------------------------*; /*<=========================Comment out===================================== *---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 georef.moblks00(keep=sumlev &geos80 sldu02 sldl02 cd108 pop100); where sumlev='101' and pop100; sumlev='610'; sldcode=sldu02; output; sumlev='620'; sldcode=sldl02; output; sumlev='500'; sldcode=cd108; output; drop sldu02 sldl02 cd108; 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.mosenate02pct(label='Missouri Senate Districts 2002 PCT Tables (est)' drop=sldl house cd108 compress=yes) sf1.mohouse02pct(label='Missouri House Districts 2002 PCT Tables (est)' drop=sldu senate cd108 compress=yes) sf1.mocd108pct(label='Missouri 108th cong Districts PCT Tables (est)' drop=sldu senate sldl house compress=yes) ; length sumlev $3 geocode sldl sldu senate house $3 cd108 $2 areaname $40 ; set aggoutpct; geocode=sldcode; if sumlev='500' then do; cd108=sldcode; areaname="Cong District " ||cd108||" (108th Congress)"; end; else if sumlev='610' then do; sldu=sldcode; senate=sldcode; areaname='State Senate District '||left(senate); end; else if sumlev='620' then do; sldl=sldcode; house=sldcode; areaname='State House District '||left(house); end; drop _lvl_ _nag_ sldcode; format geocode house senate sldl sldu $char3.; *<---no leading blanks on these as with originals--; label senate='State Senate District (2002)' house='State House District (2002)'; if sumlev='620' then output sf1.mohouse02pct; else if sumlev='610' then output sf1.mosenate02pct; else output sf1.mocd108pct; run; proc means data=sf1.mosenate02pct (keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mosenate02pct'; run; proc means data=sf1.mohouse02pct(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mohouse02pct'; run; proc means data=sf1.mocd108pct(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mocd108pct'; run; ===========================End commented out================================ */ %macro prntchar(set,morevars=obs=20); title2 "Partial Listing of &set"; proc print data=&set(keep=_char_ &morevars obs=&obs); run; %mend prntchar; %prntchar(sf1.mosenate02ph); %prntchar(sf1.mosenate02pct,morevars=); %prntchar(sf1.mohouse02ph); %prntchar(sf1.mohouse02pct,morevars=); %prntchar(sf1.mocd108ph); %prntchar(sf1.mocd108pct,morevars=); %prntchar(georef.moblks00,morevars=); *<========================Edit the geocode variable on the cd108 data sets==========; data sf1.mocd108ph; length geocode $5; set sf1.mocd108ph_temp; geocode='29-'||cd108; run; data sf1.mocd108pct; length geocode $5; set sf1.mocd108pct_temp; geocode='29-'||cd108; run;