x cd C:\Users\riceg\Desktop\blkrel\blkrel20; %let pgm=cnvt_blkrel2020; filename pgm "&pgm..sas"; /* Accesses a block 2010 to block 2020 relationship file collection from the USCB. Merges this data with data from the latest mable database (mable18 - 2010 block based). Creates a series of geographic equivalency data sets per state and generates the tractrelation_reports directory of pdf reports. */ %let datapath=%str(C:\Users\riceg\Desktop\blkrel\blkrel20\source\); title "Census 2010 to Census 2020 Geographic Relationship Report" ; libname blkrel20 'C:\Users\riceg\Desktop\blkrel\blkrel20\output'; libname pl942020 'C:\Users\riceg\Desktop\blkrel\pl942020'; libname mable18 'C:\Users\riceg\Desktop\blkrel\mable18'; libname mable22 'C:\Users\riceg\Desktop\blkrel\mable22'; %include 'C:\Users\riceg\Desktop\blkrel\blkrel20\sas\dostates.sas'; %include 'C:\Users\riceg\Desktop\blkrel\blkrel20\sas\corrwt.sas'; %include 'C:\Users\riceg\Desktop\blkrel\blkrel20\sas\cnvtdlm.sas'; %include 'C:\Users\riceg\Desktop\blkrel\blkrel20\sas\agg.sas'; %let state=10; %let stab=de; /* Part 1: Create the 2010 to 2020 relationship and correlation list sets / reports */ %macro xcode; County10 = state10||cnty10; County = state||cnty; label tract10='Tract 2010' tract='Tract 2020' afact='Tract 2010 to Tract 2020 allocation factor'; drop cnty10 cnty; Keep county county10; %mend xcode; %macro doit(state, stab); filename in "&datapath.tab2010_tab2020_st&state._&stab..txt" ; ***Step A***; data &stab.blks (compress=no) blkrel20.&stab.xstate(label="Blocks crossing &stab state line") ; infile in dsd delimiter='|' missover lrecl=255 firstobs=2 termstr=lf obs=max; length state10 $2 cnty10 $3 tract10 $7 blk10 $4 blksfx10 $1 AreaLand10 AreaWater10 5 pflag10 $1 state $2 cnty $3 tract $7 blk $4 blksfx $1 AreaLand AreaWater 5 pflag $1 AreaLandInt AreaWaterInt 5; input state10 -- AreaWaterInt; substr(tract,5,3)='.'||substr(tract,5,2); *--edit 950100 to 9501.00--; substr(tract10,5,3)='.'||substr(tract10,5,2); *if substr(blk10,2,2)='99' and state=state10 then output blkrel20.&stab.wblks; * else ---; if state=state10 then output &stab.blks; else output blkrel20.&stab.xstate; label tract='2020 tract' blk='2020 block' AreaLand='Block land area (2020; square meters)' AreaWater='Block water area (2020; square meters)' AreaLandInt='Area of 2010/2020 block land area intersection'; run; proc sort data=pl942020.&stab.sums(keep=sumlev cnty tract block totpop tothus where=(sumlev='750') rename=(block=blk)) out=pl94sorted; by cnty tract blk; run; *-- At this point we want to add several variables from the 2020 pl94 data set to this: namely the pop20 and hus20 data items. We do the merge by 2020 county, tract and block of 2 data sets not sorted by these variables, by defining indices on both the relevant sets. ---; proc sql; create view &stab.blksSorted as select * from &stab.blks order by cnty, tract, blk; quit; ***Step D***; data &stab.blocks2; merge &stab.blksSorted(in=inclist) pl94sorted (keep=SumLev cnty tract blk TotPop TotHUs rename=(Totpop=Pop20 TotHUs=HUS20) in=inpl94) ; by cnty tract blk; if not (inclist and inpl94) then do; _nbad+1; if _nbad <= 50 then put cnty= tract= blk= inclist= inpl94= ; if inclist; end; drop SumLev _nbad; run; proc sort data=&stab.blocks2 out=&stab.blks; by state10 cnty10 tract10 blk10; run; ***Step F***; data blkrel20.&stab.blks(sortedby=cnty10 tract10 blk10 label='2010 to 2020 census block relationship + mable18' compress=Yes); merge &stab.blks (in=inblkrel) mable18.&stab.blks( in=inmable keep=cnty tract block pop10 hus10 vtd placefp cousubfp rename=(cnty=cnty10 tract=tract10 block=blk10 placefp=placefp10 cousubfp=cousubfp10) ) end=last; *===debug===; * if cnty='001' and substr(blk10, 2, 2) = '99' then put "No longer filtered: " cnty= tract= blk=; if not inmable then _nomable+1; else if not inblkrel then _noblkrel+1; if last then put /'**** missing data counts**** ' _nomable= _noblkrel= ; attrib afactLand length=4 label='Allocation factor based on land intersection' format=5.3; if arealand10 then afactLand = AreaLandInt / AreaLand10; attrib Pop10Int length=4 label='Estimated 2010 population in intersection per afactLand ' format=6.1; if pop10 then Pop10Int = pop10 * afactLand; else Pop10Int = 0; drop _nomable _noblkrel; run; ***Step G***; *-- Create a block 2010 to block 2020 correlation list (equivalency file) --; %corrwt( setin=blkrel20.&stab.blks, geocds1=state10 cnty10 tract10 blk10, geocds2=state cnty tract blk, wtvar=Pop10Int, keepwt=1, afacts=1, exitout=xcode, setout=blkrel20.&stab.bl10_bl20 ); ***Step H***; *-- Create a tract 2010 to tract 2020 correlation list (equivalency file) --; %corrwt( setin=blkrel20.&stab.blks, geocds1=state10 cnty10 tract10, geocds2=state cnty tract, wtvar=Pop10Int, keepwt=1, afacts=1, exitout=xcode, setout=blkrel20.&stab.tr10_tr20 ); run; ***Step I***; options nodate; ods pdf file="C:\Users\riceg\Desktop\blkrel\blkrel20\reports\&statenm..pdf" style=plateau notoc columns=2; title2 "Tract 2010 to tract 2020 equivalencies for state of &statenm"; proc print data= blkrel20.&stab.tr10_tr20 label; by county10; id county10; format county10 $county.; var tract10 tract pop10int afact; footnote h=1 'Report by MCDC using Census Bureau block relationship files as source.'; footnote2 h=1 "The equivalency data for this report is stored in blkrel20.&stab.tr10_tr20 data set in MCDC public archive."; run; ods pdf close; /* %macro Part2 ( ); *-- NOT USED. See usblk20_10geos ; *-- PART 2: Create mable22 datasets linking 2020 blocks to various 2010 geos. Generate CSV files as well. --; ***Step 2A***; proc sort data=blkrel20.&stab.blks out=blks2 ; by cnty tract blk arealandint areawaterint; run; ***Step 2B***; data mable22.&stab.blk10_20geos(sortedby=cnty tract blk label='2020 block to "primary" 2010 geographies' compress=Yes); length State $2 cnty $3 tract $7 blk $4 blksfx $1 Pop10 HUs10 5; set blks2; by cnty tract blk; *if substr(blk,2,2)='99' then delete; if last.blk; *-- We keep the intersection with the largest AreaLandInt and if that is a tie then we use areawaterint to break it --; length bg10 $1; bg10=substr(blk10, 1, 1); run; ***Step 2C***; filename dlmout "C:\Users\riceg\Desktop\mable22\&stab.blk10_20geos.csv"; %cnvtdlm( setin=mable22.&stab.blk10_20geos, names=1, charvars=state cnty tract blk cnty10 tract10 bg10 puma10 cousubfp10 placefp10 vtd10 taz10 ur10 ur90, quotes=0, hquotes=0, numvars=arealandint ) run; %mend Part2; */ %mend doit; options NOmprint; %dostates(n=52,names=1); %*dostates(10,names=1); *run; *-----------------------------------Part 3: -----------------------------------------------------------------; *----This phase does not need to rerun. If and when it does we may need to tweak the code since we slightly changed the variables on the blk10_blk20 data set (replacing county with cnty variables). ---; *-----In this section we create the XXpoptrend2020blks data set with estimated 2010 pop and pl94-based 2020 pop counts for current (i.e. 2020) blocks and with 2020 placefp and cousubfp codes. ---*; %macro doit(state,stab); proc sort data=blkrel20.&stab.bl10_bl20(keep=county tract blk pop10int) out=&stab.temppops; by county tract blk; run; %agg( aggin=&stab.temppops, aggout=pop10_20blks, aggby=county tract blk, aggvars=pop10int, naggvars=1, debug=0 ); *-- so now we have a data set with 2020 geos and 2010 total pop counts aggregated. We now want to merge this our 2020 pl94 data and add some geocodes to create an aggin data set which we shall then use to create a whole hierarchy of summaries : county-tract-bg-block. --; proc sort data= pl942020.&stab.sums( keep=state stab sumlev county tract block totpop tothus placefp cousubfp where=(sumlev='750') rename=(block=blk ) ) out=pl94sorted; by county tract blk; run; data aggin; merge pop10_20blks(in=in1) pl94sorted(in=in2020); by county tract blk; retain state "&state" stab "&stab"; if not in1 then TotPop10 = 0; else TotPop10 = round(pop10int); length bg $1; bg = substr(blk, 1, 1); label totpop='Total 2020 population' tothus='Total 2020 housing units'; drop sumlev pop10int; run; proc means data=aggin noprint; id state stab; class county tract bg blk placefp cousubfp; types county * tract * bg *blk *cousubfp * placefp county*tract*bg county*tract county placefp county*placefp county*cousubfp ( ) ; var totpop tothus totpop10 ; output out=freqout sum=; run; data blkrel20.&stab.poptrend2020blks blkrel20.&stab.poptrend2020bgtrcopl(drop=blk); length State $2 County $5 SumLev $3; set freqout; select (_type_); when(63) SumLev='100'; *---census block summary but with placefp and cousbfp carried along--; when(56)SumLev='150'; when(48)SumLev='140'; when(32)SumLev='050'; when(0) SumLev='040'; when(2) SumLev='160'; when(34)SumLev='155'; when(33)SumLev='060'; end; *-- drop _freq_ _type_; if sumlev='100' then output blkrel20.&stab.poptrend2020blks; else output blkrel20.&stab.poptrend2020bgtrcopl; drop _type_ _freq_; run; %mend doit; %dostates(n=52,names=1) ; %*dostates(10,names=1);