x cd /pub/data/corrlst/Tools; %let pgm=us_zip2020_state; filename pgm "&pgm..sas"; * Create an equivalency dataset linking 2020 ZCTA5 values with states. Input is a csv file generated using Geocorr 2022. We specified Zip codes as source geo and county, place as target with 2020 pop as weight variable. We used it in setup to create zip2020_cntyplace and now we are just tweaking the setup to do it at the state level. ; title 'Creating a national ZCTA (2020) to state correlation list'; options nodate mprint nosymbolgen; libname corrlst '/pub/data/corrlst'; filename csvin "/pub/data/corrlst/uszip20202cntyplace.csv"; %include "/pub/sas/autoexec.sas"; data zipcoplc; infile csvin dsd lrecl=512 firstobs=2 termstr=crlf; length ZCTA5 $5 ZCTAName $40 State $2 Stab $2 _County $5 _CntyName $32 _PlaceFP $5 _PlaceNm $40 Pop2020 4 _afact 8; input ZCTA5--pop2020; if zcta5 = ' ' then do; * there are some blocks that are not assigned a ZCTA value. We assign these the pseudo-value 99999. ; zcta5='99999'; zctaname='Area with no ZCTA/ZIP assigned'; end; drop _: ; run; proc sort; by zcta5 state; run; *---aggregate by zip and state---; %agg(setin=zipcoplc,setout=zipstate, aggby=zcta5 state, idvars=stab, aggvars=pop2020, debug=0); proc sort data=zipstate; by zcta5 descending pop2020; run; data corrlst.us_zip2020_state(compress=no label='2020 ZIP code to state equivalency' sortedby=zcta5); set zipstate(in=in1 ) zipstate(in=in2); by zcta5; if first.zcta5 then do; ZipTotPop=0; Nparts=0; Primary=1; retain Primary; end; if in1 then do; ZipTotPop + pop2020; Nparts+1; delete; end; *---On first pass we just aggregate to get total pop for the ZIP-; *---If we get here it is the second pass and we want to process and keep---; PctPop=100*Pop2020/ZipTotPop; output; Primary=0; *---this flag is 1 for the first obs associated with the ZCTA which means the one with largest Pop2020 value.---; label Primary='Primary State Flag' Pctpop='Pop2020 as % of ZipTotPop'; format Primary 2. PctPop 5.1; drop _: ; run; proc datasets library=corrlst nolist; modify us_zip2020_state; index create state; quit;