x cd /pub/data/popests/Tools; %let pgm=poptrends_pi20xx; filename pgm "&pgm..sas"; /*--Create the poptrends.ustrendspi20 county, state and nation-level poptrends_ca datasets that will be used as input for the poptrendspi20xx web application (two modules: websas.poptrendspi20xx_menu.sas and websas.poptrendspi20xx.sas). Modeled after setup we did with the poptrends_casrh which in turn was modeled after the setup in poptrends.sas that generates a poptrends data set based on nchsbri data estimates. Accesses casrh and the new intercensal estimates to create the dataset popests.ustrendspi20xx ("pi" stands for post-inter). jgb, 10-1-11 (begin work). --*/; /* filename mprint 'poptrends.mprint.sas'; options mfile; */ title "&pgm.: Pop Trends Based on the casrhi post-censal estimates and intercensal ests for the decade 2000-2010"; *filename deftpl '/pub/data/system/tableeditor/tableeditor.tpl'; libname user '/SAS_temp/userb'; libname popests '/pub/data/popests'; libname casrhalt '/pub/data/popests/casrhalt/casrhalt09' access=readonly; libname intrcnsl '/pub/data/popests/intrcnsl' access=readonly; %let latestyr=09; %let latestyear=20&latestyr; %let nyears=%eval(5 + &latestyear - 2005); %let debug=0; *---optional generation of debugging info---; proc format; *---this format takes the year and returns the index in our table where we store the data for that year--; Value $yearix '2000','2000j'='1' '2003'=2 '2005'=3 '2006'=4 '2007'=5 '2008'=6 '2009'=7 '2010'=8 other=0; Value $demog 01='Total Population' 02='White alone' 03='White alone non-hispanic' 04='Black alone' 05='American Indian, Etc alone' 06='Asian alone' 07='Hispanic' 08='Non-hispanic' 09='Pop Aged 0 to 14' 10='Pop Aged 15 to 24' 11='Pop Aged 25 to 44' 12='Pop Aged 45 to 64' 13='Pop Aged 65+' 14='Multi-racial'; run; /* ========================begin comment==================================== %macro doit(state,stab); data post_ests20xx&stab; length State $2 County $5 year $5; retain EstType "Post "; set casrhalt.&stab.casrh_alt&latestyr(rename=(year=yr)); by county yr; retain state "&state"; where yr='02' or yr='03' or yr='06' or yr ge '08'; *--yr code has value 01 and 02 for census and estbase. 3 is 7-1-00, 8 is for 7-1-05--; array _vars age00 age15 age25 age45 age65 total white whitenh black amind asianpi multirace hispanic nonhispanic; if first.yr then do over _vars; _vars=0; end; retain age00--nonhispanic; if agegrp=0 then do; *---values are for the total pop, not just a specific age cohort---; total=Tot_pop; white=wa_male + wa_female; whitenh=nhwa_male+nhwa_female; black=ba_male + ba_female; amind=ia_male + ia_female; asianpi= aa_male+aa_female+na_male+na_female; hispanic=h_male+h_female; nonhispanic=total-hispanic; multirace=tom_male + tom_female; end; else do; if agegrp <=3 then age00 +Tot_pop; else if agegrp<=5 then age15 + Tot_pop; else if agegrp<=9 then age25 + Tot_pop; else if agegrp <=13 then age45 + Tot_pop; else age65 + Tot_pop; end; if last.yr then do; length _nyear 8; _nyear=2000+yr -3; *--03=2000, 08=2005, .... ---; if yr='02' then year='2000a'; else if yr='03' then year='2000'; else year=_nyear; year=left(year); output; end; keep EstType year county state age00--nonhispanic; run; %mend doit; %dostates(names=1,msgs=1) ; *<====generate the 51 county level 20xx datasets. No state or nation data yet--; *---Aggregate all the county level data to get nation and state by year summaries. We want to do all the geographic aggregating before we run the step to create the final ustrends dataset. (Different approach than what we did with original poptrends).--; %macro cntysets(state,stab); user.post_ests20xx&stab %mend cntysets; *<---Used to reference all the 51 state county level sets----; data agginPost_&pgm; set %dostates(n=51,mac=cntysets) open=defer; run; proc sort; by state county year; run; proc means data=agginPost_&pgm missing noprint; class state year; types year state*year; var total age00--age65 white whitenh black amind asianpi multirace hispanic nonhispanic; output out=aggoutPost_&pgm sum=; run; data aggoutPost_edited; set aggoutPost_&pgm; if _type_=1 then do; sumlev='010'; county=' '; AreaName='United States'; end; else do; sumlev='040'; county=state||' '; Areaname=put(state,$state.); end; drop _: ; EstType='Post '; Year=left(Year); run; proc sort; by state county year; run; *------------------------------Now create the parallel data based on inter-censal estimates----------------------------*; data inter_ests20xx; length State $2 County $5 year $5; retain EstType "Inter"; set intrcnsl.usstcntyagesex5yr intrcnsl.usstcntysexracehisp(in=inrace); by state county; retain state "&state"; array estTable(9,14) _temporary_; *--Rows are years: 1=estbase (2000a), 2=2000 3=2003 4=2005 5=2006 6=2007 7=2008 8=2009 9=2010. The cols are: 1=total 2=age00 3=age15 4=age25 5=age45 6=age65 7=white, 8=whitenh, 9=black, 10=amind, 11=asianpi, 12=multirace 13=Hispanic 14=nonhispanic --; array estsIn(13) EstBase2k -- PopEst2010; array _vars(14) total age00 age15 age25 age45 age65 white whitenh black amind asianpi multirace hispanic nonhispanic; retain age00--nonhispanic; if first.county then do irow=1 to 9; do icol=1 to 14; estTable(irow,icol)=0; end; end; if sex gt '0' then goto cklast; *---we never want detail by sex---; icol=0; if agegrp ne ' ' and sex='0' then do; if agegrp='00' then icol=1; else if agegrp le '03' then icol=2; else if agegrp le '05' then icol=3; else if agegrp le '09' then icol=4; else if agegrp le '13' then icol=5; else icol=6; *--age 65+ ---; end; if race gt '0' and origin='0' then select(race); when('1') icol=7; *--white alone. col 8 will be white alone, non-hispanic--; when('2') icol=9; when('3') icol=10; when('4') icol=11; when('5') icol=11; *--we combine asian and Nat Hawn and PI---; when('6') icol=12; end; if race ='1' and origin='1' then icol=8; *--white non-hispanic--; if race='0' and origin='2' then icol=13; *--hispanic--; if race='0' and origin='1' then icol=14; *--nonHispanic--; if icol=0 then goto cklast; *----if icol has a positive value it tells us we have data to save. We either assign it or add it to the cells in that column.---; do irow=1 to 9; EstsInRow=.; if irow=1 then EstsInRow=1; else if irow=2 then EstsInRow=2; else if irow=3 then EstsInrow=5; else if irow=4 then EstsInRow=7; else if irow=5 then EstsInRow=8; else if irow=6 then EstsInRow=9; else if irow=7 then EstsInRow=10; else if irow=8 then EstsInRow=11; else if irow=9 then EstsInRow=13; if (2 <= icol <=6) or icol=11 then estTable(irow,icol) + estsIn(EstsInRow); else estTable(irow,icol)= estsIn(EstsInRow); end; cklast: if not last.county then return; do _ivar=1 to 14; _vars(_ivar)=0; end; *---We are going to output 9 observations corresponding to the years stored in the estTable.---; do irow=1 to 9; if irow=1 then Year='2000a'; else if irow=2 then Year='2000'; else if irow=3 then Year='2003'; else if irow=4 then Year='2005'; else year= 2001+ irow; year=left(year); do icol=1 to 14; _vars(icol)=estTable(irow,icol); end; output; end; *--irow loop---; keep EstType year SumLev county state total age00--nonhispanic; run; *----Aggregate to get the national summaries---; proc sort data=inter_ests20xx(where=(sumlev='040')) out=interstates; by year; run; proc means data=interstates missing noprint; class year; types year; var total age00--age65 white whitenh black amind asianpi multirace hispanic nonhispanic; output out=aggoutInterNation sum=; run; data aggoutInter_edited; set aggoutInterNation; length Areaname $40; if _type_=1 then do; sumlev='010'; State=" "; county=' '; Areaname="United States"; end; else delete; drop _: ; Year=left(Year); EstType="Inter"; run; ==========================end comment====================================== */ *---this dataset is going to be used as the source for a poptrendspi20xx web application----; data popests.ustrendspi20xx(label="Post and Inter-censal pop ests 2000 to 2010" index=(state)); length SumLev $3 State $2 EstType $5; length County $5 PerNo 3 Period $12 Demographic $2 StartPop PctStart EndPop PctEnd Change PctChange AnnualizedChange AnnualizedPctChange 5; keep EstType sumlev AreaName state county -- AnnualizedPctChange; format StartPop EndPop comma11. Change AnnualizedChange comma10. PctStart PctEnd 5.1 PctChange AnnualizedPctChange 6.1; array est(8,14) _temporary_; *---This set statements interleaves all 4 data sets with 2 per type (Post and Inter) with county and national total sets.---; set agginPost_&pgm(in=inP) aggoutPost_edited (in=inP2) inter_ests20xx(in=inI) aggoutInter_edited (in=inI2) ; by state county EstType Year; where year ne '2000a'; *---We save the 14 demographic categories including all 4 race groups and the 5 broad age groups. Nothing is crossed.-; _iyr=0; *--makes it numeric-; _iyr=put(year,$yearix.); if inP then SumLev='050'; IF _iyr=0 or ( _iyr > 8) THEN Do; format year $char5.; put / '***Logic Problem assigning _iyr: ' year= _all_; abort abend; end; est{_iyr,1}=Total; est{_iyr,2}=white; est{_iyr,3}=whitenh; est{_iyr,4}=Black; est{_iyr,5}=AmInd; est{_iyr,6}=AsianPI; est{_iyr,7}=Hispanic; est{_iyr,8}=NonHispanic; est{_iyr,9}=age00; est{_iyr,10}=age15; est{_iyr,11}=age25; est{_iyr,12}=age45; est{_iyr,13}=age65; est{_iyr,14}=Multirace; if last.EstType then do; *---here is where we build and write the output, a series of time-period trend summaries for the current geographic area. (County includes state here)---; *---For the post estimates we do not have 2010 data (8th row of saved data table). The values from the intercensal sets is still sitting in that row of the table and we have to write missing values over them.---; if EstType='Post' then do _idemog=1 to 14; est{8,_idemog}=.; end; State=substr(county,1,2); if state=' ' then state='00'; do _idemog=1 to 14; Demographic=put(_idemog,z2.); *<---demographic category code. 2-char with leading 0. See format code, above.--; PerNo=1; Period="2000 to 2010"; _periodLen=10; StartPop=est{1,_idemog}; EndPop=est{8,_idemog}; _starttot=est{1,1}; _endtot=est{8,1}; link trendcode; PerNo=2; Period="2000 to 2005"; _periodLen=5; StartPop=est{1,_idemog}; EndPop=est{3,_idemog}; _starttot=est{1,1}; _endtot=est{3,1};link trendcode; PerNo=3; Period="2005 to 2010"; _periodlen=5; StartPop=est{3,_idemog}; EndPop=est{8,_idemog}; _starttot=est{3,1}; _endtot=est{8,1}; link trendcode; PerNo=4; Period="2000 to 2003"; _periodlen=3; StartPop=est{1,_idemog}; EndPop=est{2,_idemog}; _starttot=est{1,1}; _endtot=est{2,1}; link trendcode; PerNo=5; Period="2003 to 2006"; _periodlen=3; StartPop=est{2,_idemog}; EndPop=est{4,_idemog}; _starttot=est{2,1}; _endtot=est{4,1}; link trendcode; PerNo=6; Period="2006 to 2009"; _periodlen=3; StartPop=est{4,_idemog}; EndPop=est{7,_idemog}; _starttot=est{4,1}; _endtot=est{7,1}; link trendcode; PerNo=7; Period="2007 to 2008"; _periodlen=1; StartPop=est{5,_idemog}; EndPop=est{6,_idemog}; _starttot=est{5,1}; _endtot=est{6,1}; link trendcode; PerNo=8; Period="2008 to 2009"; _periodlen=1; StartPop=est{6,_idemog}; EndPop=est{7,_idemog}; _starttot=est{6,1}; _endtot=est{7,1}; link trendcode; PerNo=9; Period="2009 to 2010"; _periodlen=1; StartPop=est{7,_idemog}; EndPop=est{8,_idemog}; _starttot=est{7,1}; _endtot=est{8,1}; link trendcode; end; *---the _idemog loop in which we ouput a set of trends for for each of the specified demographic categories-; *----We could "blank out" the est array here but it should not be necessary-------------------------; end; *---last.county processing---; return; trendcode: Change=EndPop - StartPop; if StartPop then PctChange=100*Change/StartPop; else PctChange=.; PctStart=StartPop*100/_starttot; PctEnd=EndPop*100/_endtot; if _periodlen ne . and StartPop then do; AnnualizedChange=Change/_periodLen; AnnualizedPctChange= 100*((Endpop/StartPop)**(1/_periodLen)-1); end; else do; AnnualizedChange=.; AnnualizedPctChange=.; *---why repeat what is already shown as the value for the 1-year interval?-; end; output; if &debug then if length(county)=2 then put / _all_; return; run; proc freq data=popests.ustrendspi20xx; tables demographic PerNo*period/list; tables EstType state sumlev; format demographic $demog.; title2 "Freq Reports on popests.ustrendspi20xx"; run ;