x cd /pub/data/sf12010/Tools; %let pgm=agg_sf12010_mocdslds2012; filename pgm "&pgm..sas"; *---Aggregation of 2010 Summary File 1 complete table data sets to new CDs and SLDs for Mo. Uses code from California SDC and from Ellen Cryan at Cleveland State U. Adapted by jgb, OSEDA/MCDC running Dec 2011. We use the correlation list provided by J Harlan, GRC. We only aggregate the P and H tables from block level - no PCT or HCT. Rerunning 1-11-12 after update to equivalency dataset. Rerunning 4-30-12. The House and Senate should be final but the CDs are still subject to challenge. ---*; libname sf12010 '/pub/data/sf12010'; libname sf1 (sf12010); libname corrlst '/pub/data/corrlst' access=readonly; libname user '/SAS_temp/userb'; %let outset=mocdslds2012; %let aggbyvars=sumlev newdist county; %let aggids=; title "&pgm -Aggregate State Level SF1 Data to US Totals With Regions and Divisions"; %INCLUDE SASCode(CalcMedian) ; *<---from the California SDC, Aug. 2011---; /* ========================begin comment==================================== *---The sf1 dataset is not sorted by county tract and blocks so we shall create an index for that ordering---; proc datasets library=sf1 nolist; modify moblocks; index create ctb=(county tract block); quit; ==========================end comment====================================== */ options compress=binary; *<======================IMPORTANT!!!========================; *----Step 1: Create, sort and filter the input data set by merging block level sf1 with the correlation list with new cds and slds---; data aggin; merge sf1.moblocks(in=insf1 keep=county tract block sumlev _numeric_) corrlst.moblk2cdslds2012(in=inclist); by county tract block; length newdist $3; if not (insf1 and inclist) then do; if not insf1 then _nosf1+1; else _noclist+1; _nbad+1; if _nbad=_n_ and _n_=10 then abort abend; if _nbad le 50 then put county= tract= block= insf1= inclist= ; else abort abend; drop _: ; end; *----We output 3 obs, 1 for each summary level group we want (cd, senate and house)-----; sumlev='500'; newdist=cd113; output; sumlev='610'; newdist=senate12; output; sumlev='620'; newdist=house12; output; run; proc datasets library=user nolist; modify aggin; index create s=( &aggbyvars ); quit; ***proc sort; ***by &aggbyvars; *** run; %agg(aggin=aggin, aggout=aggout, aggby=&aggbyvars, idvars=&aggids, aggvars=pop100 hu100 p1i1--p39Ii20 h1i1--h17Ii21 IntPtLon IntPtLat AreaSQMI LandSQMI AreaLand AreaWatr, means=IntPtLon IntPtLat, mweights=AreaSQMI AreaSQMI, agglvls=3,grand=no, debug=0) run; data sf1.&outset (label="Mo CDs and SLDs as of 2012" compress=binary); length SumLev $3 cd113 $2 House $3 Senate $3 County $5 Geocode $9 Areaname $64 esriid $8 ; *<----must be custom assigned.----; set aggout; *====================================Here is where sumlev, geocode and areaname need to be assigned=====; Select (_lvl_); when(1) select(SumLev); when('500') do; SumLev='510'; cd113=newdist; geocode=cd113||'-'||county; Areaname="CD "||cd113||" / "||put(county,$county.); end; when('610') do; SumLev='612'; senate=newdist; geocode=senate||'-'||county; Areaname="Senate District "||senate||" / "||put(county,$county.); end; when('620') do; SumLev='622'; house=newdist; geocode=house||'-'||county; Areaname="House District "||house||" / "||put(county,$county.); end; end; when(2) select(SumLev); when('500') do; cd113=newdist; geocode=cd113; Areaname="CD "||cd113||" (113th Congress)"; end; when('610') do; senate=newdist; geocode=senate; Areaname="Senate District "||senate||" (2012)"; end; when('620') do; house=newdist; geocode=house; Areaname="House District "||house||" (2012)"; end; end; when(3) do; put Sumlev= pop100= ; delete; end; otherwise do; put _lvl_= " not expected ****"; abort abend; end; end; *--select _lvl_ ---; esriid=compress(geocode,'-'); *---The following code from Ellen Cryan, Cleveland State U. with refs to CalcMedian from California SDC ---; * General macro which calculates any median using linear interpolation. ; * Macro to calculate all 30 median ages (total pop & 9 race groups by sex) using P12, P12A-P12I. Called below for custom-defined areas only. Also called by GenProf3 whenever any of the races J through O are requested) ; ARRAY P12LowBounds [23] _TEMPORARY_ (0, 5,10, 15,18, 20,21,22, 25,30,35,40, 45,50,55, 60,62, 65,67, 70,75,80,85) ; %Macro MedAge(Race) ; ARRAY P12&Race.male23 [23] P12&Race.i3 -P12&Race.i25 ; ARRAY P12&Race.feml23 [23] P12&Race.i27-P12&Race.i49 ; ARRAY P12&Race.Both23 [23] ; DO age = 1 TO 23 ; P12&Race.Both23[age] = P12&Race.male23[age] + P12&Race.feml23[age] ; END ; %CalcMedian (P13&Race.i1, 23, P12LowBounds, P12&Race.Both23, P12&Race.i1) ; %CalcMedian (P13&Race.i2, 23, P12LowBounds, P12&Race.male23, P12&Race.i2) ; %CalcMedian (P13&Race.i3, 23, P12LowBounds, P12&Race.feml23, P12&Race.i26) ; %MEnd ; ARRAY OccHU [3,0:9] H6i1-H6i8 H16Hi1 H16Ii1 /* Hisp & WhiteNH not in H6 & H14 */ H14i2-H14i9 H16Hi2 H16Ii2 /* Owners */ H14i10-H14i17 H16Hi10 H16Ii10 ; /* Renters */ %Macro AvgSize(Race,RNo) ; IF (P18&Race.i1>0) THEN P17&Race.i1=P16&Race.i1/P18&Race.i1 ; * Households ; IF (P18&Race.i1>0) THEN P17&Race.i2=P16&Race.i2/P18&Race.i1 ; * Households ; IF (P18&Race.i1>0) THEN P17&Race.i3=P16&Race.i3/P18&Race.i1 ; * Households ; IF (P35&Race.i1>0) THEN P37&Race.i1=P36&Race.i1/P35&Race.i1 ; * Families ; IF (P35&Race.i1>0) THEN P37&Race.i2=P36&Race.i2/P35&Race.i1 ; * Families ; IF (P35&Race.i1>0) THEN P37&Race.i3=P36&Race.i3/P35&Race.i1 ; * Families ; ***put 'before ' occhu[2,&RNo]= h11&race.i2= h12&race.i2=; IF (OccHU[1,&RNo]>0) THEN H12&Race.i1=H11&Race.i1/OccHU [1,&RNo] ; * all Occ HUnits ; IF (OccHU[2,&RNo]>0) THEN H12&Race.i2=(H11&Race.i2+H11&Race.i3)/OccHU [2,&RNo] ; * OwnerOcc HU ; IF (OccHU[3,&RNo]>0) THEN H12&Race.i3=H11&Race.i4/OccHU [3,&RNo] ; * RenterOcc HU ; *** put occhu[2,&RNo]= h11&race.i2= h12&race.i2=; %MEnd ; * In first call to %MedAge, Race is null (for total pop) ; %MedAge () ; %MedAge (A) ; %MedAge (B) ; %MedAge (C) ; %MedAge (D) ; %MedAge (E) ; %MedAge (F) ; %MedAge (G) ; %MedAge (H) ; %MedAge (I) ; * In first call to %AvgSize, Race is null and zero (for total pop) ; %AvgSize ( ,0) ; %AvgSize (A,1) ; %AvgSize (B,2) ; %AvgSize (C,3) ; %AvgSize (D,4) ; %AvgSize (E,5) ; %AvgSize (F,6) ; %AvgSize (G,7) ; %AvgSize (H,8) ; %AvgSize (I,9) ; *---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.--; drop P12Both: P12ABoth: P12BBoth: P12CBoth: P12DBoth: P12EBoth: P12FBoth: P12GBoth: P12HBoth: P12IBoth: ; drop _: i age; drop newdist; run; proc sort; by sumlev geocode; run; proc print data=sf12010.&outset(keep=_character_ p1i1); by sumlev; id cd113 senate house county geocode; title2 "sf12010.&outset"; run;