x cd /pub/data/popests/Tools; %let pgm=poptrends_by_age_SQL; filename pgm "&pgm..sas"; options nosource nosource2 nosymbolgen nomprint nonotes; /* This program is used to generate SQL code for updating the Population Estimates by Age app (https://mcdc.missouri.edu/applications/population/by-age/). It reads the custom single-year-of-age x bridged race x sex and hispanic estimates downloaded from NCHS web site (http://www.cdc.gov/nchs/nvss/bridged_race/data_documentation.htm). Use the multi-year file, e.g. pcen_v2017_y1017.sas7bdat.zip & unzip it. */ %let work_dir = /home/riceg/work; %let source = pcen_v2019_y1019; *<-- CHANGE THIS ; %let lastyr=2019; *<-- CHANGE THIS ; %*let lastyr=2011; *<-- testing ; libname user "&work_dir"; %macro doit; %do yr=2010 %to &lastyr; %let year_col = POP&yr; %if &yr = 2010 %then %let year_col = POP2010_jul; %put Starting &yr; proc sql; *-- totals ; create table counties_&yr as select st_fips, co_fips, ('total') as demographic_code, sum(&year_col) as population, age from &source group by st_fips, co_fips, (calculated demographic_code), age; *-- sex ; insert into counties_&yr (st_fips, co_fips, demographic_code, population, age) select st_fips, co_fips, (case when 1 = racesex then 'd1' when 2 = racesex then 'd2' when 3 = racesex then 'd1' when 4 = racesex then 'd2' when 5 = racesex then 'd1' when 6 = racesex then 'd2' when 7 = racesex then 'd1' when 8 = racesex then 'd2' end) as demographic_code, sum(&year_col), age from &source group by st_fips, co_fips, (calculated demographic_code), age; *-- non/Hispanic ; insert into counties_&yr (st_fips, co_fips, demographic_code, population, age) select st_fips, co_fips, (case when 1 = hisp then 'd12' when 2 = hisp then 'd11' end) as demographic_code, sum(&year_col), age from &source group by st_fips, co_fips, (calculated demographic_code), age; *-- race (all) ; insert into counties_&yr (st_fips, co_fips, demographic_code, population, age) select st_fips, co_fips, (case when 1 = racesex then 'd3' when 2 = racesex then 'd3' when 3 = racesex then 'd5' when 4 = racesex then 'd5' when 5 = racesex then 'd7' when 6 = racesex then 'd7' when 7 = racesex then 'd9' when 8 = racesex then 'd9' end) as demographic_code, sum(&year_col), age from &source group by st_fips, co_fips, (calculated demographic_code), age; *-- race (non-hispanic) ; insert into counties_&yr (st_fips, co_fips, demographic_code, population, age) select st_fips, co_fips, (case when 1 = hisp and 1 = racesex then 'd4' when 1 = hisp and 2 = racesex then 'd4' when 1 = hisp and 3 = racesex then 'd6' when 1 = hisp and 4 = racesex then 'd6' when 1 = hisp and 5 = racesex then 'd8' when 1 = hisp and 6 = racesex then 'd8' when 1 = hisp and 7 = racesex then 'd10' when 1 = hisp and 8 = racesex then 'd10' end) as demographic_code, sum(&year_col), age from &source where hisp = 1 group by st_fips, co_fips, (calculated demographic_code), age; *-- get the state-level totals ; create table states_&yr as select st_fips, (0) as co_fips, demographic_code, (sum(population)) as population, age from counties_&yr group by st_fips, (calculated co_fips), demographic_code, age; *-- add states to counties ; insert into counties_&yr select * from states_&yr; quit; *-- rename the counties dataset ; proc datasets library=user nolist; change counties_&yr = allgeos_&yr; run; *-- sort the allgeos dataset ; proc sort data=allgeos_&yr; by st_fips co_fips age; run; *-- transpose the allgeos dataset ; proc transpose data=allgeos_&yr out=allgeos_transposed_&yr; by st_fips co_fips age; id demographic_code; var population; run; *-- make the output dataset ; proc sql; create table output_&yr as select st_fips format=z2. as state, ((st_fips * 1000) + co_fips) format=z5. as county, (&yr) format=4. as year, age format=z2., total, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12 from allgeos_transposed_&yr; quit; proc export data=output_&yr dbms=csv outfile="&work_dir./data_&yr..csv"; run; %put Ending &yr; %end; %mend doit; %doit; /* WORKING AREA ============================== ======================================= */