%macro circagg10c( filetype=sf12010xx, /* Specify specific recognized filetypes such as sf12000x, sf32000x, stf901x2, stf903x2 */ setsin=~stab.bgs150, /* SAS dataset(s) containing data to aggregate. The default value says to generate a list of sets from the postal abbreviations for the state followed by bgs150. This assumes that either states=usebbox is specified telling the app to determine the relevant states by checking for bounding boxes or the user explicitly chose more than 1 state. */ radii=, /* List of 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 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(s). A value of usebbox will cause the app to determine a value for states by using the georef.usbbbb2010 dataset to determine what states the circle might intersect. */ sumlev=, /* geographic summary level to use when doing aggregation. Only specify if _setin has more than one level and you want to filter */ 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 _states _setsin _counties statescsq state0; %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; %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 (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 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; 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 there 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); if "&setsin" =: "~stab." then do; _setsfx = substr("&setsin",7); if stab = " " then stab = put(state, $fipstab2.); _setname = "&filetype.."||stab||_setsfx; if exist(_setname, "VIEW") or exist(_setname, "DATA") then _setsin = trimn(_setsin)||' '||_setname; else _missingset = 1; end; cklast: if last then do; call symput('_states', trim(_states)); call symput('state0', put(_state0, 2.)); call symput('statescsq', trim(_statescsq)); if _missingset then do; _setname = "&filetype..us"||_setsfx; if exist(_setname, "DATA") or exist (_setname, "VIEW") then _setsin = _setname; end; if "&setsin" =: "~stab." then call symput("_setsin", trim(_setsin)); end; run; %end; %if %superq(_states) ne %str() %then %let states = &_states; %else %let _states = &states; %if %superq(_setsin) ne %str() %then %let setsin = &_setsin; %else %let _setsin = &setsin; %if %superq(setsin) eq %str() or %substr(%superq(setsin), 1, 1) = %str(~) %then %do; %let errmsg=No data sets were selected to access by circagg macro; %goto endmac; %end; %if %superq(states) ne %str() %then %do; libname georef '/pub/data/georef' access=readonly; data _null_; set georef.usbbbb2010 end=last; where sumlev='050' ; where also state in (%qlist(&states)); retain _D2R .0174532925; 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; end; *-- Now see which counties are searchable. ; if minlat > maxlatC or maxlat < minlatC or minlon > maxlonC or maxlon < minlonC then goto cklast; *-- If we get there the county is selected. ; length _counties $16000; retain _counties; _counties = trimn(_counties)||' '||county; cklast: if last then do; call symput('_counties', trim(_counties)); end; run; %end; %if %superq(_counties) ne %str() %then %let counties = &_counties; data _aggin; retain sitename "&sitename" radius ; retain _x0 &x0 _y0 &y0; retain xlong0r ylat0r sinylat0 cosylat0; array _radius(&nradii) _temporary_ (&radii); rename _x0=Longitude _y0=Latitude; if 0 then set sf12010x.ustracts; set &setsin; %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; retain _D2R .0174532925; drop _d2r; if xlong0r = . then do; xlong0r = abs(_x0) * _d2r; ylat0r =_y0 * _d2r; sinylat0 = sin(ylat0r); cosylat0 = cos(ylat0r); end; %if %nrbquote(&states) ne %str() %then %do; if state ne ' ' then if index("&states", state); %end; %if &sumlev ne %str() %then %do; if SumLev = "&sumlev"; %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; *-- 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; format distance 7.2; run; data _null_; if 0 then set _aggin nobs=_n; if _n=0 then do; call symput('errmsg','_aggin dataset has no observations: no data were selected to aggregate'); end; stop; run; %if %superq(errmsg) ne %str() %then %goto endmac; proc sort data=_aggin; by radius; run; *-- 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 latitude longitude, agglvl=1 , vars= LandSQMI AreaSQMI TotPop--AvgSizeRenter , facvar= , dropvars=_lvl_ _nag_, grand=0 , naggvars=0, nmeans=0 , nmwts=0 , means=PctMale PctFemale PopPSqmi PctAge0_4 PctAge5_9 PctAge10_14 PctAge15_17 PctAge18_19 PctAge20_24 PctAge25_34 PctAge35_44 PctAge45_54 PctAge55_59 PctAge60_64 PctAge65_74 PctAge75_84 PctOver85 MedianAge PctUnder18 PctAge18_24 PctAge25_44 PctAge45_64 PctOver62 PctOver65 PctOver18 PctOver18_male PctOver18_female PctOver21 PctAge15_19 PctAge18_24 PctOver65_male PctOver65_female PctOne_Race_total PctWhite1 PctBlack1 PctIndian PctAsian1 PctHawnPI1 PctOther1 PctMultRace PctWhite2 PctBlack2 PctIndian2 PctAsian2 PctHawnpi2 PctOther2 PctHawaiian PctGuam_or_Cham PctSamoan PctOther_Pac_Islndr PctAsian_indian PctChinese PctFilipino PctJapanese PctKorean PctVietnamese PctOtherAsian PctHispPop PctMexican PctPuertoRican PctCuban PctOtherHispanic PctNon_Hispanic PctWhite_Non_Hisp PctHHpop PctHouseholder PctSpouse PctChild PctOwnChild_under18 PctOther_Relatives PctNonrelatives PctNonreltvUnder18 PctNonreltvOver65 PctUnmarriedPartner PctFamilies PctFamilyKidsLT18 PctMarried_Couple PctMarrdCplKidsLT18 PctFemaleHhldr PctSingleMoms PctNonFamily_HHs PctUnmPartnerHHs PctSameSexPrtnrHHs PctLivingAlone PctHousehldrOver65 PctHHsIndvLT18 PctHHsIndvOvr65 AvgHHoldSize AvgFamilySize PctGQpop PctInstitution_pop PctCorrectionalPop PctNursingHomePop PctOtherInstPop PctNonInstGQPop PctCollegeDorms PctMilitaryQtrs PctOthrNoninstGQPop PctOccupied_Units PctOwnerOccupied PctRenterOccupied PctVacantUnits PctVacantForRent PctVacantForSale PctSeasonal PctOwnerTot PctRenterTot OwnerVacrate RentalVacrate PctPopInOwnerUnits PctPopInRentedUnits AvgSizeOwner AvgSizeRenter ,meanwts=TotPop TotPop landsqmi TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop totpop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop tothhs tothhs tothhs tothhs tothhs tothhs tothhs tothhs tothhs tothhs tothhs tothhs tothhs tothhs families TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop TotPop tot_units Occupied_units Occupied_units tot_units tot_units tot_units tot_units tot_units tot_units OwnerTot RenterTot TotPop TotPop OwnerOccupied RenterOccupied, pcts=PctPopChange, nums=PopChange, denoms=Totpop00 ) run; %endmac: %mend circagg10c;