x cd /pub/data/popests/Tools; %let pgm=poptrendsYY; filename pgm "&pgm..sas"; *----Edit the dotrends macro to define the custom time periods, which we allow to vary from year to year. Edit $yearix format. The 2 poptrends websas mo modules (websas.poptrend.sas and the corr poptrends.html page) must be edited to agree with what we do here. ---*; **--Create the state and nation-level poptrends datasets that will be used to create the corresponding poptrends web reports. These datasets/report are based on the raw data taken from the nchsi bridged race summary datasts for the 1990s, 2000s and 2010s. jgb, 11-3-12 (adapting poptrends,sas) (begin work). 8-01-13: Running with latestyear=2012. Modifying so that final output dataset is nchscri.ustrends&latestyr . Note that last year (2012) we called the dataset ustrends10. So, we have a break in naming convention here (Aug. 2013). 6-30-14: Running with latstyear=2013. 4-6-15: Running after finding problems with PctStart and PctEnd vars for perno in (11,12,13). 7-6-15: Running with new 204 vintage estimates. 4-27-16: Running again to fix problem with the 2009-2011 period (perno=10). We specified 3 year when it was just 2. --*/; /* filename mprint 'poptrends.mprint.sas'; options mfile; */ title "&pgm.: Pop Trends Based on the nchsbri estimates for the decades of the 90s thru the 2010s"; *filename deftpl '/pub/data/system/tableeditor/tableeditor.tpl'; libname nchsbri '/pub/data/popests/nchsbri' ; **libname user '/SAS_temp/userb'; libname popests '/pub/data/popests' access=readonly; %let latestyr=15; %let latestyear=20&latestyr; *<====; %let nyears=&latestyr; *<====just a conincidence that this works out this way. ; *--the years are 1990, 1995, 2000, 2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 --; %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--; *---we are not currently using the 2007 data but easier to leave it in---; Value $yearix '1990'=1 '1995'=2 '2000'='3' '2003'=4 '2005'=5 '2006'=6 '2007'=7 '2008'=8 '2009'=9 '2010'=10 '2011'=11 '2012'=12 '2013'=13 '2014'=14 '2015'=15 other=0; *<==================Add another entry in 2015 !!!!=; Value $demog 01='Total Population' 02='White' 03='White non-hispanic' 04='Black' 05='American Indian, Etc' 06='Asian or Pac Islndr' 07='Hispanic' 08='Non-hispanic' 09='Pop Aged 0 to 17' 10='Pop Aged 18 to 24' 11='Pop Aged 25 to 44' 12='Pop Aged 45 to 64' 13='Pop Aged 65+'; run; %*let state=29; %*let stab=mo; *---the poptrends web app is officially replaced by new version. But we maintain a version in websas/test. ---; %macro dotrends; %*---This code is used twice, once for the state/county data and once for us totals. So its a macro now---; %*<===This code is to be edited to reflect new set of time periods for each cycle. Perno and Period and _periodLen must be kept in sync. In the poptrends.sas (websas app) there are format codes that convert perno to Period And the poptrends.html page also needs to be updated to reflect changes made here.---; do _idemog=1 to 13; Demographic=put(_idemog,z2.); *<---demographic category code. 2-char with leading 0. See format code, above.--; *---longtime periods of 10+ years---; PerNo=1; Period="1990 to &latestyear"; _periodLen=&latestyear - 1990; StartPop=est{1,_idemog}; EndPop=est{&nyears,_idemog}; _starttot=est{1,1}; _endtot=est{&nyears,1}; link trendcode; PerNo=2; Period="1990 to 2000"; _periodLen=10; StartPop=est{1,_idemog}; EndPop=est{3,_idemog}; _starttot=est{1,1}; _endtot=est{3,1};link trendcode; PerNo=3; Period="2000 to 2010"; _periodlen=10; StartPop=est{3,_idemog}; EndPop=est{10,_idemog}; _starttot=est{3,1}; _endtot=est{10,1}; link trendcode; PerNo=4; Period="2000 to &latestyear"; _periodlen=&latestyr; StartPop=est{3,_idemog}; EndPop=est{&nyears,_idemog}; _starttot=est{3,1}; _endtot=est{&nyears,1}; link trendcode; *---5-year periods----; _periodlen=5; PerNo=5; Period="1990 to 1995"; StartPop=est{1,_idemog}; EndPop=est{2,_idemog}; _starttot=est{1,1}; _endtot=est{2,1}; link trendcode; PerNo=6; Period="1995 to 2000"; _periodlen=5; StartPop=est{2,_idemog}; EndPop=est{3,_idemog}; _starttot=est{2,1}; _endtot=est{3,1}; link trendcode; PerNo=7; Period="2000 to 2005"; _periodlen=5; StartPop=est{3,_idemog}; EndPop=est{5,_idemog}; _starttot=est{3,1}; _endtot=est{5,1}; link trendcode; PerNo=8; Period="2005 to 2010"; _periodlen=5; StartPop=est{5,_idemog}; EndPop=est{10,_idemog}; _starttot=est{5,1}; _endtot=est{10,1}; link trendcode; PerNo=9; Period="2010 to 2015"; _periodlen=5; StartPop=est{10,_idemog}; EndPop=est{15,_idemog}; _starttot=est{10,1}; _endtot=est{15,1}; link trendcode; *---3-year periods------*; _periodlen=3; PerNo=10; Period="2000 to 2003"; _periodlen=3; StartPop=est{3,_idemog}; EndPop=est{4,_idemog}; _starttot=est{3,1}; _endtot=est{4,1}; link trendcode; PerNo=11; Period="2003 to 2006"; _periodlen=3; StartPop=est{4,_idemog}; EndPop=est{6,_idemog}; _starttot=est{4,1}; _endtot=est{6,1}; link trendcode; PerNo=12; Period="2006 to 2009"; _periodlen=3; StartPop=est{6,_idemog}; EndPop=est{9,_idemog}; _starttot=est{6,1}; _endtot=est{9,1}; link trendcode; PerNo=13; Period="2009 to 2012"; StartPop=est{9,_idemog}; EndPop=est{12,_idemog}; _starttot=est{9,1}; _endtot=est{12,1}; link trendcode; PerNo=14; Period="2012 to 2015"; StartPop=est{12,_idemog}; EndPop=est{15,_idemog}; _starttot=est{12,1}; _endtot=est{15,1}; link trendcode; *---1-year periods------*; _periodlen=1; PerNo=15; Period="2010 to 2011"; _periodlen=1; StartPop=est{10,_idemog}; EndPop=est{11 ,_idemog}; _starttot=est{10,1}; _endtot=est{11 ,1}; link trendcode; PerNo=16; Period="2011 to 2012"; _periodlen=1; StartPop=est{11,_idemog}; EndPop=est{12,_idemog}; _starttot=est{11,1}; _endtot=est{12,1}; link trendcode; PerNo=17; Period="2012 to 2013"; _periodlen=1; StartPop=est{12,_idemog}; EndPop=est{13,_idemog}; _starttot=est{12,1}; _endtot=est{13,1}; link trendcode; PerNo=18; Period="2013 to 2014"; _periodlen=1; *<====new each year; StartPop=est{13,_idemog}; EndPop=est{14,_idemog}; _starttot=est{13,1}; _endtot=est{14,1}; link trendcode; PerNo=19; Period="2014 to 2015"; StartPop=est{14,_idemog}; EndPop=est{15,_idemog}; _starttot=est{14,1}; _endtot=est{15,1}; link trendcode; %mend dotrends; %macro doit(state,stab); data aggin/view=aggin; set nchsbri.&stab.nchsbridged19xx_sumry (where=(year in ('1990','1995'))) nchsbri.&stab.nchsbridged20xxi_sumry (where=(year='2000' or year='2003' or year ge '2005')) nchsbri.&stab.nchsbridged201x_sumry; *<====for now we keep ALL the 201x years====; run; proc means data=aggin missing noprint nway; class county year age_cohort1; var total white whitenh black amind asianpi hispanic nonhispanic; output out=aggout&stab(drop=_type_ _freq_) sum=; run; data &stab.trends(label="Trends in pop ests based on nchsbri data 1990 to &latestyear"); length SumLev $3 State $2; length County $5 PerNo 3 Period $12 Demographic $2 StartPop PctStart EndPop PctEnd Change PctChange AnnualizedChange AnnualizedPctChange 5; keep sumlev state county -- AnnualizedPctChange; format StartPop EndPop comma11. Change AnnualizedChange comma10. PctStart PctEnd 5.1 PctChange AnnualizedPctChange 6.1; array est(&nyears,13) _temporary_; set aggout&stab; by county year; *---We save these 13 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 _iyr=0 or ( _iyr > &nyears) THEN Do; put / '***Logic Problem assigning _iyr: ' _all_; abort abend; end; if age_cohort1=' ' then do; 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; end; else select(age_cohort1); when('00') est{_iyr,9}=total; when('18') est{_iyr,10}=total; when('25') est{_iyr,11}=total; when('45') est{_iyr,12}=total; when('65') est{_iyr,13}=total; otherwise do; put / '****Logic problem: unrecognized age_cohort1 value: ' age_cohort1= / _all_; abort abend; end; end; *---Select--; if last.county 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)---; if length(county)=2 then SumLev='040'; else SumLev='050'; State=substr(county,1,2); %dotrends ; 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; %if %superq(stab)=mo %then %do; proc freq data=&stab.trends; tables state sumlev county demographic ; tables PerNo*period/list; format county $county. demographic $demog.; title2 "Freq Reports on nchsbri.&stab.trends"; run ; %end; %mend doit; options NOmprint; %dostates(names=1,msgs=1); *-----This code being added 5/09 to create US totals summaries---------------------------*; %let debug=1; %macro aggout_(state,stab); aggout&stab %mend aggout_; *<---Used to reference all the 51 state aggout sets----; data agginustots; set %dostates(n=51,mac=aggout_) open=defer; where length(county)=2; state='00'; county=state; run; proc means data=agginustots missing noprint nway; class state county year age_cohort1; var total white whitenh black amind asianpi hispanic nonhispanic; output out=aggoutustots(drop=_type_ _freq_) sum=; run; proc sort data=aggoutustots; by year age_cohort1; run; *---this step pretty much duplicates the one from above but with different first and last processing-; options mprint; data ustottrends(label="Trends in pop ests based on nchsbri data 1990 to &latestyear"); length SumLev $3 State $2; length County $5 PerNo 3 Period $12 Demographic $2 StartPop PctStart EndPop PctEnd Change PctChange AnnualizedChange AnnualizedPctChange 5; keep sumlev state county -- AnnualizedPctChange; format StartPop EndPop comma11. Change AnnualizedChange comma10. PctStart PctEnd 5.1 PctChange AnnualizedPctChange 6.1; array est(&nyears,13) _temporary_; set aggoutustots; by state; *---We save these 13 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 _iyr=0 or ( _iyr > &nyears) THEN Do; put / '***Logic Problem assigning _iyr: ' _all_; abort abend; end; if age_cohort1=' ' then do; 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; end; else select(age_cohort1); when('00') est{_iyr,9}=total; when('18') est{_iyr,10}=total; when('25') est{_iyr,11}=total; when('45') est{_iyr,12}=total; when('65') est{_iyr,13}=total; otherwise do; put / '****Logic problem: unrecognized age_cohort1 value: ' age_cohort1= / _all_; abort abend; end; end; *---Select--; if last.state 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)---; SumLev='010'; %dotrends ; end; end; *---last.state 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; *---and now concatenate to create the final national dataset----; %macro setref(state,stab); &stab.trends %mend setref; data nchsbri.ustrends&latestyr(label="Pop Trends 1990 to &latestyear for U.S. states and counties" index=(state)); set ustottrends %dostates(n=51,mac=setref) open=defer; run; proc freq data=nchsbri.ustrends&latestyr; tables state sumlev demographic ; tables PerNo*period/list; format state $state. demographic $demog.; title2 "Freq Reports on nchsbri.ustrends&latestyr"; run;