%macro circaggacs( revdate=%str(6/3/2014 9:02:26 AM), /* Being created specifically to play a role within the caps10acs websas module, 5/13. Adapted from circagg10c.sas macro */ setin=acs.ustracts5yr, acsvintage=, setout=aggout, /* name of aggregated dataset */ 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) */ idvars=, /* ID variables carried along by cumagg and kept on output set. A Sitename variable is also defined and kept with value of &sitename parm. */ 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 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 countees by using the georef.usbbbb2010 dataset to determine what counties the circle might intersect. */ bbbbset=georef.usbbbb2010, 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 sets contain the long and lat values, resp. */ usecumagglastnaggvars=0, /* if 1 is specified then we pass cumagg same value of naggvars as calculated in prior call */ debug=0); %if &debug %then %put ***** circaggacs macro, rev. &revdate is being compiled *****; %*---Check for and assign aliases----------; %****global _states _setsin _counties statescsq state0; %global errmsg; %local lon1; %let lon1=%substr(&x0,1,1); %if &debug %then %put lon1= &lon1; %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; %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 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 %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; %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; %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; cklast: if last then do; call symput('counties',trim(_counties)); %if &debug %then %str( put '*****Edited value of _counties is ' _counties= ; ); end; run; %end; 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; set &setin; %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 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); %end; %else %&filter ; %end; %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--; *---this code should only have to execute once.---; if xlong0r=. then do; 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)); 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; drop totpop_moe -- cashrenterOver750_moe; drop xlongr ylatr; 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; *** distance; run; %local avparmsfx; %if %index(&setin,usbgs) %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; %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=&idvars sitename latitude longitude vintage period, agglvl=1 , vars= LandSQMI AreaSQMI &samplecountsetc TotPopsf1 TotPop--PctCashRenterOver750, facvar= , dropvars=_lvl_ _nag_, grand=0 , naggvars=&_naggvars, nmeans=0 , nmwts=0 , means=intptlat intptlon &acsmeans &pctvars, meanwts=totpop totpop &acsmwgts &pctwts, debug=0) run; %endmac: %mend circaggacs;