x cd /pub/data/popests/Tools; %let pgm=casrh_cnvt201X; 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. This is a revision of the casrh_cnvt20yya program. Works only for the post-2010 (thru 2019) data. Found problem with several states (WY, WV, VA, WI, at least) because they had crlf delimiters. Code modified to check for this and insert a termstr=crlf to handle it. Rerunning conversions for all states 6-12-12. 7-2-15: Adding code to create change variables (PopChang, PctChang, PopCh1yr, Pctch1yr and Popest). (These variables will not be added to earlier converted casrh data sets.) */ %let year = 2020; %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 datum 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 translate 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 doit(state, stab); *-- The input data were downloaded from Census Bureau web site in the estimates section. Specifically in May 2009 we accessed http://www.census.gov/popest/counties/asrh/CC-EST2008-alldata.html and from that page we accessed the CSV files for each state (e.g. http://www.census.gov/popest/counties/asrh/files/cc-est2008-alldata-29.csv was the file for state 29 - Missouri. ; %global infiletype; %if &infiletype = state %then %do; filename in "./cc-est&YEAR.-alldata-&STATE..csv"; *<== USED TO READ A SINGLE STATE, UNZIPPED ; %end; %else %if &infiletype = us %then %do; filename in "./cc-est&YEAR.-alldata.csv"; *<== USED TO 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; put _lline=; crlf= (substr(_infile_, _lline, 1) = '0d'x); if crlf then call symput('ts', 'termstr=crlf'); else call symput('ts', ' '); stop; run; %put ts: &ts; 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 ; *<------------------ 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', ' '); *-- Some times they did not code leading zeroes on the county codes! ; County = state || _cnty; if length(year) = 1 then year = '0' || year; ***format year $esttime.; *<-- stop using because we cannot find the source of code. We know the meanings, but... ; if __N = 1 then do; put _all_; list; end; output; run; *-- Following comment is of historic interest only. Apparently things really changed circa 2005 and we had to adapt. ; *-- This step accesses the just-read _alt data and creates something close to the kind of table-cell data we had in 2002 and 2003 (data years). It takes a lot of restructuring since the Bureau totally turned the data upside down. We have the years going down instead of across (rows instead of cols), so we need multiple data steps with sorts in between to try and replicate the old structure. We are going to lose some subtotals that we had before because they are not included in the new data. ; 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; put '**Problem with assigning race: ' _all_; abort abend; end; if _post = 'male' then sex = '1'; else if _post = 'female' then sex = '2'; else do; put '**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_10c=Cen10 popest_10a=EstBase; %mend ai; %macro vsfxs; %global year; %do y = 2010 %to &year; &y %end; %mend vsfxs; options NOmprint; %collapse(setsin=cells&state,setout=sumryin&state, byvars=county acflag age sex race hispanic, colvars=pop, varroots=popest_, nslots=%eval( &year-2007), varsufxs=10c 10a %vsfxs , assigni=ai, setzero=1, debug=0); proc summary data=sumryin&state; by county acflag; class age sex race hispanic; var cen10 estbase popest:; output out=sumsout&state sum=; run; proc sort; by county age sex race hispanic; run; data popests.&stab.casrh&yr(label="&stab ests of pop by age, race, sex, hispanic: 2010-&year"); length SumType $4 State $2 Fipco County $5 Age $2 Sex $1 Race $2 Hispanic $1; length cen10 estbase popest_2010 - popest_&year 5; *-- New variables being added 7-2-15 to keep trend variables similar to our SScomYY data sets ; length PopEst 5 PopChang 5 PctChang 4 PopCh1yr 5 PctCh1yr 4 ; label PctChang='% change since 2010' pctch1yr="% Change 20&yrm1.-20&yr,"; label PopChang='Change since 2010' popch1yr="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.cbsacos20(keep=county cbsa cbsatype); set georef.cbsacos20(keep=cbsa cbsatype); end; SumType=' '; if acflag=' ' or (acflag = 'AC' and race < '07') then delete; *-- to avoid redundant summaries ; **drop acflag; 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; PopChang = PopEst - estbase; if estbase then PctChang = 100 * PopChang / estbase; PopCh1yr = popest_&year - popest_&yearm1; if popest_&yearm1 then PctCh1yr = PopCh1yr * 100 / popest_&yearm1; format pctch: 5.1; drop _:; *<-- drop all variables that start with an underscore ; drop acflag; label Cen10='Census 2010 count' EstBase='Estimates Base 2010'; 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 module 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; options nomprint; /* ========================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) ==========================end comment====================================== */ %dostates(n=51,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 Cen10 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 PctChang PctCh1yr 4; if EstBase then PctChang = 100 * PopChang / estbase; if popest_&yearm1 then PctCh1yr = 100 * popch1yr / 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;