x cd /pub/data/sf12000/Tools; %let pgm=aggsf1_mislds; filename pgm "&pgm..sas"; /*----------------------------------------------------------------------------------------------------------------- This code aggregates sf12000 data to Michigan SLDs - State Legislative Districts (revised as of 2002!). Creates the SAS datasets: sf1.misenateph: Michigan state senate districts - P and H tables. sf1.misenatepct: Michigan state senate districts - PCT tables sf1.mihouseph: Michigan state house districts - P and H tables. sf1.mihousepct: Michigan state house districts - PCT tables sf1.micd108ph: Michigan 108th congressional districts - PH tables. sf1.micd108pct: Michaing 108th cds - PCT tables Coded by John Blodgett, OSEDA, U. of Missouri, blodgettj@umsystem.edu Under contract with the Missouri Census Data Center Revision History: 11.05.02: Coding begins. Adapting from model for Missouri. ------------------------------------------------------------------------------------------------------*/ libname sf1 '/pub/data/sf12000'; libname corrlst '/pub/data/corrlst'; libname user '/tmp/scratch/user'; *<===Optional. Permissions on this directory limit who can use it; title 'sf12000 Aggregation Run: To Michigan State Legislative Districts: aggsf1_mislds'; 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; *<--geocodes making up summary level 080---; /*<=========================Comment out===================================== *---Create an index on the block level correlation list so we can do keyed lookups. Would be nice to do a simple merge but the sf1 blocks dataset is not sorted as you would expect by county tract block (the mcd-place hierarchy order instead.)---; proc datasets nolist library=corrlst; modify mibl2slds02; index create cotrblk=(county tract block) /unique; quit; data agginsf1(compress=yes); length sumlev $3 sldcode $3; *merge sf1.migeos(where=(sumlev='101' and pop100) in=keep) * sf1.miphng; *by geo_id; *if keep; set sf1.miphblks; *---be sure we have a county-tract-block index to permit this lookup--; set corrlst.mibl2slds02 key=cotrblk; if _error_ then do; _error_=0; if pop100=0 then delete; _nerr+1; if _nerr le 20 then put &geos80 _iorc_= ; else if _nerr gt 100 then abort abend; end; drop _nerr; *---Output once for each district type--; keep sumlev sldcode _numeric_; sumlev='610'; sldcode=sldu; output; sumlev='620'; sldcode=sldl; 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.misenateph(label='Michigan State Senate Districts (rev. 2002) P & H Tables' drop=sldl house compress=yes) sf1.mihouseph(label='Michigan State House Disticts (rev. 2002) P & H tables' drop=sldu senate compress=yes) sf1.micd108ph(label='Michigan 108th Cong District, P & H Tables' drop=sldu senate sldl house compress=yes); length sumlev $3 geocode sldu senate sldl house $3 areaname $40 Year $4; retain year '2002'; set aggoutsf1; *by sumlev sldcode; geocode=sldcode; 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; else if sumlev='500' then do; cd108=sldcode; areaname="Cong District " ||cd108||" (108th Congress)"; 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.misenateph; else if sumlev='620' then output sf1.mihouseph; else if sumlev='500' then output sf1.micd108ph; run; *---verify that the total pops agg to state total---; proc means data=sf1.misenateph(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.misenateph'; run; proc means data=sf1.mihouseph(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mihouseph'; run; proc means data=sf1.micd108ph(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.micd108ph'; run; *-------------------------------------------------------------------------------------------------------------*; ===========================End commented out================================ */ *---Part 2: Allocate the pct tables to the districts. This involves allocating with disaggregation from 080 sumlevs-; *---merge the split-tract (080 level -- NOT 085 as were used for sf3 data allocation) to leg dist. correlation lists with the 080 level summaries in the pct sf1 data set--; data tagged; length SumLev $3 sldcode $3; merge corrlst.mistr0802k_cd108 (in=incds rename=(afact=afactcd) drop=pop2k) corrlst.mistr0802k_sldl02(in=insldL rename=(afact=afactL) drop=pop2k) corrlst.mistr0802k_sldu02(in=insldU rename=(afact=afactU) drop=pop2k) sf1.mipct(in=insf1 where=(sumlev='080') keep=sumlev &geos80 pop100 hu100 pct1i1 -- pct17Ii75); by &geos80; if not insf1 then do; put '***corrlst entry not matched on sf1: ' &geos80; _nosf1+1; if _nosf1 ge 50 then abort abend; drop _nosf1; delete; end; if incds then do; SumLev='500'; sldcode=cd108; afact=afactcd; if afact=. then afact=0; output; end; if insldL then do; SumLev='620'; sldcode=sldl02; afact=afactL; if afact=. then afact=0; output; end; if insldU then do; SumLev='610'; sldcode=sldU02; afact=afactU; if afact=. then afact=0; output; end; drop afactcd afactl afactu; run; proc sort data=tagged; by sumlev sldcode; run; *----Aggregate the pct table data by the districts----; %agg(aggin=tagged,aggout=aggoutpct, aggby=sumlev sldcode , facvar=afact, aggvars=pop100 hu100 pct1i1 -- pct17Ii75, naggvars=1920, agglvls=1) run; data sf1.misenatepct(label='Michigan Senate Districts PCT Tables (est)' drop=sldl house cd108 compress=yes) sf1.mihousepct(label='Michigan House Districts PCT Tables (est)' drop=sldu senate cd108 compress=yes) sf1.micd108pct(label='Michigan 108th cong Districts PCT Tables (est)' drop=sldu senate sldl house compress=yes) ; length sumlev $3 geocode sldl sldu senate house $3 areaname $40 ; set aggoutpct; geocode=sldcode; if sumlev='500' then do; cd108=sldcode; areaname="Cong District " ||cd108||" (108th Congress)"; end; 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 cd108 $char3.; label senate='State Senate District' house='State House District'; if sumlev='620' then output sf1.mihousepct; else if sumlev='610' then output sf1.misenatepct; else if sumlev='500' then output sf1.micd108pct; run; proc means data=sf1.misenatepct (keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.misenatepct'; run; proc means data=sf1.mihousepct(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.mihousepct'; run; proc means data=sf1.micd108pct(keep=pop100) sum n min max mean; var pop100; title 'Means of sf1.micd108pct'; run; %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.misenateph); %prntchar(sf1.misenatepct,morevars=); %prntchar(sf1.mihouseph); %prntchar(sf1.mihousepct,morevars=); %prntchar(sf1.micd108ph); %prntchar(sf1.micd108pct,morevars=); %include sascode(notify);