%macro circaggacsb( revdate=%str(10/1/2016 9:48:37 AM), /* Being created specifically to play a role within the caps10acs websas modules. Adapted from circaggacs.sas macro . Implements BBIA: block-based data apportioning algorithm. */ setin=acs.ustracts5yr, /* Will also accept acs.usbgs5yr. Contains standard MCDC profile vars for small geographic areas. These data are aggregated to CAs. */ acsvintage=, setout=aggout, /* name of aggregated (output) dataset */ radii=, /* List of (10 or fewer) radii for which to create summaries. Must be in ascending order, e.g. " 1 5 10" */ x0=, /* x coordinate (longitude) of circle center in degrees (west assumed unless a "+" is coded) */ y0=, /* y coordinate (latitude) of circle center in degrees */ sitename=, /* Optional: describes the point represented by (x0,y0) */ states=usebbox, /* optional comma-delimited quoted list of state fips codes indicating states to search for data. Variable state containing 2-digit (character) fips codes must be on input data set. A value of usebbox (the default) will cause the app to determine a value for states by using the georef.usbbbb2010 dataset to determine what states the circle might intersect. */ counties=usebbox, /* optional list of 5-digit county fips codes indicating counties to search for data. Variable county containing 5-digit (character) fips codes must be on input data set(s). Can be specfied in addition to or instead of states parm. The default value of usebbox will cause the app to determine a value for counties by using the georef.usbbbb2010 dataset to determine what counties the circle might intersect. */ bbbbset=georef.usbbbb2010, /* block-based bounding box dataset. */ filter=, /* value can be the name of a user written macro or it can be a where clause without the word where. E.g. filter=select, will cause macro to invoke the select macro. filter=%str(county = '29019') will cause a where statement to be generated with this filter applied. You MUST CODE AT LEAST 1 BLANK for the program to recognize a where filter. */ xvar=intptlon, yvar=intptlat, /* names of the variables on input set (&setin) containing the long and lat values, resp. The macro can calculate distance between pt specified by the vars and (&x0,&yo) */ usecumagglastnaggvars=0, /* if 1 is specified then we pass cumagg same value of naggvars as calculated in prior call. Saves processing time when doing "batch caps" runs. */ debug=0 /* Set to 1 to get diagnostic msgs */ ); %*--This is a variation of circaggacs macro with major difference in how we determine the portion of tracts or bgs within the circles. We implement a new algorithm that looks at census block data (sf1, 2010) that allows us to apportion the bg/tract data based on census block inclusions. E.g. a tract has 20 blocks and 14 of them are in with 6 outside the circle. We sum the populations of the 14 and divide by the total pop of the tract to get the apportioning factor (afact).---; %*---input acs data set must have a variable TotPopsf1 with total pop for the area per 2010 census. This can be a potential problem if we have tracts or bgs that are not consistent between the 2010 census and current codes used for ACS data. Such cases are known to occur (as of 9/16) in PIma Co AZ, 2 tracts in Los Angeles and 3 counties in upstate NY. We have dealt with this in our acs2014/Tools modules: mcdcprofiles5yr.sas (for the ustracts5yr dataset) and usbgprofiles.sas (for the usbgs5yr dataset) ---; %*--As of 2017 the acs2015.ustracts5yr and usbgs5yr dsets have NO observation with TotPopsf1 missing! Also, for implementing the bbia we have to reference a block level 2010 SF1 dataset. We use a special dataset mable14.mableblksalt to do some renaming of blocks to get the latest ACS version. Only works for the 3 areas in AZ, Los Angeles and NY . We did not do anything for other areas where changes were trivial. Earlier (than 2014) vintages may have problems. ----; %*---The output file usually has 1 observation per radius specified (radii parm). But, if there are no geographic areas (tracts or bgs) in a circles then no output is generated. So if radii=1 3 5 and setin=usbgs5yr and no block within any BG falls within 1 mile of (&x0,&y0), then there will not be an output row with r=1. ---; %global errmsg; %local lon1; %let lon1=%substr(&x0,1,1); %if %superq(lon1) ne %nrbquote(-) and %superq(lon1) ne %nrbquote(+) %then %let x0=%str(-)&x0; %if &debug %then %put Parms: Radii=&radii x0=&x0 y0=&y0; %if %superq(sitename) eq %str() %then %let sitename=%str(%() &x0 , &y0 %str(%)); %if &debug %then %put _local_ _global_; %local nradii _r r1 r2 r3 r4 r5 r6 r7 r8 r9 r10; *---10 radii maximum---; %let nradii=1; %do %until (&_r = ); %let _r=%scan(&radii,&nradii,' ,'); %let r&nradii=&_r; %if &debug %then %put Radius &nradii = &_r %str( = ) &&r&nradii; %if &_r ne %str() %then %let nradii=%eval(&nradii+1); %end; %let nradii=%eval(&nradii-1); %local maxrad; %let maxrad=&&r&nradii; %*---See if we need to build the "real" value of setsin by checking for states that qualify for searching using bounding box. If the dataset does not exist for that state then we check to see if there is a national dataset that matches the set name pattern and reference the us set instead. We also build the _states string that will be used to generate a where also statement to filter based on state variable. ---; %if %superq(states) = %str() %then %let states=usebbox; %if %superq(states)=usebbox %then %do; %local bblibref bbdataset; %let bblibref=%scan(&bbbbset,1,%str(.)); %let bbdataset=%scan(&bbbbset,2,%str(.)); %libname(&bblibref); %let states= ; data _null_; set &bbbbset (where=(sumlev='040' ))end=last; *---determine the "bounding box" of the circle with center x0, y0 and radius _r. This BB is in terms of coords minlatC, maxlatC, minlonC, maxlonC in decimal degrees. x0, y0 values are also assumed to be in decimal degrees. ----; *---Get latitude coords by simply going due north and south by radius miles and converting to degrees--; retain _D2R .0174532925; *--- This is ATAN(1)/45: converts degrees to radians; retain x0 &x0 y0 &y0 _r &maxrad; if _n_=1 then do; retain _milesperdegreelat 68.7034; *<--used to be 68.703 . We added .0004 to make box a little bigger 12/2016-; *--We added or subtracted the .002 value to these extrema starting 12-22-16--; maxlatC= y0 + _r/_milesperdegreelat +.002; minlatC = y0 - _r/_milesperdegreelat -.002; _milesperdegreeSouth= cos(minlatC*_d2r)*69.77545; minlonC=x0 - _r/_milesperdegreeSouth -.002; maxlonC=x0 + _r/_milesperdegreeSouth + .002; retain maxlatC minlatC maxlonC minlonC; %if &debug %then %str(***put 'The point ' x0= y0= ' with a radius of ' _r ' yields a "bounding box" of ' minlonc= minlatc= maxlonc= maxlatc=; ); end; *----Now see which states are searchable----; if minlat > maxlatC or maxlat < minlatC or minlon > maxlonC or maxlon < minlonC then goto cklast; *---if we get here the state is selected---; length _states $200 _statescsq $400; *** _setsin $500 _setsfx $30 _setname $40; retain _states _statescsq; *** _setsin; retain _missingset 0; _states=trimn(_states)||' '||state; _state0+1; if _state0=1 then _statescsq=quote(state); else _statescsq=trimn(_statescsq)||','||quote(state); cklast: if last then do; call symput('_states',trim(_states)); call symput('state0',put(_state0,2.)); call symput('states',trim(_statescsq)); put '***' _state0= _statescsq=; end; run; %*---This check for a bad coordinate pair added 11-2-16---; %if %nrbquote(&_states)=%str() %then %do; %*let errmsg=&x0 %str(,) &y0 %nrbquote(These coordinates not found in any US state. No data were selected to aggregate.); %*let errmsg= Coordinates &x0, &y0 not found in any US state. No data were selected. ; %let errmsg=%str(Coordinates) &x0 %str(,) &y0 %str(are not found in any US state. No data were selected to aggregate.); %goto endmac; %end; %end; %else %if %length(&states)=2 %then %let states="&states"; %*--added 5-31-14. Only works when 1 state specified--; %local statescsq; %let statescsq=&states; %*---let this be an alias to empasize the format of states---; /* %if %superq(_states) ne %str() %then %let states=&_states; %else %let _states=&states; */ %if %superq(counties)=usebbox and %superq(states) ne %str() %then %do; libname georef '/pub/data/georef' access=readonly; data _null_; set &bbbbset end=last; where sumlev='050' ; where also state in ( &statescsq ); retain _D2R .0174532925; *--- This is ATAN(1)/45: converts to radians; retain x0 &x0 y0 &y0 _r &maxrad; if _n_=1 then do; maxlatC= y0 + _r/68.824; minlatC = y0 - _r/68.824; _milesperdegreeSouth= cos(minlatC*_d2r)*69.77545; minlonC=x0 - _r/_milesperdegreeSouth; maxlonC=x0 + _r/_milesperdegreeSouth; retain maxlatC minlatC maxlonC minlonC; if &debug then put 'The point ' x0= y0= ' with a radius of ' _r ' yields a "bounding box" of ' minlonc= minlatc= maxlonc= maxlatc=; end; *----Now see which counties are searchable----; if minlat > maxlatC or maxlat < minlatC or minlon > maxlonC or maxlon < minlonC then goto cklast; _county2010=county; %include "/pub/sas/code/county_recodes201x.sas"; %if &debug %then %do; put county $county.; %end; *---if we get there the county is selected---; length _counties $16000; retain _counties; _counties=trimn(_counties)||' '||county; if _county2010 ne county then _counties=trimn(_counties)||' '||_county2010; *<--keep both---; cklast: if last then do; call symput('counties',trim(_counties)); %if &debug %then %str( put '*****Edited value of _counties is ' _counties= ; ); end; run; %end; *----Here we create a data set at census block level with radius (donut area) where found. Has the block pop which will be used in subsequent step to create an allocation factor for the _setin data set.----; %global units bg; %if &units=bgs %then %let bg=bg; %else %let bg=; data _blkpops; retain _x0 &x0 _y0 &y0; retain xlong0r ylat0r sinylat0 cosylat0; *** array _radius(&nradii) _temporary_ (&radii); %if &debug %then %put _all_; set %do _i=1 %to &state0; %*--the utility macro st2stab returns the state postal abbreviation-; %local _state _stab; %if %superq(_states) ne %str() %then %let _state=%scan(&_states,&_i); %else %let _state=&&state&_i; %let _stab=%st2stab(&_state); sf12010x.&_stab.blocks(keep=state county tract bg block intptlat intptlon totpop landsqmi areasqmi) %end; %if &state0 gt 1 %then %str(open=defer) ; ; *--ends set statement--; ****sf12010x.&stab.blocks(keep=state county tract bg block intptlat intptlon totpop landsqmi areasqmi); where totpop or areasqmi; *--we can ignore uninhabited blocks since we are allocating based on portion of pop but to get the landsqmi and areasqmi correct we DO have to keep everything--; %if %superq(statescsq) ne %str() %then %do; where also state in ( &statescsq ); %*--redundant but just to be sure--; %end; %include "/pub/sas/code/county_recodes201x.sas"; %if %superq(counties) ne %str() %then %do; where also county in ( %qlist(&counties) ); %end; retain _D2R .0174532925; *--- This is ATAN(1)/45: converts to radians; drop _d2r; *--convert coords from degrees to radians--; if xlong0r=. then do; *---this code should only have to execute once. The values assigned here are retained.---; xlong0r=abs(_x0) * _d2r; ylat0r=_y0 * _d2r; sinylat0=sin(ylat0r); cosylat0=cos(ylat0r); end; xlongr=abs(&xvar) * _d2r; ylatr=&yvar * _d2r; *--This distance algorithm coded by jgb from a formula found in an old Schaums Outline book back in the 70s. Seems to work OK.--; distance=3949.99*arcos(sinylat0 * sin(ylatr) + cosylat0 * cos(ylatr) * cos(xlong0r- xlongr)); *--*****Edited value of _counties is _counties=31031 31045 31161 31165 46007 46033 46047 46055 46071 46103 46113-; if distance le &maxrad; if distance ne .; *--added 5-5-16--; /* --- if county='51515' then county='51019'; %*---Bedford VA independent city now part of Bedford co--; if county='02270' then county='02158'; %*---Wade Hampton AK is now Kusilwak--; if county='46113' then county='46102'; %*---Shannon, SD becomes Oglala Lakota --; --- */ %include "/pub/sas/code/county_recodes201x.sas"; *---In this set of nested if-else statements we find the first (smallest) radius distance falls within--; if distance le &r1 then radius=&r1; %if &nradii gt 1 %then %do i=2 %to &nradii; else if distance le &&r&i then radius=&&r&i; %end; output; keep state county tract bg totpop radius; rename totpop=_blkpop; keep block distance; *--for now... --; keep landsqmi areasqmi; format landsqmi areasqmi best9.; ***; rename landsqmi=_landsqmi areasqmi=_areasqmi; format distance 7.2; run; proc sort data=_blkpops; by state county tract block; run; %*--If we had to change a county code then we need to sort--; %if %index(&counties,51515) %then %do; proc sort; by state county; run; %end; %*---Some blocks have been renamed post-2010 for use with ACS data. We need to substitute the ACS code for the old 2010 SF1 code. We have this data set created in mable14/Tools/alt_tractbg that lets us do the lookup.---; *proc sql noprint; * select distinct county into :altcounties separated by ' ' from mable14.mableblksalt; * quit; %*put altcounties=&altcounties; %let altcounties=04019 06037 36053 36065 36085; *--these are the counties with block recodes--; libname mable14 '/pub/data/mable14' access=readonly; data _blkpops2; merge _blkpops(in=inblkpops) mable14.mableblksalt(in=inalt keep=state county tract block tractACS bgACS); by state county tract block; if inblkpops; if inalt then do; %if &debug %then %do; put '**block sub: ' county= tract= tractACS= bg= bgacs=; %end; tract=tractACS; bg=bgACS; drop tractACS bgACS; end; run; proc summary data=_blkpops2 noprint sum nway; by state county; class tract &bg radius; var _blkpop _landsqmi _areasqmi; output out=blkradsums sum=; run; *--And now calculate the area values for each radius (cum so that it is area with the circle, not the ring--; proc sort data=blkradsums(keep=radius _landsqmi _areasqmi) out=brs2; by radius; run; data circle_areas; set brs2; by radius; landsqmi + _landsqmi; areasqmi + _areasqmi; if last.radius then output; *--one obs per radius (circle) with cumulative areas calculated--; drop _: ; run; ***proc print data=circle_areas; ***title2 "circle_areas"; *** run; data _aggin; retain sitename "&sitename" radius afact _blkpop ; *--To make these be first on data set--*; merge &Setin (in=in1 ) blkradsums(in=in2); by state county tract &bg; if in2; if totpopsf1; %if %superq(statescsq) ne %str() %then %do; where state in ( &statescsq ); %let _also=also; %end; %if %superq(counties) ne %str() %then %do; where &_also county in ( %qlist(&counties) ); %let _also=also; %end; %if %superq(filter) ne %str() %then %do; %*-- if just a single word we assume its the name of a macro. Otherwise its a where clause --; %if %scan(%superq(filter),2) ne %str() or (%length(%superq(filter)) gt 8) %then %do; where &_also %unquote(&filter); %let _also=also; %end; %else %&filter ; %end; length afact 8; format afact 5.3; length x0 y0 $11; retain x0 y0; retain _x0 &x0 _y0 &y0; if x0=' ' then do; x0=put(_x0,11.6); y0=put(_y0,11.6); label x0="Longitude of Circle Center" y0="Latitude of Circle Center"; end; afact=_blkpop/totpopsf1; *--the allocation factor to be used in doing summing--; /*<=========================Comment out===================================== %if %str(&geocsvfile) ne %str() %then %do; if bg=' ' then bg=' '; *--to avoid warning msg re undefined var--; length _outline $12; drop _outline; %do i=1 %to &nradii; if distance le &&r&i then do; _outline=county||compress(tract,'.')||bg; _pop=totpop; drop _pop; file "&geocsvfile&i"; put _outline +(-1) "," _pop "," afact 6.4 ; end; %end; %end; format distance 7.2; ===========================End commented out================================ */ drop _: ; blkpop_=_blkpop; drop totpop_moe -- cashrenterOver750_moe; format age: over: under: 9.1; *--so user can see that we have rounding---; run; options mprint; data _null_; if 0 then set _aggin nobs=_n; if _n=0 then do; call symput('errmsg','No data were selected to aggregate. This may be due to no populated blocks within your specified latitude/longitude + radii. Or, you may have limited your results to a state outside of the selected areas.'); end; stop; run; %if %superq(errmsg) ne %str() %then %goto endmac; proc sort data=_aggin; by radius; *** distance; run; %local avparmsfx; %if %index(&setin,bgs) %then %let avparmsfx=bg; %else %let avparmsfx=; %let samplecountsEtc=; %*--used to hold variables unique to bgs data set or where var is not in standard variable list--; filename avparms "/pub/data/acs/Tools/aggvarparms&acsvintage.&avparmsfx..sas"; %include avparms/NOsource2; %if &avparmsfx=bg %then %do; %let acsmeans=&acsmeansbg; %let pctvars=&pctvarsbg; %let acsmwgts=&acsmwgtsbg; %let pctwts=&pctwtsbg; %*let samplecountsEtc=persons_sampled hus_sampled poor employedclf; * <-- no longer provided by bureau as of 2015-2019 ; %let samplecountsEtc=poor employedclf; %end; %if &usecumagglastnaggvars %then %let _naggvars=&_cumagglastnaggvars; %else %let _naggvars=0; %local setoutP; %let setoutP=&setout; %*---Now invoke the cumagg macro to generate cumulative sums by radius for final aggout dataset---; options NOmlogic; %cumagg(setin=_aggin , setout=&setoutP , aggby=radius, idvars=sitename x0 y0 vintage period, agglvl=1 , vars= &samplecountsetc TotPopsf1 TotPop--PctCashRenterOver750, facvar=afact , dropvars=_lvl_ _nag_ afact, grand=0 , naggvars=&_naggvars, nmeans=0 , nmwts=0 , means=intptlat intptlon &acsmeans &pctvars, meanwts=totpop totpop &acsmwgts &pctwts, debug=0) run; %if &debug %then %do; data _null_; set &setout (obs=3); put "***&setout values: " radius= persons_sampled= hus_sampled= ; run; %end; *--Final step is to merge the specially calculated landsqmi and arasqmi variables with rest of the radius estimates and define a pop density variable, PopPsqmi --; data &setout; merge &setout circle_areas; by radius; attrib PopPSqmi length=5 format=6.1 label='Pop (ACS) Per Sq Mile Land'; poppsqmi=TotPop/LandSQMI; run; %endmac: %mend circaggacsb;