/* ================================================================= corrwt22 Programmed by John Blodgett with revisions by Glenn Rice. Function: Convert correlation list with weight variable to one with allocation factors. Can be used to do "auto-correlation", or as an efficient utility to generate percent-of-total stats. E.g. specify GEOCDS1=STATE, GEOCDS2=STATE COUNTY, WEIGHT=TOTPOP to get results in output data set with afact being percent of state pop in each county. Update history: 10-12-2022: Refactoring code to remove unused features. ==================================================================== */ %macro corrwt22 ( /* Convert weighted correlation list to allocation list. */ revdate=12Oct22, /* Date of last revision. */ setin=, /* Required name of input SAS data set. */ setout=, /* Name of output SAS data set. */ geocds1=, /* Required list of source geocds. The input data set (&setin) must be sorted by these codes. */ geocds2=, /* Required list of target geocds. The allocation factor will represent the decimal fraction of geocds1 that is to be allocated to geocds2. For example: geocds1=county tract80, geocds2=tract90, weight=nsegs,... for each value of county-tract80 corrwt will sum the values of nsegs. Then, for each sub-group based on values of tract90 within the county-tract80 group, the sum of nsegs will be calculated and used as the numerator over the tract80 total to determine the value of the allocation factor. */ xyopt=, /* Null (default) says no coordinates. Value of latlon causes processing of coordinate vars on setin. A weighted average will be taken for each geocds1/geocds2 combination and written to setout. */ latvar=intptlat, lonvar=intptlon, /* In conjunction with xyopt=latlon specified, these 2 parms specify the names of the variables containing the lat and long coordinates in degrees. */ exitout=, /* Value of parm is the name of a macro that the user must ensure is defined prior to invoking corrwt. That macro will be referenced just prior to the output statement. It may contain a delete or return statement to avoid output. */ afacts=1, /* Specify afacts=2 to get afact and afact2, the latter being the portion of the geocods2 area that is in geocds1. */ sort2=0, /* Specify sort2=1 to have final output set sorted by &geocods2, &geocds1 instead of usual &geocds1, &geocds2. */ weight=, /* Required name of variable on &setin to be used in weighting. */ idvars= /* List of additional vars on setin to carry on output data set. Should be values uniquely associated with values of the geocode variables. */ ); options nomprint; %put %str( ); %put ***********************************************; %put * corrwt22 macro rev &revdate begin execution *; %put * Missouri Census Data Center *; %put ***********************************************; %put %str( ); /* Check for required parameters. */ %let err=0; %if %quote(&setin)= %then %do; %put *** ERROR: required parm setin not specified. ***; %let err=1; %end; %if %quote(&setout)= %then %do; %put *** ERROR: required parm setout not specified. ***; %let err=1; %end; %if &weight = %then %do; %put *** ERROR: required parm weight not specified. ***; %let err=1; %end; %if %quote(&geocds1) = %then %do; %put *** ERROR: required parm geocds1 not specified. ***; %let err=1; %end; %if %quote(&geocds2) = %then %do; %put *** ERROR: required parm geocds2 not specified. ***; %let err=1; %end; %local xvar yvar; %let xvar=; %let yvar=; %if &xyopt = latlon %then %do; %let xvar = &lonvar; %let yvar = &latvar; %end; %if &err %then %do; %put; %put *** ERRORS FOUND: macro will not execute. ***; %goto endmac; %end; /* For by statments, we need to get the name of the last variable in the geocds1 and geocds2 lists. */ %let ngeos = 1; %let geocode = %scan(&geocds1, 1); %let lastcd = &geocode; %do %while(&lastcd ne ); %let ngeos = %eval(&ngeos+1); %let lastcd = %scan(&geocds1, &ngeos); %if &lastcd ne %then %let geocode = &lastcd; %end; %let ngeos = 1; %let geocode2 = %scan(&geocds2, 1); %let lastcd = &geocode2; %do %while(&lastcd ne ); %let ngeos = %eval(&ngeos+1); %let lastcd = %scan(&geocds2, &ngeos); %if &lastcd ne %then %let geocode2 = &lastcd; %end; /* Calculate allocation factors from geocds2 to geocds1. */ %if &afacts = 2 %then %do; proc sort data = &setin(keep = &geocds1 &geocds2 &weight) out = _setin; by &geocds2 &geocds1; run; data _totals2; set _setin; by &geocds2; if first.&geocode2 then _total = &weight; else _total + &weight; if last.&geocode2 then output; keep &geocds2 _total; run; data _afacts2; set _setin; by &geocds2 &geocds1; if first.&geocode2 then set _totals2; * To define _total ; if first.&geocode then _total1 = &weight; else _total1 + &weight; if last.&geocode then do; if _total le 0 then afact2 = .; else afact2 = _total1 / _total; output; end; keep &geocds1 &geocds2 afact2; length afact2 4; label afact2 = "&geocode2-to-&geocode allocation factor"; format afact2 6.4; run; proc sort data = _afacts2; by &geocds1 &geocds2; run; %put *** NOTE: _afacts2 dataset generated. ***; %end; * End processing to determine afact2 vars. ; /* Now do normal processing. */ proc sort data = &setin out = _setin2; by &geocds1 &geocds2; run; data _totals; set _setin2; by &geocds1; if first.&geocode then _total = &weight; else _total + &weight; if last.&geocode then output; keep &geocds1 _total; run; %if &sort2 %then %str(data _setout; ); %else %do; data &setout; %end; * Indented below because of 'data' statement above. ; set _setin2(keep = &idvars &geocds1 &geocds2 &xvar &yvar &weight); by &geocds1 &geocds2; if first.&geocode then set _totals; * To define _total ; if first.&geocode2 then _total2 = &weight; else _total2 + &weight; %if &xyopt ne %then %do; if first.&geocode2 then do; _xtotal = &weight * &xvar; _ytotal = &weight * &yvar; end; else do; _xtotal + &weight * &xvar; _ytotal + &weight * &yvar; end; if last.&geocode2 then do; if _total2 then do; &xvar = _xtotal / _total2; &yvar = _ytotal / _total2; * weighted avgs ; end; else do; &xvar = &xvar; &yvar = &yvar; * if weight is 0 use last obs ; end; end; %end; if last.&geocode2 then do; %if &afacts = 2 %then %do; set _afacts2(keep = &geocode2 afact2 rename = (&geocode2 = _geo2_)); * Make sure codes are in sync ; if &geocode2 ne _geo2_ then do; file log; put '*** ERROR: Check geocode on _afacts2 dataset does not match code on setout. Probable logic error. Program aborting. ***' /_all_; abort 888; end; keep afact2; %end; if _total le 0 then afact = .; else afact = _total2 / _total; format afact 5.3; &weight = _total2; %if %superq(exitout) ne %str() %then %&exitout; output; end; %local _afact; %let _afact = afact; keep &geocds1 &geocds2 &_afact &idvars &xvar &yvar &weight; length afact 4; label afact = "&geocode-to-&geocode2 allocation factor"; format afact 6.4; %endmac: %if &sort2 %then %do; run; * now do final sort by target geocodes. ; proc sort data = _setout out = &setout; by &geocds2 &geocds1; run; %end; %mend corrwt22 ;