x cd /pub/data/pl942010/Tools; %let pgm=create_motrends; filename pgm "&pgm..sas"; *--- Read the custom dbf files generated by Jim Harlan at MsDIS for state of Mo. These are block level and above files with basic pl94 data from 3 decades: 1990 t0 2010. We convert the block level data and merge it with geographic data from the pl942010 set to get a dataset that we can then pass to the summary proc in order to get many different geographic summaries. jgb, 3-7-11. ---; libname pl942010 '/pub/data/pl942010' access=readonly; libname pl94 (pl942010); %let tpath=%str(/pub/data/pl942010/trends); libname trends "&tpath"; filename blkcsv "&tpath/block10.csv" ; *--Created by saving the dbf file to CSV format in excel---; ods html file="&pgm..html" style=sasweb; %macro postproc; %*---this gets invoked at end of last data step--; %mend postproc; options compress=no; /* ========================begin comment==================================== *---This step simple does not work. proc dbf is not able to read the dbf file correctly. So we used Excel to save it as a CSV file, which we shall be reading in the alternative version, Step 1A --------* ; *-----Step 1: Convert the downloaded dbf files with raw data. We want the tract10 data because we want to verify that we can aggregate the block level data and match that data----; filename dbfbl "&tpath/block10.dbf" ; filename dbftr "&tpath/tract10.dbf"; proc dbf db4=dbfbl out=trends.block10grc(label='Data as received from J Harlan, Mar 4 2011'); run; proc contents data=trends.block10grc; run; proc dbf db4=dbftr out=trends.tract10grc(label='Data for Mo tracts as received from J Harlan, Mar 4 2011'); run; *-----Step 1A: Create trends.moblocksplus(label='Mo data from GRC with 2010 geocodes added-----*; data trends.block10grc (label='Data as received from J Harlan, Mar 4 2011' ); infile blkcsv firstobs=2 obs=max termstr=crlf dsd lrecl=512 missover ; length _statefp10 $2 _countyfp10 $3 County $5 _tractin $6 _bgce10 $1 Tract $7 Block $4 vtd $6 _trctid _bgid esriid $16; length pop90 white90 black90 asian90 amind90 other90 hisp90 pop00 white00 black00 asian00 amind00 hawnpi00 other00 multra00 hisp00 pop10 white10 black10 asian10 amind10 hawnpi10 other10 multra10 hisp10 4; input _statefp10 _countyfp10 _tractin _bgce10 block--hisp10; County=_statefp10||_countyfp10; Tract=substr(_tractin,1,4)||'.'||substr(_tractin,5,2); drop _: ; if _N_ le 5 then put / _all_; run; proc sort; by esriid; run; title "Missouri Totals"; proc means data=trends.block10grc n sum maxdec=0; var pop90--hisp10; run; ==========================end comment====================================== */ /* ========================begin comment==================================== *---Step 2: merge these block pop counts with all the geocodes stored in the pl942010 level 750 summaries----; data pl94temp; set pl942010.mosums(where=(sumlev='750') keep=sumlev state county tract block placefp cousubfp cbsa vtd vtdi sdelm sduni cd111 sldl sldu areasqmi landsqmi OccHUs vacant tothus intPtLon IntPtLat); if sdelm ='99999' then schldist=sduni; else schldist=sdelm; *---MO-specific thing---; run; proc sort; by county tract block; run; data trends.block10grcplus(label='this is block10grc with many 2010 geocodes added'); merge trends.block10grc(in=ingrc) pl94temp(in=inpl94); by county tract block; length bg $1; bg=substr(block,1,1); run; ==========================end comment====================================== */ *-----Step 3: Use a macro to be invoked once for each unit of geography for which we want to create a trends data set.----; %macro doit(sumlev=,geovars=,ids=,nlvls=1,setout=,names=1 ); %local ngeovars; %let ngeovars=%lenlst(&geovars); %local geovars_withcommas i; %let geovars_withcommas=%scan(&geovars,1); %if &ngeovars gt 1 %then %do i=2 %to &ngeovars; %let geovars_withcommas=&geovars_withcommas %str(,) %scan(&geovars,&i); %end; %if &names %then %do; proc sql; create view names as select &geovars_withcommas, esriid, areaname from pl942010.mosums where sumlev="&sumlev" order by &geovars_withcommas; quit; %end; %local geovars_edited _geovar; %*--edit to remove refs to county tract or block for use in keep list to follow--; %do i=1 %to &ngeovars; %let _geovar=%scan(&geovars,&i); %if %index(county tract block,&_geovar)=0 %then %let geovars_edited=%trim(&geovars_edited) &_geovar; %end; proc sort data=trends.block10grcplus(keep=pop90--hisp10 LandSQMI--Vacant county tract block &geovars_edited &ids) out=data&sumlev ; by &geovars; run; %agg(setin=data&sumlev, setout=aggregated_data&sumlev, aggby=&geovars, aggvars=_numeric_, idvars=&ids, agglvls=&nlvls); run; data &setout; length esriid $20 AreaName $90 SumLev $3 ; retain SumLev "&sumlev" &geovars; %if &names=0 %then %str( drop areaname; ); *<====; %let varnms=pop white black asian amind other hisp; %let yrs=90 00 10; length %do i=1 %to 7; %let var=%scan(&varnms,&i); %do iyr=1 %to 3; %let yr=%scan(&yrs,&iyr); &var&yr %if &i ne 1 %then %str( Pct&var&yr ); %end; %end; 5; *<---end of length statement----; %if &names %then %do; merge aggregated_data&sumlev (in=indata) names(in=innames); by &geovars; *** esriid; if not indata then delete; %end; %else %do; set aggregated_data&sumlev; %end; esriid=cat( %do i=1 %to &ngeovars; %if &i=1 %then %scan(&geovars,&i); %else %str(,)%scan(&geovars,&i); %end; ); esriid=compress(esriid,'.'); **if not innames then Areaname="Geoid "||geoid; drop _:; *---Calculate all the percentages---; %do i=2 %to 7; %let var=%scan(&varnms,&i); %do iyr=1 %to 3; %let yr=%scan(&yrs,&iyr); Pct&var&yr=100* &var&yr /pop&yr; %end; %end; PopPSQMi10=pop10/LandSQMI; attrib PopPSQMi10 label='Persons Per Sq Mile Land 2010' format=best7.; format Pct: 5.1; %postproc run; proc means data=&setout n sum min max maxdec=0; class sumlev; var pop90 pop00 pop10 hisp90 hisp00 hisp10; title "Means Run Against &setout"; run; %mend doit; options mprint; /* ========================begin comment==================================== %doit(sumlev=950,geovars=state sdelm,setout=trends.moschlselem ); %doit(sumlev=970,geovars=state sduni,setout=trends.moschlsuni ); run; data trends.moschls(label='Mo School districts: Unified and Elem combined'); length SchlDist $5; label schldist='School District'; set trends.moschlsuni trends.moschlselem(in=inelem); by esriid; if inelem then schldist=sdelm; else schldist=sduni; if inelem then sduni='99999'; else sdelm='99999'; run; ==========================end comment====================================== */ %doit(sumlev=040,geovars=state,setout=trends.mostate); /* ========================begin comment==================================== %doit(sumlev=050,geovars=county,setout=trends.mocounties); %doit(sumlev=610,geovars=state sldu,names=1,setout=trends.mosenate); %doit(sumlev=620,geovars=state sldl,names=1,setout=trends.mohouse); %doit(sumlev=160,geovars=state placefp,setout=trends.moplaces); %doit(sumlev=140,geovars=state county tract,names=0,setout=trends.motracts); %doit(sumlev=150,geovars=state county tract bg,names=0,setout=trends.mobgs); %macro postproc; _arg=substr(county,1,2)||placefp; Areaname2=put(_arg,$moplace.); rename areaname2=areaname; %mend postproc; %doit(sumlev=155,geovars=county placefp,names=0,setout=trends.moplacecos); %macro postproc; %mend; %doit(sumlev=060,geovars=county cousubfp,names=1,setout=trends.momcds); ==========================end comment====================================== */ ods html close;