 x cd /pub/data/dhc2020/Tools; %let pgm=fix_tract_ids; filename pgm "&pgm..sas";

options compress=binary;
options notes nomprint nosymbolgen source source2 ;

libname dhc2020 '/pub/data/dhc2020';
libname test '/pub/data/dhc2020/temp';

%include '/pub/sas/autoexec.sas';

%macro fix_tracts(state, stab);
	
	data blocks;
		set dhc2020.&stab.blocks ;
		length tract2 $7 ;
		label tract2 = "Census Tract";
		tract2 = SUBSTR(geoid, 15, 4)||"."||SUBSTR(geoid, 19, 2);
		drop tract;
		run;
	data dhc2020.&stab.blocks (index=(county));
		retain logrecno geoid esriid sumlev geovar geocomp areaname region division state stab county countycc cousub cousubcc submcd submcdcc concit place placecc tract2 ;
		set blocks;
		rename tract2 = tract ;
		run;

	data bgs150 ;
		set dhc2020.&stab.bgs150 ;
		length tract2 $7 ;
		label tract2 = "Census Tract";
		tract2 = SUBSTR(geoid, 15, 4)||"."||SUBSTR(geoid, 19, 2);
		drop tract;
		run;
	data dhc2020.&stab.bgs150 ;
		retain logrecno geoid esriid sumlev geovar geocomp areaname region division state stab county countycc cousub cousubcc submcd submcdcc concit place placecc tract2 ;
		set bgs150;
		rename tract2 = tract ;
		run;

	data bgs090 ;
		set dhc2020.&stab.bgs090 ;
		length tract2 $7 ;
		label tract2 = "Census Tract";
		tract2 = SUBSTR(esriid, 16, 4)||"."||SUBSTR(esriid, 20, 2);
		drop tract;
		run;
	data dhc2020.&stab.bgs090 ;
		retain logrecno geoid esriid sumlev geovar geocomp areaname region division state stab county countycc cousub cousubcc submcd submcdcc concit place placecc tract2 ;
		set bgs090;
		rename tract2 = tract ;
		run;

	data inventory ;
		set dhc2020.&stab.inventory ;
		length tract2 $7 ;
		label tract2 = "Census Tract";
		if tract then tract2 = SUBSTR(esriid, 6, 4)||"."||SUBSTR(esriid, 10, 2);
		drop tract;
		run;
	data dhc2020.&stab.inventory ;
		retain logrecno geoid esriid sumlev geovar geocomp areaname region division state stab county countycc cousub cousubcc submcd submcdcc concit place placecc tract2 ;
		set inventory;
		rename tract2 = tract ;
		run;

	data hierarchal ;
		set dhc2020.&stab.hierarchal ;
		length tract2 $7 ;
		label tract2 = "Census Tract";
		if tract then do;
			if sumlev in ('080', '085') then tract2 = SUBSTR(esriid, 16, 4)||"."||substr(esriid, 20, 2);
			if sumlev in ('140', '144', '154') then tract2 = SUBSTR(esriid, 6, 4)||"."||substr(esriid, 10, 2);
			if sumlev = '158' then tract2 = SUBSTR(esriid, 11, 4)||"."||substr(esriid, 15, 2);
			if sumlev = '511' then tract2 = SUBSTR(esriid, 8, 4)||"."||substr(esriid, 12, 2);
			if sumlev in ('631', '636') then tract2 = SUBSTR(esriid, 9, 4)||"."||substr(esriid, 13, 2);
			end;
		drop tract;
		run;
	data dhc2020.&stab.hierarchal ;
		retain logrecno geoid esriid sumlev geovar geocomp areaname region division state stab county countycc cousub cousubcc submcd submcdcc concit place placecc tract2 ;
		set hierarchal;
		rename tract2 = tract ;
		run;

	data phctr ;
		set dhc2020.&stab.phctr ;
		length tract2 $7 ;
		label tract2 = "Census Tract";
		if tract then do;
			if sumlev in ('080', '085') then tract2 = SUBSTR(esriid, 16, 4)||"."||substr(esriid, 20, 2);
			if sumlev in ('140', '144', '154') then tract2 = SUBSTR(esriid, 6, 4)||"."||substr(esriid, 10, 2);
			if sumlev = '158' then tract2 = SUBSTR(esriid, 11, 4)||"."||substr(esriid, 15, 2);
			if sumlev = '511' then tract2 = SUBSTR(esriid, 8, 4)||"."||substr(esriid, 12, 2);
			if sumlev in ('631', '636') then tract2 = SUBSTR(esriid, 9, 4)||"."||substr(esriid, 13, 2);
			end;
		drop tract;
		run;
	data dhc2020.&stab.phctr ;
		retain logrecno geoid esriid sumlev geovar geocomp areaname region division state stab county countycc cousub cousubcc submcd submcdcc concit place placecc tract2 ;
		set phctr;
		rename tract2 = tract ;
		run;

	%mend fix_tracts ;

/*
%dostates( 10 , mac=fix_tracts );
%dostates( 01 02 04 05 06 , mac=fix_tracts );
%dostates( 08 09 10 11 12 13 15 , mac=fix_tracts );
%dostates( 16 17 18 19 20 21 , mac=fix_tracts );
%dostates( 22 23 24 25 26 27 28 29 30 , mac=fix_tracts );
%dostates( 31 32 33 34 35 36 37 38 , mac=fix_tracts );
%dostates( 39 40 41 42 44 45 46 , mac=fix_tracts );
%dostates( 47 48 49 50 51 , mac=fix_tracts );
%dostates( 53 54 55 56 , mac=fix_tracts );
%dostates( 72 , mac=fix_tracts );

*/

* fix ustracts ; 

data ustracts ;
	set dhc2020.ustracts ;
	length tract2 $7 ;
	label tract2 = "Census Tract";
	tract2 = SUBSTR(esriid, 6, 4)||"."||SUBSTR(esriid, 10, 2);
	drop tract;
	run;
data dhc2020.ustracts ;
	retain logrecno geoid esriid sumlev geovar geocomp areaname region division state stab county countycc cousub cousubcc submcd submcdcc concit place placecc tract2 ;
	set ustracts;
	rename tract2 = tract ;
	run;

