x cd /pub/data/stf803/Tools; %let pgm=migrate_plus; filename pgm "&pgm..sas"; *---This custom setup creates a new era of stf803 datasets. They have been "migrated" to the 64-bit format required for native access with indices for SAS v9, but at the same time we take the opportunity to significantly enhance them. See detailed comment below. (We moved the original stf803 data to the stf803/v8 directory prior to coding this setup.) jgb, 1-03-05 . Running various combinations of steps 1-3-05 to fix small bugs in pop100 on places. *---Found some problems with inventory datasets where marf2-based data were not being set to missing when no such data were available. See setup fix_marfdata where we shall attempt to fix these problems. We cannot rerun this setup because the v8 data are gone. jgb, 2-6-06. ---*; title "&pgm : Migrate and Enhance filetype stf803 - 1980 Summary Tape File 3"; proc printto print="&pgm..lst" ; run; *<----so that it will append to previous runs instead of starting anew--; libname v8 '/pub/data/stf803/v8'; libname stf803 '/pub/data/stf803'; libname stf3app '/pub/data/stf803/app'; %libname(marf2); *--1980 geographic ref file. Contains basic header info from sf3 but adds lat/long and (for some levels) landarea. Also has some basic pop counts and a pci but we keep only to total pop variable, pop100. --*; *---apportioned datasets in separate sublibraries - will be migrated to new stf803/app subdir---; libname moapp '/pub/data/stf803/v8/moapp.sdl'; libname ilapp '/pub/data/stf803/v8/ilapp.sdl'; libname trapuma '/pub/data/stf803/v8/stlmsa.trapuma'; libname user '/SAS_temp/userb'; /* Summary of enhancements over the orginal stf803 datasasets: 1. Assign current 3-digit SumLev codes where there are equivalents to the orginal 2-digit 80 SumryLvl codes. 2. Merge 1980 marf2 (Master Area Reference File 2) data to get longitude and latitude coords (vars. longitud, latitude), landarea (only filled in for a small number of levels) as well as FIPS MCD and place codes. Also the variable Pop100 - the 100% pop count. 3. Pick up enhanced Areaname value from MARF2 (mixed case and with level id word included: E.g. you get "Bay Village City" instead of just "BAY VILLAGE". For level 14-16 (tract and below) we substitute an actual description of the area being summarized. The orginal value (the name of the Place in which the entity is located) we create a new variable Location to contain that name. 4. We create a geocode variable with the usual string of required codes separated by dashes. On inventory dataset only. 5. We make the County variable 5 chars instead of 3 for consistency with our later products. We associate the $county format code with this field. The new variable FipCo contains the same value but without the format code. 6. State-level datasets are indexed by SumLev and Geocode (composite) and County. 7. Above items apply to 'A' state files. The national 'C' file will be handled separately. We will create it as a series of usxxxx datasets, most dervied from concatenating data from the state datasets. 8. Datasets are compressed to save space. 9. Separate inventory and hierarchal summaries on 2 datasets per state, A files only. John Blodgett, OSEDA, under contrct with MCDC, Mo Census Data Center. 12-30-04. */ options nosource; /* =======================Begin comment================ proc format; *--This code translates 1980 SumryLvl codes to 2000 SumLev codes (sometimes the translation is only approxiamte, as with '15' being mapped to '090').---; value $SLTRAN '01'='010' '02'='020' '03'='030' '04'='040' '05'='05 ' '06'='06 ' '07'='07 ' '08'='08 ' '09'='400' '10'='410' '11'='050' '12'='060' '13'='070' '14'='082' '15'='090' '16'='16 ' '27'='160' '33'='500' '35'='871' '36'='881' '35'='871' '36'='881'; run; *=========================================Convert the 51 A files for each state=============================*; %macro doit(state,stab,app=); title "&pgm: Converting and Enhancing 1980 STF3 Data -- for &statenm"; %let ucstab=%upcase(&stab); %let setmarf=marf2.&stab(drop=supflg: pci white black indian asianpi other spanish families groupq oneperhh hunits ochunits ownochu samplehu rename=(totalpop=pop100)); proc sort data=marf2.&stab(keep=sumrylvl place areaname fipsplac where=(sumrylvl='13')) out=&stab.places(index=(place) drop=sumrylvl) nodupkeys; by place; *--we use this dataset to look up the names of the level 27 summaries--; data marf2&stab.d(index=(_geokey)); length county $5; set &setmarf ; length _geokey $44 ; if length(county)=3 then county=state||county; select (sumrylvl); when('04') _geokey=state; when('11') _geokey=county; when('12') _geokey=county||mcd; when('13') _geokey=county||mcd||place; when('14') _geokey=county||mcd||place||tract; when('15') _geokey=county||mcd||place||tract||blkgrp||urbarea||condist||indanv; when('16') _geokey=county||mcd||place||tract||ed ||urbarea||condist||indanv; end; if state in('23','50') and tract ne ' ' then _geokey=trim(_geokey)||smsa; *<---per Usage Note saying there were tracts spanning smsas in Maine and Vermont----; if state in('26') and ward ne ' ' then _geokey=trim(_geokey)||ward; if state in('28') and edind ne ' ' then _geokey=trim(_geokey)||edind; *<--makes no sense, but seems to work; run; %if &app=app %then %do; %let iset=stf3app.&stab.appi; %let hset=stf3app.&stab.apph; %let stf3lib=stf3app; %end; %else %do; %let iset=stf803.&stab.i; %let hset=stf803.&stab.h; %let stf3lib=stf803; %end; data &iset (label="Inventory Summaries for &statenm " drop= tract blkgrp parttrct edind ed scsa urbarea indanv ward xcity cbd compress=binary) &hset(label="Hierarchal Summaries for &statenm " drop=geocode landarea compress=binary); length SumLev $3 Geocode $16 Areaname $40 pop100 5 State $2 County Fipco $5; format county $county.; if 0 then set &setmarf ; *--to put these vars first in the pdv----; %*if %sysfunc( exist(v8.&stab.app) ) %then %do; %*--scrapped this plan when it was discovered that our non-MO apportioned datasets were missing key geographic identifiers and could not be used---; %if &app =app %then %do; set v8.&stab.app; State="&state"; *<--we did not save this variable on app datasets!--; %end; %else %do; set v8.&stab; *--access the originally converted dataset with all tabular data, etc. --*; %end; format supflg: 1. pop100 comma11. latitude 9.6 longitud 10.6 landarea best9.; length _geokey $44 ; if length(county)=3 then county=state||county; fipco=county; if tract ne ' ' and substr(tract,1,1)=' ' then tract = translate (tract,'0',' '); *--add leading zeros--; select (sumrylvl); when('04') _geokey=state; when('11') _geokey=county; when('12') _geokey=county||mcd; when('13') _geokey=county||mcd||place; when('14') _geokey=county||mcd||place||tract; when('15') _geokey=county||mcd||place||tract||blkgrp||ubarea||condist||indanv; when('16') _geokey=county||mcd||place||tract||ed ||urbarea||condist||indanv; otherwise _geokey=' '; end; if state in('23','50') and tract ne ' ' then _geokey=trim(_geokey)||smsa; *<---per Usage Note saying there were tracts spanning smsas in Maine and Vermont----; if state in('26') and ward ne ' ' then _geokey=trim(_geokey)||ward; if state in('28') and edind ne ' ' then _geokey=trim(_geokey)||edind; *<--makes no sense, but seems to work; if _geokey ne ' ' then do; set marf2&stab.d key=_geokey; *<---access the corresponding marf2 geographic id and coordinate data-; if t1i1 ne samplpop and (pop100 ne t3i1 ) then do; if t3i1=0 and t1i1 gt 0 then delete; *---found such a bogus case in Mississippi----; *--we added extra check here after finding a bad value for samplpop on MO marf2 file--; put '****Problem with syncronization of stf803 and marf2: Sample pops differ: ' t1i1= samplpop= areaname= sumrylvl= _n_=; abort abend; end; end; else if Sumrylvl='27' then set &stab.places key=place; *----get the better areaname value---; SumLev=put(sumrylvl,$sltran.); if SumLev='160' then Areaname=Tranwrd(Areaname,' %cdp<',' (cdp)'); pop100=t3i1; *<----just make sure we get this right and it agrees with the tables---; _invsum= (sumlev in('040','050','060','160','500')); if _invsum then do; select(SumLev); when ('040') Geocode=state; when ('050') Geocode=county; when ('060') Geocode=%concatd(county,mcd); when ('160') Geocode=%concatd(state,place); when ('500') do; Geocode=%concatd(state,condist); Areaname='(96th) Congressional District '||condist||" - &ucstab"; end; otherwise do; put '***logic error: unexpected inventory sumlev value: ' sumlev=; abort abend; end; end; output &iset ; end; else output &hset; %if &state=20 %then %do; %*--fix this glitch/bug in the Kansas dataset---; rename t146i=t146i1; drop t146i1; %end; return; drop _: samplpop; %if &app=app %then %do; drop aflags rural; %end; run; proc freq data=&iset(keep=sumlev sumrylvl fipco county); tables sumlev*sumrylvl / list; table county*fipco / list; title2 "Frequencies on stf803.&stab.i"; run; proc freq data=&hset(keep=sumlev sumrylvl fipco county); tables sumlev*sumrylvl / list; table county*fipco / list; title2 "Frequencies on stf803.&stab.h"; run; proc datasets library=&stf3lib nolist; modify &stab&app.i; index create county; index create sumgeo=(sumlev geocode); modify &stab&app.h; index create county; quit; proc datasets library=user nolist; delete marf2&stab.d ; quit; %mend doit; options nomprint; %***dostates(10,names=1) ; *---run test on Delaware only---; %dostates(n=51,names=1); %*dostates(17 18 19 20 21 22 23 24 25 26 27); %*dostates(28 29 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,names=1) ; *==========================Migrate the aggregated apportioned/approximated datasets to the new app subdirectory.==========*; *---first we do the moppp, ksapp and ilapp datasets---; %let stname=Missouri; %*doit(29,mo,app=app); *<=====NOTE: We could not do the enhanced conversion on the il and ks datasets because they were missing critical geographic identifier data. Those datasets are not worth migrating. They will need to be regenerated from scratch later, if anybody wants them.========*; %*let statenm=Kansas; %*doit(20,ks,app=app) ; %*let stname=Illinois; %*doit(17,il,app=app); *---we keep only tract, block group and ed summary levels since the others are already accessible on the &stab.appi dataset.. And we combine the 3 datasets into 1. --; %macro trbged(state,stab) ; data stf3app.&stab.trbged(label="Aggregated complete tract, bg and ed data for &stab" compress=binary); length SumryLvl $2 SumLev $3 AreaName $40 State $2 County fipco $5 Tract $7 blkgrp $1 ed $5 smsa $4 Geocode $16; set &stab.app.tracts(in=intr) &stab.app.blkgrps(in=inbg) &stab.app.eds(in=ined); if length(county)=3 then county=state||county; fipco=county; format county $county.; if tract ne ' ' and substr(tract,1,1)=' ' then tract = translate (tract,'0',' '); *--add leading zeros--; if intr then do; sumrylvl='tr'; sumlev='140'; Geocode=%concatd(county,tract); end; else if inbg then do; sumrylvl='bg'; sumlev='150'; Geocode=%concatd(county,tract,blkgrp); end; else if ined then do; sumrylvl='ed'; sumlev='15e'; Geocode=%concatd(county,ed); end; %mixcase(AreaName); label AreaName='Name of City in which the area is all or partly contained'; label na='# of hierarchal summaries aggregated to get this total'; run; proc freq data=stf3app.&stab.trbged; tables sumrylvl*sumlev / list; table fipco*county / list; title2 "Frequencies from stf3app.&stab.trbged"; run; %mend trbged; options mprint; %trbged(29,mo); options nomprint; %trbged(17,il); *---and, finally, just copy the trapumas dataset, as is, for what it may be worth. Will probably never use it.-; data stf3app.trapumas(label='St. Louis metro west tract area and puma summaries, no editing applied' compress=binary); set trapuma.trapumas; run; ======================End comment================== */ options source; *============================Here we migrate/enhance the B file - ZIP code summaries============================*; proc sort data=v8.uszips( where=(zip not in ('00000',' ') and t1i1 ) drop=urbarurl fstatus psad mcd: plac: tract blkgrp partplac edind ed scsa urbarea ward sea esr doffice cbd uasize indsubr parttrct condist indanv seqoid smsasize uatype xcity sfar ) out=olduszips; by state zip sumrylvl county smsa; run; data newcodes; set olduszips(keep=state sumrylvl zip smsa county t1i1); by state zip; length split 4 pcounty pfipco $5 psmsa $4; label split='Split ZIP flag' pfipco='Primary County FIPS' pcounty='Primary County' psmsa='Primary SMSA'; format pcounty $county.; if t1i1; *--drop pieces with no pop--; *<---already filtered in sort step but wont hurt to emphasize--; if first.zip then do; totpop=t1i1; split=1; retain totpop split; delete; end; if t1i1=totpop then do; pcounty=state||county; pfipco=pcounty; psmsa=smsa; split=0; output; return; end; else if t1i1 > .5 * totpop then do; pcounty=state||county; pfipco=pcounty; psmsa=smsa; split=1; output; return; end; keep sumrylvl state zip county smsa split pcounty pfipco psmsa; run; data uszips; length SumryLvl $2 SumLev $3 ZIP $5 Geocode $20 Areaname $40 State $2 County Fipco $5; format county $county.; merge newcodes(in=innew) olduszips(in=instf3 ); by state zip; if length(county)=3 then county=state||county; fipco=county; if zip=' ' or zip='00000' then zip='99999'; if Sumrylvl='35' then geocode=%concatd(state,zip); else if sumrylvl='36' then geocode=%concatd(state,zip,county,smsa); t3i1=.; t6i1=.; *<--there are no 100% data for zips so these particular items are missing rather than 0--; if sumrylvl='35' then do; SumLev='871'; if not innew then split=1; if not split then do; county=pcounty; fipco=county; smsa=psmsa; end; areaname='ZIP '||zip; output; return; end; if sumrylvl='36' then if split; *<=====We delete redundant within-county summaries!!====; *---we have a level 36 that is needed---; SumLev='881'; *---not exactly but close---; areaname='ZIP '||zip||' ('||trim(put(county,$county.-L))|| ' part)'; format split supflg: 1.; output; run; proc sort data=uszips out=stf803.uszips(label='US 5-digit ZIPs and ZIP/County/SMSA summaries' compress=binary ); by state zip sumlev descending t1i1; run; proc datasets library=stf803 nolist; modify uszips; index create state; index create sg=(SumLev geocode); quit; proc sql; create view stf803.mozips(label='MO subset of uszips') as select * from stf803.uszips where state='29'; quit; options nosource; /* =======================Begin comment================ *==================================Here is our work on whats left of the national file C========================*; *----------------Create a series of national files by simply concatenating selected observations from the state level (A) inventory datasets. Insures consistency. We shall have to retrieve only a small number of levels from the us file, which will not be "enhanced" with marf2 data. -----------------*; options compress=binary; *---This data step was rerun interactively 2-6-06 after running fix_marfdata run improved the lat-long-landarea data for places and cds---; data stf803.usstcnty(label='US State and County data' index=(state) ) stf803.usplaces(label='US Places of any size' index=(geocode) ) stf803.usmcds11(label='MCDs of any size for 11 selected states' index=(state) ) stf803.uscds96th(label='US 96th Congressional Districts'); %macro stcd(state,stab); stf803.&stab.i %mend stcd; set %dostates(n=51,mac=stcd) open=defer ; *--this fancy macro stuff generates the list of 51 inventory state datasets: stf803.ali stf803.aki .... stf803.wyi ---*; where sumlev ne '060' or state in('09','23','25','26','33','34','36','42','44','50','55'); *--we only do mcds for these 11 states---; select (sumlev); when ('040','050') output stf803.usstcnty; when('060') output stf803.usmcds11; when ('160') output stf803.usplaces; when ('500') output stf803.uscds96th; otherwise do; put '*****Unexpected SumLev value encountered. Program aborting ***** ' /_all_; abort abend; end; end; run; proc freq data=stf803.usmcds11(keep=_char_); table state division; format state $state.; title2 'Frequencies from stf803.usmcdcs11 - These are the 11 states'; run; *-----Finally, we create the usmisc dataset which is what is left on the national ("stf3c") file other than what we already captured above by concatenating the A file summaries. Mostly scsas, smsas and uas. ----*; data usnames; *--genreate a datset that can be used to look up names of scsas, smsa, and urbareas--; set v8.us(keep=sumrylvl scsa smsa urbarea areaname); where sumrylvl in ('05','07','09'); %mixcase(areaname); select(sumrylvl); length _key $6; when('05') do; _key='06'||scsa; areaname=trim(areaname)|| ' SCSA'; end; when('07') do; _key='08'||smsa; areaname=trim(areaname)||' SMSA'; end; when('09') do; _key='10'||urbarea; areaname=trim(areaname)||' UA'; end; end; rename areaname=_name; run; proc datasets library=user nolist; modify usnames; index create _key; quit; data stf803.usmisc(label='Summaries for US, Regions, Divs, SCSAs, SMSAs and UAs' index=(SumryLvl) ); length SumLev $3 geocode $16 AreaName $64; set v8.us; where sumrylvl in ('01','02','03','05','06','07','08','09','10'); SumLev=put(sumrylvl,$sltran.); drop urbarurl county place tract blkgrp partplac parttrct edind ed condist sea esr placdesc placesze xcity cbd mcdccdsz indsubr sfar; select(sumrylvl); when('01') geocode=' '; when('02') geocode=region; when('03') geocode=division; when('05') geocode=scsa; when('06') geocode=%concatd(scsa,state); when('07') geocode=smsa; when('08') geocode=%concatd(smsa,state); when('09') geocode=urbarea; when('10') geocode=%concatd(urbarea,state); end; %mixcase(areaname) ; *--converts "NEW ENGLAND" to "New England"--; select(sumrylvl); when('05') do; geocode=scsa; areaname=trim(areaname)|| ' SCSA'; end; when('07') do; geocode=smsa; areaname=trim(areaname)||' SMSA'; end; when('09') do; geocode=urbarea; areaname=trim(areaname)||' UA'; end; otherwise; end; if sumrylvl in ('06','08','10') then do; *---Trick here is to replace (for example) the name "Missouri" for the Mo portion of the St Louis SMSA with " St. Louis SMSA - Missouri portion " --; length _key $6; _key=sumrylvl||scan(geocode,1,'-'); length _name $40; _name=' '; *--getting a false negative on keyed read setting _iorc_ to bogus value when it actually reads OK. So we do this workaround by checking to see if _name gets assinged.--; set usnames key=_key ; Areaname= trim(_name)|| ' - '||trim(areaname)|| ' portion'; if _iorc_ then do; length _errmsg $200; _errmsg=iorcmsg(); if _name=' ' then do; put '**Keyed read failed ... step will be stopped!!!!' / _key= sumrylvl= _name= geocode= _errmsg= _iorc_= _n_= ; _nbad+1; if _nbad ge 2 then stop; _error_=0; end; else do; _iorc_=0; _error_=0; end; end; end; drop _: ; run; proc freq data=stf803.usmisc(keep=sumrylvl); table sumrylvl; title2 'Freqs on stf803.usmisc'; run; ======================End comment================== */