%macro circaggacs20( setin=acs.ustracts5yr, /* Will also accept acs.usbgs5yr. Contains standard MCDC profile vars for small geographic areas. These data are aggregated to circular areas. */ acsvintage=, 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 description 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-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.usbbbb2020 dataset to determine what states the circle might intersect. */ 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. */ ); %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 %superq(sitename) eq %str() %then %let sitename = %str(%() &x0 , &y0 %str(%)); %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 &_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; libname georef '/pub/data/georef' access=readonly; %let states= ; data _null_; set georef.usbbbb2010 end=last; where sumlev='040'; *-- 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; maxlatC = y0 + _r / 69; minlatC = y0 - _r / 69; _milesperdegreeSouth = cos(minlatC * _d2r) * 69.77545; minlonC = x0 - _r / _milesperdegreeSouth; maxlonC = x0 + _r / _milesperdegreeSouth; retain maxlatC minlatC maxlonC minlonC; 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; retain _states _statescsq; 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('statescsq', 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 = %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"; %*local statescsq; %*let statescsq = &states; *-- Alias to empasize the format of states. ; %if %superq(states) ne %str() %then %do; libname georef '/pub/data/georef' access=readonly; data _null_; set georef.usbbbb2020 end=last; where sumlev='050' ; where also state in (&states); retain _D2R .0174532925; retain x0 &x0 y0 &y0 _r &maxrad; if _n_ = 1 then do; maxlatC = y0 + _r / 69; minlatC = y0 - _r / 69; _milesperdegreeSouth = cos(minlatC * _d2r) * 69.77545; minlonC = x0 - _r / _milesperdegreeSouth; maxlonC = x0 + _r / _milesperdegreeSouth; retain maxlatC minlatC maxlonC minlonC; end; *-- Now see which counties are searchable. ; if minlat > maxlatC or maxlat < minlatC or minlon > maxlonC or maxlon < minlonC then goto cklast; _county2020 = county; *-- If we get there the county is selected. ; length _counties $16000; retain _counties; _counties = trimn(_counties)||' '||county; if _county2020 ne county then _counties = trimn(_counties)||' '||_county2020; * keep both ; cklast: if last then do; call symput('counties',trim(_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; set %do _i = 1 %to &state0; %*--the utility macro stcnvt 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 = %stcnvt(&_state); mable22.&_stab._blocks(keep=state county tract blockgroup block intptlat intptlon pop20 landsqmi areasqmi rename=(blockgroup=bg pop20=totpop)) %end; %if &state0 gt 1 %then %str(open=defer) ; ; *-- Ends set statement. ; *-- 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. ; where totpop or areasqmi; %if %superq(states) ne %str() %then %do; where also state in (&states); *-- Redundant, but just to be sure. ; %end; retain _D2R .0174532925; drop _d2r; if xlong0r = . then do; xlong0r = abs(_x0) * _d2r; ylat0r = _y0 * _d2r; sinylat0 = sin(ylat0r); cosylat0 = cos(ylat0r); end; xlongr = abs(intptlon) * _d2r; ylatr = intptlat * _d2r; distance = 3949.99 * arcos(sinylat0 * sin(ylatr) + cosylat0 * cos(ylatr) * cos(xlong0r - xlongr)); if distance le &maxrad; if distance ne . ; *-- 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; 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; proc summary data=_blkpops noprint sum nway; by state county; class tract &bg radius; var _blkpop _landsqmi _areasqmi; output out=blkradsums sum=; run; *-- Calculate the area values for each radius (cumulative, so that it is area within 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; data _aggin; retain sitename "&sitename" radius afact _blkpop ; merge &Setin(in=in1 ) blkradsums(in=in2); by state county tract &bg; if in2; if totpop; %if %superq(statescsq) ne %str() %then %do; where state in (&statescsq); %end; %if %superq(filter) ne %str() %then %do; *-- If just a single word, we assume it's the name of a macro. Otherwise it's a where clause. ; %if %scan(%superq(filter), 2) ne %str() or (%length(%superq(filter)) gt 8) %then %do; where also %unquote(&filter); %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 / totpop; *-- The allocation factor to be used in doing summing. ; drop _: ; blkpop_ = _blkpop; drop totpop_moe -- cashrenterOver750_moe; format age: over: under: 9.1; *-- So user can see that we have rounding. ; run; 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; 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; %end; *-- Now invoke the cumagg macro to generate cumulative sums by radius for final aggout dataset. ; options NOmlogic; %cumagg(setin=_aggin , setout=aggout, aggby=radius, idvars=sitename x0 y0 vintage period, agglvl=1 , vars= TotPop--CashRenterOver750, facvar=afact , dropvars=_lvl_ _nag_ afact, grand=0 , naggvars=0, nmeans=0 , nmwts=0 , means=intptlat intptlon &acsmeans &pctvars, meanwts=totpop totpop &acsmwgts &pctwts) run; *-- Final step is to merge the specially calculated landsqmi and areasqmi variables with rest of the radius estimates and define a pop density variable, PopPsqmi --; data aggout; merge aggout circle_areas; by radius; attrib PopPSqmi length=5 format=6.1 label='ACS population per square mile (land area)'; poppsqmi = TotPop / LandSQMI; run; %endmac: %mend circaggacs20;