x cd /pub/data/sf12010/Tools; %let pgm=agg_sf12010; filename pgm "&pgm..sas"; *---Aggregation of 2010 Summary File 1 complete table data sets. Uses code from California SDC and from Ellen Cryan at Cleveland State U. Adapted by jgb, OSEDA/MCDC running Oct 2011. ---*; libname sf12010 '/pub/data/sf12010'; libname sf1 (sf12010); libname user '/SAS_temp/userb'; *----Aggregate state summaries to create sf12010.usregdiv ---; %let outset=usregdiv; %let sumlevin=040; %let aggbyvars=region division; %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---; proc format; VALUE $DIVISIO "1"="New England" "2"="Middle Atlantic" "3"="East North Central" "4"="West North Central" "5"="South Atlantic" "6"="East South Central" "7"="West South Central" "8"="Mountain" "9"="Pacific" ; /* ========================begin comment==================================== *----Step 1: Create, sort and filter the input data set---; *---In this example we combine (merege) the usual p, h, pct and hct with the pchctr tables---; *--We create a us dataset with all but the phctr tables at state level. Then we create a second with the phctr state tables only, and finally we merge those 2 sets to create aggin. ---; data statesphct; %macro doit(state,stab); sf12010.&stab.inventory(where=(sumlev='040' and geocomp='00') keep=sumlev geocomp region division state _numeric_) %mend doit; set %dostates(n=51) open=defer; drop sumlev geocomp; run; data statesphctr; %macro doit(state,stab); sf12010.&stab.phctr(where=(sumlev='040' and geocomp='00') keep=sumlev geocomp region division state _numeric_) %mend doit; set %dostates(n=51) open=defer; drop sumlev geocomp; run; data aggin; merge statesphct statesphctr; by state logrecno; drop logrecno; run; proc sort; by &aggbyvars; run; ==========================end comment====================================== */ %agg(aggin=aggin, aggout=aggout, aggby=&aggbyvars, idvars=&aggids, aggvars=pop100 hu100 p1i1--p39Ii20 PCT1i1--PCT22i21 h1i1--h17Ii21 hct1i1--hct4i13 pct12ai1--pct22ii21 IntPtLon IntPtLat AreaSQMI LandSQMI AreaLand AreaWatr, means=IntPtLon IntPtLat, mweights=AreaSQMI AreaSQMI, agglvls=2,grand=yes, debug=0) run; data sf1.&outset (label="US Totals with Regions & Divisions"); length SumLev $3 Geocode $16 Areaname $80 esriid $16 ; *<----must be custom assigned.----; set aggout; *====================================Here is where sumlev, geocode and areaname need to be assigned=====; Select (_lvl_); when(99) do; sumlev='010'; Areaname="United States"; Geocode=" "; esriid=" "; end; when(1) do; SumLev='030'; geocode=division; esriid=geocode; Areaname="U.S. Division "||division||" - "|| put(division,$divisio.); end; when(2) do; SumLev='020'; geocode=region; esriid=geocode; Areaname="U.S. Region "||region; end; otherwise do; put _lvl_= " not expected ****"; abort abend; end; end; *---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 _: ; run; proc sort; by sumlev geocode; run; proc print data=sf12010.usregdiv(keep=_character_ p1i1); by sumlev; id sumlev; title2 "sf12010.usregdiv";