x cd /pub/data/popests/Tools; %let pgm=casrh_cnvt202X; filename pgm "&pgm..sas"; /* Run program from /pub/data/popests/Tools to keep the outreps correct! Must also create the /pub/data/popests/casrhalt subdirectory before running this. (Can be deleted later.) Convert county and state level age-sex-race-hispanic origin pop estimates. Input data: (all) https://www2.census.gov/programs-surveys/popest/datasets/2020-[yyyy]/counties/asrh/cc-est[yyyy]-all.csv (state) https://www2.census.gov/programs-surveys/popest/datasets/2020-[yyyy]/counties/asrh/cc-est[yyyy]-alldata-[st].csv */ options nosource nosymbolgen nomlogic; %let year = 2022; *<=================== EDIT EACH TIME; %let yr = %substr(&year, 3, 2); %let yearm1 = %eval(&year - 1); %let yrm1 = %substr(&yearm1, 3, 2); %let infiletype = us; %* other possibility is state ; missing X; * The Bureau fills fields with this letter when data is not available. ; libname popests "/pub/data/popests"; libname casrhalt "/pub/data/popests/casrhalt"; libname georef "/pub/data/georef"; %global ts; %include './casrh_formats.sas'; %include '/pub/sas/autoexec.sas'; proc format; * This format table translates the variable name prefix to the race variable code as used in earlier versions. Value 01 to 05 are the [race]-alone code, 06 is multi-race and 07 to 11 are the [race]- alone-or-in-combination values. ; value $racepre 'wa'='01' 'ba'='02' 'ia'='03' 'aa'='04' 'na'='05' 'tom'='06' 'wac'='07' 'bac'='08' 'iac'='09' 'aac'='10' 'nac'='11' other=' '; run; %macro lenlst(list, puthdr=0, delim=, debug=0); %global parserr; %let parserr = 0; %let regdelim = %str(, -); %let newlist = %quote(&list); %let numitems = 0; %let delim = %quote(&delim); %if &delim ne %str() %then %let regdelim = %str(&delim); %let stopscan = false; %do %while (&stopscan = false); %let numitems = %eval(&numitems+1); %let word = %quote(%scan(&newlist, &numitems, ®delim)); %if &word = %str() %then %do; %let stopscan = true; %let numitems = %eval(&numitems-1); %end; %end; %endmac: &numitems %mend lenlst; %macro doit(state, stab); %if &infiletype = state %then %do; filename in "./cc-est&YEAR.-alldata-&STATE..csv"; * read a single state, unzipped ; %end; %else %if &infiletype = us %then %do; filename in "./cc-est&YEAR.-all.csv"; * read the entire US file ; %end; data _null_; * This step checks to see if there is an CR character at the end of input line, indicating a crlf delimiter situation. We call symput to define the ts macro var that can be used to modify the infile stmt of the following input step. ; infile in length=_lline obs=1 lrecl=32760; input; putlog _lline= ; crlf= (substr(_infile_, _lline, 1) = '0d'x); if crlf then call symput('ts', 'termstr=crlf'); else call symput('ts', ' '); stop; run; data casrhalt.&stab.casrh_alt&yr(label="&stab estimates by county, age, sex, race, Hispanic"); infile in dsd missover lrecl=32767 &ts firstobs=2 ; * uses the &ts var from data step just above. ; * infile in dsd missover lrecl=32767 firstobs=2 ; * or, manually set the termstr. ; length County $5 ; length SumLev $3 State $2 _cnty $3 _stname $40 Cntyname $64 Year $2 AgeGrp 3; length Tot_Pop Tot_Male Tot_Female WA_Male WA_Female BA_Male BA_Female IA_Male IA_Female AA_Male AA_Female NA_Male NA_Female TOM_Male TOM_Female WAC_Male WAC_Female BAC_Male BAC_Female IAC_Male IAC_Female AAC_Male AAC_Female NAC_Male NAC_Female NH_Male NH_Female NHWA_Male NHWA_Female NHBA_Male NHBA_Female NHIA_Male NHIA_Female NHAA_Male NHAA_Female NHNA_Male NHNA_Female NHTOM_Male NHTOM_Female NHWAC_Male NHWAC_Female NHBAC_Male NHBAC_Female NHIAC_Male NHIAC_Female NHAAC_Male NHAAC_Female NHNAC_Male NHNAC_Female H_Male H_Female HWA_Male HWA_Female HBA_Male HBA_Female HIA_Male HIA_Female HAA_Male HAA_Female HNA_Male HNA_Female HTOM_Male HTOM_Female HWAC_Male HWAC_Female HBAC_Male HBAC_Female HIAC_Male HIAC_Female HAAC_Male HAAC_Female HNAC_Male HNAC_Female 5 ; label Tot_Pop='Total Population' Tot_Male='Total Males ' Tot_Female='Total Females ' WA_Male='White Alone Males ' WA_Female='White Alone Females ' BA_Male='Black Alone Males ' BA_Female='Black Alone Females ' IA_Male='American Indian & Alaskan Nat Alone Males ' IA_Female='American Indian & Alaskan Nat Alone Females ' AA_Male='Asian Alone Males ' AA_Female='Asian Alone Females ' NA_Male='Natv Hawaiian & Oth Pac Islndr Alone Males ' NA_Female='Natv Hawaiian & Oth Pac Islndr Alone Females ' TOM_Male='Two or More Race Males ' TOM_Female='Two or More Race Females ' WAC_Male='White Alone or in Combination Males ' WAC_Female='White Alone or in Combination Females ' BAC_Male='Black Alone or in Combination Males ' BAC_Female='Black Alone or in Combination Males ' IAC_Male='American Indian & Alaskan Nat Alone or in Comb Males ' IAC_Female='American Indian & Alaskan Nat Alone or in comb Females ' AAC_Male='Asian Alone or in Combination Males ' AAC_Female='Asian Alone or in Combination Females ' NAC_Male='Natv Hawaiian & Oth Pac Islndr Alone or in comb Males ' NAC_Female='Natv Hawaiian & Oth Pac Islndr Alone or in comb Females ' NHWA_Male='Non-hisp White Alone Males ' NHWA_Female='Non-hisp White Alone Females ' NHBA_Male='Non-hisp Black Alone Males ' NHBA_Female='Non-hisp Black Alone Females ' NHIA_Male='Non-hisp American Indian & Alaskan Nat Alone Males ' NHIA_Female='Non-hisp American Indian & Alaskan Nat Alone Females ' NHAA_Male='Non-hisp Asian Alone Males ' NHAA_Female='Non-hisp Asian Alone Females' NHNA_Male='Non-hisp Natv Hawaiian & Oth Pac Islndr Alone Males ' NHNA_Female='Non-hisp Natv Hawaiian & Oth Pac Islndr Alone Females' NHTOM_Male='Non-hisp 2+ Race Males' NHTOM_Female='Non-hisp 2+ Race Females' NHWAC_Male='Non-hisp White Alone or in comb Males' NHWAC_Female='Non-hisp White Alone or in comb Females' NHBAC_Male='Non-hisp Black Alone or in comb Males' NHBAC_Female='Non-hisp Black Alone or in comb Females ' NHIAC_Male='Non-hisp American Indian & Alaskan Nat Alone or in comb Males' NHIAC_Female='Non-hisp American Indian & Alaskan Nat Alone or in comb Females' NHAAC_Male='Non-hisp Asian Alone or in comb Males' NHAAC_Female='Non-hisp Asian Alone or in comb Females' NHNAC_Male='Non-hisp Natv Hawaiian & Oth Pac Islndr Alone or in comb Males' NHNAC_Female='Non-hisp Natv Hawaiian & Oth Pac Islndr Alone or in comb Females' HWA_Male='Hispanic White Alone Males ' HWA_Female='Hispanic White Alone Females ' HBA_Male='Hispanic Black Alone Males ' HBA_Female='Hispanic Black Alone Females ' HIA_Male='Hispanic American Indian & Alaskan Nat Alone Males ' HIA_Female='Hispanic American Indian & Alaskan Nat Alone Females ' HAA_Male='Hispanic Asian Alone Males ' HAA_Female='Hispanic Asian Alone Females' HNA_Male='Hispanic Natv Hawaiian & Oth Pac Islndr Alone Males ' HNA_Female='Hispanic Natv Hawaiian & Oth Pac Islndr Alone Females' HTOM_Male='Hispanic 2+ Race Males' HTOM_Female='Hispanic 2+ Race Females' HWAC_Male='Hispanic White Alone or in comb Males' HWAC_Female='Hispanic White Alone or in comb Females' HBAC_Male='Hispanic Black Alone or in comb Males' HBAC_Female='Hispanic Black Alone or in comb Females ' HIAC_Male='Hispanic American Indian & Alaskan Nat Alone or in comb Males' HIAC_Female='Hispanic American Indian & Alaskan Nat Alone or in comb Females' HAAC_Male='Hispanic Asian Alone or in comb Males' HAAC_Female='Hispanic Asian Alone or in comb Females' HNAC_Male='Hispanic Natv Hawaiian & Oth Pac Islndr Alone or in comb Males' HNAC_Female='Hispanic Natv Hawaiian & Oth Pac Islndr Alone or in comb Females' ; drop _: ; /* %if &infiletype = us %then %do; input sumlev state @; if state > "&state" then stop; %end; input @1 SumLev -- HNAC_FEMALE; */ input SumLev -- HNAC_FEMALE; state = put(input(state, best2.), z2.); if state = "&state"; __n+1; drop __n; _cnty = translate(right(_cnty), '0', ' '); * Sometimes they did not code leading zeroes on the county codes! ; County = state || _cnty; if length(year) = 1 then year = '0' || year; if __N = 1 then do; put _all_; list; end; output; run; data cells&state; length Age $2 Race $2 Sex $1 Hispanic $1 pop 8; length acflag $2; * Used to separate out race-alone (A) vs race-alone-or-in-combination (AC) ; set casrhalt.&stab.casrh_alt&yr ; if agegrp = 0 then delete; * We are only going to output 4-way summaries. ; age = put(agegrp, z2.); * The rest of the variables specify race and sex. Some specify Hispanic. ; array _pop_(*) WA_MALE -- HNAC_FEMALE; * We turn each variable into an output obs with the cell codes. ; length _varname $32 _pre $5 _post $6; do _i_ = 1 to dim(_pop_); pop = _pop_{_i_}; call vname(_pop_{_i_}, _varname); _pre = lowcase(scan(_varname, 1, '_')); _post = lowcase(scan(_varname, 2, '_')); if _pre =: 'h' then do; hispanic = '2'; _pre = substr(_pre, 2); end; else if _pre =: 'nh' then do; hispanic = '1'; _pre = substr(_pre, 3); end; else goto endloop; *** hispanic=' '; if _pre =: ' ' then goto endloop; race = put(_pre, $racepre.); if race = ' ' then do; putlog 'WARNING: Problem with assigning race: ' _all_; abort abend; end; if _post = 'male' then sex = '1'; else if _post = 'female' then sex = '2'; else do; putlog 'WARNING: Problem with assigning sex: ' _all_; abort abend; end; if race le '06' then acflag = 'A'; else acflag = 'AC'; output; endloop: end; *--do _i_ loop---; keep county age sex race hispanic pop year acflag; run; proc sort; by county acflag age sex race hispanic year; run; %macro ai; _i_ = year; rename popest_base=estbase; %mend ai; %macro vsfxs; %global year; %do y = 2020 %to &year; &y %end; %mend vsfxs; %collapse( setsin=cells&state, setout=sumryin&state, byvars=county acflag age sex race hispanic, colvars=pop, varroots=popest_, nslots=%eval( &year-2018), varsufxs=base %vsfxs , assigni=ai, setzero=1, debug=0 ); proc summary data=sumryin&state; by county acflag; class age sex race hispanic; var estbase popest_: ; output out=sumsout&state sum=; run; proc sort; by county age sex race hispanic; run; data popests.&stab.casrh&yr(label="&stab estimates of population by age, race, sex, Hispanic: 2020-&year"); length SumType $4 State $2 Fipco County $5 Age $2 Sex $1 Race $2 Hispanic $1; length estbase popest_2020 - popest_&year 5; * Keep trend variables similar to our SScomYY data sets ; length PopEst 5 PopChange 5 PctChange 4 PopChg_1yr 5 PctChg_1yr 4 ; label PctChange='% change since 2020' pctchg_1yr="% Change 20&yrm1.-20&yr,"; label PopChange='Change since 2020' popchg_1yr="Change 20&yrm1.-20&yr,"; set sumsout&state; by county age sex race hispanic; if first.county then do; fipco = county; format county $county.; state = "&state"; set georef.cbsacos23(keep=cbsa cbsatype); end; SumType=' '; if acflag=' ' or (acflag = 'AC' and race < '07') then delete; * To avoid redundant summaries ; retain fipco state cbsa cbsatype; * Each of the 4 characters of SumType indicate whether a specific category is being utilized on this obs. In the case of the 3rd character, the Race indicator, we use a lowercase r to indicate a single race alone (or multi-race) and an uppercase R to indicate an "alone or in combination" category. ; if age ne ' ' then substr(SumType, 1, 1) = 'a'; if sex ne ' ' then substr(SumType, 2, 1) = 's'; if ('01' le race le '06') then substr(SumType, 3, 1) = 'r'; else if race >= '07' then substr(Sumtype, 3, 1) = 'R'; if hispanic ne ' ' then substr(SumType, 4, 1) = 'h'; Sumtype = compress(Sumtype); if SumType=' ' then SumType = 't'; Popest = popest_&year; PopChange = PopEst - estbase; if estbase then PctChange = 100 * PopChange / estbase; PopChg_1yr = popest_&year - popest_&yearm1; if popest_&yearm1 then PctChg_1yr = PopChg_1yr * 100 / popest_&yearm1; format pctch: 5.1; drop _: ; drop acflag; label EstBase='Estimates Base 2020'; run; %if &state=29 %then %do; proc print data=popests.&stab.casrh&yr(obs=100); title "popests.&stab.casrh&yr "; by county; run; %end; proc sort; by county sumtype; run; proc datasets library=popests nolist; modify &stab.casrh&yr; index create county; index create Sumtype; index create typeco=(County Sumtype); quit; filename in clear; %if &state=29 %then %do; proc freq data=popests.&stab.casrh&yr; * These value label format codes are stored in the casrh_formats.sas file in this directory ; format Sumtype $sumtype. age $age. race $race. sex $sex. hispanic $hispani. ; format cbsa $cbsa.; table SumType Age Race sex hispanic /missing; table cbsa county; run ; %end; data _null_; file stdout; put "Processing completed for &stab"; stop; run; %mend doit; /* ========================begin comment==================================== %doit(29,mo) ; %doit(01,al) ; %dostates(01 02 04 05 06 08 09 10 11 12 13 15 16 17 18 19 20 21 22 23 24 25 26 27 28, msgs=1,names=1) %dostates(30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 53 54 55 56, msgs=1,names=1) %dostates(n=51,names=1,msgs=1); %dostates(10,names=1,msgs=1); ==========================end comment====================================== */ %dostates(n=52, names=1, msgs=1); *------------------------Phase II: Create state summaries dataset------------------*; %macro setname(state,stab); popests.&stab.casrh&yr %mend setname; title "&pgm"; data aggin_casrh/view=aggin_casrh; length stab $2 SumLev $3 Areaname $24; retain SumLev '040'; set %dostates(mac=setname) open=defer ; *drop county fipco cbsa: ; stab = put(state, $fipstab2.); cntyname = areaname; areaname = put(state, $state.); run; proc means data=aggin_casrh nway missing noprint sum ; by state; id SumLev Areaname stab; class SumType age sex race hispanic; var EstBase popest: PopCh: ; output out=usstatescasrh&yr(drop=_type_ _freq_) sum= ; run; data popests.usstatescasrh&yr; set usstatescasrh&yr; * recalculate the pct change vars ; length PctChange PctChg_1yr 4; if EstBase then PctChange = 100 * PopChange / estbase; if popest_&yearm1 then PctChg_1yr = 100 * popchg_1yr / popest_&yearm1; format pctch: 5.1; run; proc freq data=popests.usstatescasrh&yr ; tables state*areaname/list; table stab*SumType; title2 "Freqs on the popests.usstatescasrh&yr dataset"; run cancel; * Phase III: Create national state and county-level 1-dimensional summaries dataset ; %macro setname(state,stab); popests.&stab.casrh&yr %mend setname; data aggin_casrh1/view=aggin_casrh1; length stab $2 SumLev $3 ; retain SumLev '050'; set %dostates(mac=setname) open=defer ; where length(sumtype) = 1; *drop county fipco cbsa: ; stab = put(state, $fipstab2.); run; * Finally, we create a national dataset with summaries for states and counties and with summaries for total pop and all 1-dimensional summaries (a,r,R,s and h). ; data popests.us1dimsums_casrh&yr(label="1 dimensional age, sex, race, hisp summaries for US thru 20&yr" ); set aggin_casrh1 (in=inco) popests.usstatescasrh&yr (where=(length(sumtype) = 1)) ; by state; if sumlev='050' then areaname=put(county, $county.); else areaname=put(state, $state.); run; proc freq data=popests.us1dimsums_casrh&yr ; table state*(sumlev sumtype)/missing ; title2 "Freq Report from popests.us1dimsusms_casrh&yr"; run cancel; proc datasets nolist library=popests; modify us1dimsums_casrh&yr; index create state; index create sumtype; quit;