x cd /pub/data/sf12000/Tools; %let pgm=cnvtsf1; filename pgm "&pgm..sas"; *<====mo only===; ***************Last revision: 12.04.01 ****************; *---This program will read a set of ascii CSV files containing the 2000 SF1 data for a state (or US in the case of the national file). John Blodgett, OSEDA, U. of Missouri Under contract with the Missouri Census Data Center. Part of the joint project of the national State Data Center. May, 2001. ---*; *-------Revision history: 5-4-01: Begin coding. 5-11-01: Test data arrives. 5-17-01: Working version with 4 sets and 4 views generated. 5-25-01: Per note from Sandra Burke added code for p28d variables. 5-31-01: Corrections related to adding variable labels (from Roy Williams, Mass. SDC) 3 auxilliary source modules are included to assign var labels (1 file/label stmt per data set: ph, pct and pct12r). 6-02-01: Mods made after seeing complete tech doc, incl. summary level sequence chart. Decided to go with ZCTA5 instead of ZIP. 6-05-01: Run with live embargoed file (de). 6-08-01: Enhance overview text. Fix bug with input by eliminating _dum1. 6-14-01: Edited overview text. Added concatd macro source code. 6-18-01: Error found in keep option for the phng data set (per Dougherty and Keating). We were not keep the ha thru hi table items. Changed h20i5 to h16Ii19 in keep list. 6-22-01: Adding usage_note commentary re use of $fplace format code. 7-13-01: Add format specs for the IntPtLon and IntPtLat vars. 11-15-01: Mods to work for Advanced National file. Lots of new values for sumlev to assign geocode. Changing the code to read first part of the CSV files -- reading separate comma delimited fields instead of _first12. 11-17-01: Now keeping sumlev on the phng output data set and indexing on it. 12-04-01: Adding code to assign the state abbreviation var, stab, using $fipstab for national files. --------*; /*---------------------------------------------------------------------------------------------------------- Program overview ================ Requirements ============ This code was written for SAS Version 8 or later. It uses names longer than 8 characters and labels longer than 40, which were not supported in earlier versions of SAS. Although it was written and tested on a Unix (IBM AIX) system, there is very little of the code that should be affected by platform. I.e. we expect it should run pretty much as-is in a Windows environemnt. MVS or CMS might be a little more of a challenge. In terms of computer resources, these files are quite large. You will need considerable space for storing the input files (although we try to minimize this by reading directly from zipped versions of the files). You will also need space if you are saving the output data sets. We'll publish estimates of the size requirements on a per-record basis once we have a chance to convert some larger files. Based on the test data we have to work with at the moment (mid-May, 2001) we can only say that we have tried to minimize storage by using length statements and the compress=yes option on the data sets. Invoking ======== As long as nothing goes wrong, and as long as you do not want to create a custom version of the select macro, the person invoking this conversion setup should really not need to know much about SAS. Just enough to be able to point to the appropriate directories and then invoke the program. The program was debugged and tested in a batch environment. There is no user input required (or allowed) while the program is running. Typically, you will save this program in a .sas file somewhere on your system and then invoke the SAS system in batch mode, pointing to this program as the input file (after editing parameters etc as described below). Typically, it would be as simple (in Unix) as sas cnvtsf1 & You can also run the program interactively from Display Manager, of course. The nice thing about a batch invocation is that you can go away and come back later. Conversion runs for large states or on slower systems could take a while. There are over 8000 data cells for each geographic area on these data files, so there is a lot of work to be done. Inputs ====== There are 40 input files, a geographic headers/id file and 39 table cell files. All are zipped ascii files. The geography headers file is fixed format, and the remaining data segment files are all CSV (comma separated value) files (though the Bureau does not use that standard extension in naming their files.) This setup reads the 39 data files using infile pipes to decompress them on the fly. We use the unzip program in the pipe. This program is compatible with pkunzip so Windows users with that program should be able to substitute pkunzip for unzip in the code. A Windows version of unzip is included in this Tools directory (as unzip.zip) which can be downloaded and used. (Alternatively, you can always go ahead and unzip all input files and alter the infile statement within the readloop macro to point to the decompressed files rather than using the pipes.) Outputs ======= The large DATA step creates 4 regular SAS data sets in the sf12000 data library. (The output library is expected to be permanent, so that you only have to run the conversion once, and can then access it as often as you wish. This has the advantage of being *much* faster than having to convert the raw data files each time you want to access the data. But if you prefer to reconvert each time you can easily point the sf12000 library to a temporary directory.) Each output SAS data file name begins with the state postal abbreviation, stored in the global macro variable stab. So, for example, when converting the file for New York, you would be creating the data sets: nygeos Geographic header and id data only (except it does contain Pop100 and HU100 total pop and HU counts). In the SAS views the data in nygeos is merged with the data in each of the other sets. nyphng/nyph Population and housing data available at all geo. levels. nyphng is a SAS data set, with the "ng" suffix standing for "no geocodes". The SAS **view** nyph (which is what most applications should access) simply joins the nygeos and nyph sets to magically make all the geocodes and header data available. nypctng/nypct This pair (data set / view) contains the PCT tables (except for PCT12r tables, see below), which are only available for geographic levels census tract and above. So for block group and block level summaries there WILL BE NO OBSERVATIONS in this data set/view combo. Note that one of the most important reasons for breaking up the data into multiple data sets is to save a lot of space by not having to store all-missing table cell variables for the PCT tables for all the block group and block level summaries. This would be the majority of variables for the majority of observations for a complete state conversion. Breaking the PCT data into 2 sets was just a matter of noting that the PCT12 tables were taking up an enormous amount of space and the data contained in them could probably be restructured to be much smaller and easier to use. nypct12rng/nypct12r This data set contains "only" the racially stratified pct12 tables, i.e., pct12a, pct12b,...,pct12o. Over 3000 data cells. Like the previous table/view pair this data set contains no observations for block group and block level summaries. Since each of these files/views is sorted by the unique keys geo_id and logrecno, it is very easy to put the pieces back together again when using the data sets in an application. For example, to combine all the data sets for the ny sets you could code: data nyall; merge sf1.nyph sf1.nypct sf1.nypct12r; by geo_id; where sumlev in ('050','060') and county='36001'; *<---optional filter---; run; You might even want to define a SAS view that combines all the data into one data file using: proc sql; create view sf1.de as select * from degeos g, dephng ph, depctng pct, depct12rng pct12r where g.geo_id=ph.geo_id and g.geo_id=pct.geo_id and g.geo_id=pct12r.geo_id; quit; This creates the view de (yes, we switched from New York to Delaware here) containing *all* the sf1 tables for the state. Convenient in some cases, but be cautioned that the number of variables in this view is so large (over 8000) that you bump into some SAS limits. Such as the 100-screen limit in fsedit/fsbrowse. Related SAS code ================ This setup references 3 other SAS source modules directly (see the "%include curpath...." statements). These 3 modules contain rather lengthy SAS LABEL statements. The code will run perfectly well if you decide you do not need these labels, in which case you can simply delete the %include statements. Otherwise you should obtain copies of these 3 modules (from the same source where you obtained this code) and store them in the same directory as this code. The file names are phlabs.sas, pctlabs.sas, and pct12rlabs.sas . The 3 modules correspond to the 3 basic data sets created by this conversion. The $fplace format code ======================= (This note added 6.22.01 - better late than never). As part of the conversion step we have implemented a way of overriding the value of the AreaName variable on summary level 155 (place within county) summaries. On the Bureau file the value of AreaName on these records is the name of the county, but we prefer to have the name of the place. If you disagree with us, do a search right now for the string "To leave as-is" and follow the instructions to delete some lines and skip reading the rest of this section. If you want to change the AreaName on 155 summaries you need to use a SAS format code to do so. The code to USE such a format code is already written below. But the code to CREATE the format and make it accessible to your program is not. We recommend you go to the Missouri Census Data Center site (or the SDC Clearinghouse site if you a member of the State Data Center program) and get a fresh copy of the SAS source code that will let you create this format. The URL for accessing this code is: https://mcdc.missouri.edu/sas/formats/Sfplace.sas . (There was a file named fplace.sas in the original SDC distribution but that version had two problems that this one does not: 1. It was at least 2 years old and thus may have been missing entries for any places that came into being or changed codes or names since 1998 or so. 2. It contained the SAS statement: proc format ddname=library ; This can be a problem for those who have no idea what the "ddname=library" means and/or do not want to be bothered with setting up a special permanent catalog for storing this format. Download the format source code and put it in the same directory as this (cnvtsf1.sas) file. Then, add this code just prior to the data step that does the conversion: proc format ; %include 'Sfplace.sas'; *<---or whatever you named the file when you downloaded it.--; run; This defines the format code and you can now access it in the conversion step. It does not get saved in a permanent formats catalog so you will need to run this step in any job that needs to use the $fplace format code. If you would like to save a permanent copy of the format that can be accessed in future jobs without having to access the SAS source code you can code something along the following lines: libname library ''; proc format library=library; %include 'Sfplace.sas'; run; *--to make sure SAS knows to look in this permanent formats catalog so it can find $fplace you need to be sure that whatever library is referenced in the "library=" option of your proc format statement, is known to be one that SAS will search when trying to locate the format. This is handled by the fmtsearch option in SAS. E.g. you need to code something such as : options fmtsearch=(work library ); This would tell SAS to look for $fplace (or any other format code that it does not recognize as one of its own) first in the work.formats catalog, and then in the library.formats catalog. You may want to make this option a permanent part of your SAS environment by placing it in your autoexec.sas file. You can also include the "libname library .." statement there as well so that it will always be defined as part of your session initialization. --*; Parameters, etc. ================ To invoke this program to work for a specific set of files (presumably one state's worth) requires assigning some global macro variables and tweaking some other code. ****************************************IMPORTANT NOTE****************************************************** We have flagged parts of the code that may need to be altered with comments beginning with the characters "*<===". Be sure to search for all occurrences of this string to see what code you might need to change. You need to specify the state being converted by changing the line with %let statements defining the global parms stab and state. To run the program for Illinois, for example, make sure the line reads: %let stab=il; %let state=17; For consistency we strongly recommend using the lowercase for the value of stab. The maxobs parm can be assigned a value that will cause the program to run in test mode, stopping after processing the first &maxobs records off the input files. Use this for testing or when you want to do a temporary convert and know where the last record of interest is on the input files. For example, specify %let maxobs=1; to convert only the first (state summary) records. Assign a value to the inpath parameter to specify the path (directory) where the input files and output SAS data sets are stored. If you do not wish to store the SAS data sets in the same directory as the input files then you need to edit the "libname sf12000 ..." statement. A select macro is provided as a model and is referenced (invoked) within the conversion step. The code in this macro is executed after reading the geographic headers data, but **before** any data files are read. So it can access any of the fields/variables in the header record only. It needs to assign a value of 0 or 1 to the variable _keep. The program will interpret a value of _keep= to mean you want to skip processing this geographic area. It will handle the logic of skipping over the records in all 39 (at most, less for sub-tract geography) input files. Use this to code a "selective convert" of specific geographic entities. For example if you coded: if sumlev='160' and Pop100 ge 10000 then _keep=1; else _keep=0; You would be converting only place-level summaries for places with at least 10000 total population. ----------------------------------------------------------------------------------------------------------- */ filename curpath '/pub/data/sf12000/sf1pros'; *<===points to current directory. We shall do some %include s below that reference sas label-statement modules in the same directory as the current program.--; %let inpath=%str(/pub/data/sf12000); *<===path (directory) where the input raw data is stored---*; libname sf12000 "&inpath"; *<===========output sas data sets stored is same dir as input files======; libname sf1 "&inpath"; *--You can use alternate name if you want. Mix and match--; /*<=========================Comment out (previous conversions)===================================== %let stab=ne; %let state=31; %let stab=de; %let state=10; *<=======assign the state postal abbrevation and fips codes========; %let stab=mo; %let state=29; *<=======assign the state postal abbrevation and fips codes========; %let stab=il; %let state=17; *<=======assign the state postal abbrevation and fips codes========; %let stab=ks; %let state=20; *<=======assign the state postal abbrevation and fips codes========; %let stab=md; %let state=24; *<=======assign the state postal abbrevation and fips codes========; ===========================End commented out======================================================== */ %let stab=us; %let state=00; *<========reading Advanced National File, 11-2001======; %let ucstab=%upcase(&stab); title "Conversion of 2000 Summary File 1 Data for &ucstab"; %let maxobs=9999999; *<=======while testing, limits processing to this many input recs==; %let filesfx=u1; *let filesfx=uf1; *--they changed the file naming conventions on the Advanced Natl file!--; filename geos "&inpath/&stab.geo.&filesfx"; *<====assumes you have unzipped the .zip file with this inside; x cd &inpath; *<---make it the current directory--; %macro select; %*--This macro will be invoked when reading the headers data. It should assign a value of 1 to _keep to indicate that the record/observation is to be processed. 1 means read the data, 0 means do not.--; /* if sumlev in ('040','050') then _keep=1; else _keep=0; */ *<====For example==; *************************FOR US Advanced National File do NOT keep the level 070 summaries****************; if sumlev='070' then _keep=0; else _keep=1; * _keep=1; *<=====************************************To convert the entire data set (default) ***********; **<===add/substitute your custom code here. You can access any variable on the geography headers file--; %mend select; data sf12000.&stab.geos (compress=yes sortedby=geo_id label="2000 SF1 Geo Headers data for &ucstab" keep=geo_id--AreaSQMI PCT) sf12000.&stab.phng(compress=yes sortedby=geo_id label="2000 SF1 P & H Tables for &ucstab" keep=geo_id logrecno sumlev p1i1--p35Ii20 h1i1--h16Ii19 ) sf12000.&stab.pctng(compress=yes sortedby=geo_id label="2000 SF1 PCT tables exc PCT12r for &ucstab" keep=geo_id logrecno pct1i1--pct17i75 pct13ai1--pct17Ii75) sf12000.&stab.pct12Rng(compress=yes sortedby=geo_id label="2000 SF1 PCT Race Sfx Tables for &ucstab" keep=geo_id logrecno pct12ai1--pct12oi209); *<-----list of variables to keep for the phng data set edited 6.18.01: h16Ii19 replaces h20i5----; infile geos missover lrecl=1024 obs=&maxobs; retain _first 1; drop _first; length geo_id 5 GeoCode $44 SumLev $3 GeoComp $2 AreaName $90; *--establish variable order -- these go first--; retain State "&state" Stab "&stab"; length County $5 Tract $7; *--and then these... -; if _first then do; input FileId $char6. stusab $2. @; retain FileId stusab; drop fileid stusab; if substr(FileId,2,3) ne 'SF1' then do; file log; put '******Problem with input geographic headers file. Did not find "SF1" in cols. 2-4 ' FileId= / '***Conversion will not run***'; stop; end; end; input @9 SumLev $3. geocomp $2. @19 LogRecNo 7. @26 Region $1. Division $1. StateCe $2. State $2. Cnty $3. CntySC $2. @37 CouSubFP $5. CouSubCC $2. CouSubSC $2. PlaceFP $5. PlaceCC $2. PlaceDC $1. PlaceSC $2. @56 TractIn $6. BG $1. Block $4. @69 ConCit $5. @78 aianhh $char4. aianhhfp $char5. aianhhcc $char2. aihhtli $1. aitsce $char3. aits $char5. aitscc $char2. anrc $char5. anrccc $char2. @107 MSACMSA $4. MASC $2. CMSA2 $2. MACCI $1. PMSA $4. NECMA $4. @136 UrbanRur $1. cd106 $2. +6 (sldu sldl)($char3.) vtd $char6. vtdi $1. @158 ZCTA3 $3. ZCTA5 $5. @173 (AreaLand AreaWatr)(14.) AreaName $90. FuncStat $1. gcuni $1. @293 Pop100 9. HU100 9. IntPtLat 9.6 IntPtLon 10.6 LSADC $2. @332 PartFlag $1. (SDElm SDSec SDUni)($5.) TAZ $6. UGA $5. PUMA5 $5. PUMA1 $5. @384 MACC $char5. UACP $char5. ; %select ; *<-----Invoke the macro that will filter based on value of sumlev-------------------*; geo_id+1; if cnty ne ' ' then County=state||cnty; *--edit the tract so that it is in xxxx.xx format with leading and trailing zeroes--; if TractIn ne ' ' then Tract=translate( substr(TractIn,1,4)||'.'||substr(TractIn,5,2) ,'0',' '); drop tractin; %macro concatd(g1,g2,g3,g4,g5,g6,g7,g8); %*--utility macro used to create geocode values-; %*--utility macro to return a SAS expression that will be the concatenation of specified char variables separated by dashes-; %local dash gc; %let dash=%str(||'-'||); %*--we build the value of local variable gc which is then "returned" as the result of invoking the macro-; %let gc=&g1; %if &g2= %then %goto rtrn; %let gc=&gc&dash&g2; %if &g3= %then %goto rtrn; %let gc=&gc&dash&g3; %if &g4= %then %goto rtrn; %let gc=&gc&dash&g4; %if &g5= %then %goto rtrn; %let gc=&gc&dash&g5; %if &g6= %then %goto rtrn; %let gc=&gc&dash&g6; %if &g7= %then %goto rtrn; %let gc=&gc&dash&g7; %if &g8= %then %goto rtrn; %let gc=&gc&dash&g8; %rtrn: &gc %mend concatd; select(SumLev); *---Assign the standardized Geographic Code to uniquely ID the geographic area--; when('010') geocode=' '; when('020') geocode=region; when('030') geocode=division; when('040') geocode=state; when('050') geocode=county; when('060') geocode=%concatd(county,cousubfp); when('070') geocode=%concatd(county,cousubfp,placefp); when('080') geocode=%concatd(county,cousubfp,placefp,tract); when('091') geocode=%concatd(county,cousubfp,placefp,tract,aianhhfp,anrc,cd106,bg); ***????***; when('101') geocode=%concatd(county,tract,block); when('140') geocode=%concatd(county,tract); when('144') geocode=%concatd(county,tract,aianhhfp); when('150') geocode=%concatd(county,tract,bg); when('154') geocode=%concatd(county,tract,bg,aianhhfp); when('155') do; geocode=%concatd(county,placefp); *<===Override areaname to make it the name of the place rather than the county.To leave as-is delete this + next 4 lines; length _arg $7; _arg=state||placefp; drop _arg; areaname=put(_arg,$fplace.); if areaname=_arg then areaname='Place '||placefp; end; when('158') geocode=%concatd(state,placefp,cnty,tract); when('160') geocode=%concatd(state,placefp); when('170') geocode=%concatd(state,concit); when('172') geocode=%concatd(state,concit,placefp); when('250') geocode=aianhh; when('260') geocode=%concatd(aianhhfp,state); when('270') geocode=%concatd(aianhhfp,cnty); when('271') geocode=%concatd(aianhhfp,cnty,cousubfp); when('273') geocode=%concatd(aianhhfp,cnty,cousubfp,placefp); when('252') geocode=aianhh; when('262') geocode=%concatd(aianhhfp,state); when('272') geocode=%concatd(aianhhfp,cnty); when('275') geocode=%concatd(aianhhfp,cnty,cousubfp); when('276') geocode=%concatd(aianhhfp,cnty,cousubfp,placefp); when('254') geocode=aianhh; when('264') geocode=%concatd(aianhhfp,state); when('274') geocode=%concatd(aianhhfp,cnty); when('277') geocode=%concatd(aianhhfp,cnty,cousubfp); when('278') geocode=%concatd(aianhhfp,cnty,cousubfp,placefp); when('256','291','292') geocode=%concatd(aianhhfp,tract); when('258','293','294') geocode=%concatd(aianhhfp,tract,bg); when('251','253','255') geocode=%concatd(aianhhfp,aits); when('257') geocode=%concatd(aianhhfp,aits,tract); when('259') geocode=%concatd(aianhhfp,aits,tract,bg); when('290') geocode=%concatd(aianhhfp,aits,state); when('280','283','286') geocode=%concatd(state,aianhhfp); when('282','285','288') geocode=%concatd(state,aianhhfp,cnty); when('261','265','267') geocode=%concatd(state,aianhhfp,cnty,cousubfp); when('263','266','268') geocode=%concatd(state,aianhhfp,cnty,cousubfp,placefp); when('281','284','287') geocode=%concatd(state,aianhhfp,aits); when('230') geocode=%concatd(state,anrc); when('380') geocode=msacmsa; when('381') geocode=%concatd(msacmsa,state); when('382') geocode=%concatd(msacmsa,state,placefp); when('383') geocode=%concatd(msacmsa,state,cnty); when('384') geocode=%concatd(msacmsa,state,cousubfp); when('385') geocode=pmsa; when('386') geocode=%concatd(pmsa,state); when('387') geocode=%concatd(pmsa,state,cnty); when('388') geocode=%concatd(pmsa,state,cousubfp); when('370') geocode=necma; when('371') geocode=%concatd(necma,state); when('372') geocode=%concatd(pmsa,state,placefp); when('373') geocode=%concatd(pmsa,state,cnty); when('390') geocode=%concatd(state,msacmsa); when('391') geocode=%concatd(state,msacmsa,placefp); when('392') geocode=%concatd(state,msacmsa,cnty); when('393') geocode=%concatd(state,msacmsa,cnty,cousubfp); when('395') geocode=%concatd(state,msacmsa,pmsa); when('396') geocode=%concatd(state,msacmsa,pmsa,cnty); when('397') geocode=%concatd(state,msacmsa,pmsa,cnty,cousubfp); when('374') geocode=%concatd(state,necma); when('375') geocode=%concatd(state,necma,placefp); when('376') geocode=%concatd(state,necma,cnty); when('500') geocode=%concatd(state,cd106); when('510') geocode=%concatd(state,cd106,cnty); when('511') geocode=%concatd(state,cd106,cnty,tract); when('521') geocode=%concatd(state,cd106,cnty,cousubfp); when('531') geocode=%concatd(state,cd106,placefp); when('541') geocode=%concatd(state,cd106,concit,placefp); when('550','551','552') geocode=%concatd(state,cd106,aianhhfp); when('553','554','555') geocode=%concatd(state,cd106,aianhhfp,aits); when('850') geocode=zcta3; when('860') geocode=zcta5; when('851') geocode=%concatd(state,zcta3); when('852') geocode=%concatd(state,zcta3,cnty); when('871') geocode=%concatd(state,zcta5); when('881') geocode=%concatd(state,zcta5,cnty); otherwise do; if sumlev ne lag(sumlev) then do; _badsl+1; drop _badsl; if _badsl le 50 then put '**Unrecognized SUMLEV: ' sumlev; end; end; end; *--select group--; geocode=compress(geocode,' '); *--remove any blanks from geocode-----------------(mod 4.21.01)---; %macro astab; %*---Adding this macro code 12-4-01 to define the stab (State Abbreviation) variable when converting a national file---*; %if %quote(&Ucstab) eq US %then %do; if state=' ' then stab=' '; else stab=put(state,$fipstab.); *<====requires the $fipstab format code be defined. See https://mcdc.missouri.edu/sas/formats/Sfipstab.sas --; %end; %*--otherwise we have a retain statement above that causes variable stab to have a value of "&stab"-; %mend astab; %astab *--Set flag to indicate whether this summary level will have data for the CT tables--; *_readPCT= ( sumlev not in ('091','101','150','154','258','293','294','259') ); *--subtract geo summmary levels--; _readPCT= ( sumlev not in ('091','101','150','154') ); *--Tribal block groups are not sub-tract apparently--; attrib PCT length=3 format=1. label='PCT data flag'; PCT=_readPCT; *<--------save the flag on the geos data set-------------------------; ***temporary diagnostic ***** put geocode= _N_= areaname= sumlev= _readPCT=; *--Create land and total area values in square miles. --; LandSQMI=AreaLand/2589988; AreaSQMI=LandSQMI + (AreaWatr/2589988); format LandSQMI AreaSQMI 9.2; format IntPtLon 11.6 IntPtLat 10.6; *<--added 7-13-01-; label SumLev='Geographic Summary Level' GeoComp='Geographic Component'; label cnty='County code'; label LandSQMI='Land Area Sq Mls' AreaSQMI='Total Area Sq Mls' AreaLand='Land Area Sq Meters' AreaWatr='Water Area Sq Meters'; label cd106='Cong District - 106th (1998)' sldu='State Leg District Upper Chbr' sldl='State Leg District Lower Chbr'; label ZCTA5='ZIP Census Tabulation Area'; *---For processing zip codes on the national files we assign the "primary" state code associated with the zip or zip center (zcta5 or zcta3) using the builtin SAS functions zipfips and zipstate--; if sumlev in ('850','860') then do; length _ziparg $5 ; if sumlev='850' then _ziparg=zcta3||'01'; else _ziparg=zcta5; if sumlev='860' and substr(zcta5,4,2) in ("XX","HH") then _ziparg=substr(zcta5,1,3)||'01'; state= put ( zipfips(_ziparg), z2. ); stab = lowcase ( zipstate(_ziparg) ); if stab=' ' then do; _error_=0; file log; _nziperr+1; if _nziperr=1 then put '****ZIP codes not assigned to states****'; put +2 _ziparg $5. @@; end; drop _ziparg _nziperr; end; *---Use a length statement to set up the variables in order in the PDV (program data vector). Then we can used "double-dash" variable intervals in the input statements and SAS will know which variables are in those intervals----*; %*--assign the macro vars containing the first and last variables in each input file--; length p1i1 p2i1 - p2i6 p3i1-p3i71 p4i1-p4i73 p5i1-p5i71 5; %let fvar1=p1i1; %let lvar1=p5i71; length p6i1-p6i73 p7i1-p7i8 p8i1-p8i17 p9i1-p9i7 p10i1-p10i15 p11i1 p12i1-p12i49 p13i1-p13i3 p14i1-p14i43 p15i1 p16i1 p17i1 p18i1-p18i19 5; %let fvar2=p6i1; %let lvar2=p18i19; length p19i1-p19i19 p20i1-p20i31 p21i1-p21i19 p22i1-p22i11 p23i1-p23i11 p24i1-p24i11 p25i1-p25i3 p26i1-p26i16 p27i1-p27i27 p28i1-p28i17 p29i1-p29i46 p30i1-p30i22 p31i1 p32i1 p33i1 5; %let fvar3=p19i1; %let lvar3=p33i1; length p34i1-p34i20 p35i1-p35i20 p36i1-p36i20 p37i1-p37i9 p38i1-p38i57 p39i1-p39i5 p40i1-p40i3 p41i1-p41i3 p42i1-p42i3 p43i1-p43i3 p44i1-p44i3 p45i1-p45i3 5; %let fvar4=p34i1; %let lvar4=p45i3; length p12ai1-p12ai49 p12bi1-p12bi49 p12ci1-p12ci49 p12di1-p12di49 p12ei1-p12ei49 5; %let fvar5=p12ai1; %let lvar5=p12ei49; length p12fi1-p12fi49 p12gi1-p12gi49 p12hi1-p12hi49 p12Ii1-p12Ii49 p13ai1-p13ai3 p13bi1-p13bi3 p13ci1-p13ci3 p13di1-p13di3 p13ei1-p13ei3 p13fi1-p13fi3 p13gi1-p13gi3 p13hi1-p13hi3 p13Ii1-p13Ii3 p15ai1 p15bi1 p15ci1 p15di1 p15ei1 p15fi1 p15gi1 p15hi1 p15Ii1 p16ai1 p16bi1 p16ci1 p16di1 p16ei1 p16fi1 p16gi1 p16Hi1 p16Ii1 5; %let fvar6=p12fi1; %let lvar6=p16Ii1; length p17ai1 p17bi1 p17ci1 p17di1 p17ei1 p17fi1 p17gi1 p17hi1 p17Ii1 p26ai1-p26ai16 p26bi1-p26bi16 p26ci1-p26ci16 p26di1-p26di16 p26ei1-p26ei16 p26fi1-p26fi16 p26gi1-p26gi16 p26hi1-p26hi16 p26Ii1-p26Ii16 p27ai1-p27ai27 p27bi1-p27bi27 p27ci1-p27ci27 5; %let fvar7=p17ai1; %let lvar7=p27ci27; length p27di1-p27di27 p27ei1-p27ei27 p27fi1-p27fi27 p27gi1-p27gi27 p27hi1-p27hi27 p27Ii1-p27Ii27 p28ai1-p28ai17 p28bi1-p28bi17 p28ci1-p28ci17 p28di1-p28di17 p28ei1-p28ei17 5; %let fvar8=p27di1; %let lvar8=p28ei17; length p28fi1-p28fi17 p28gi1-p28gi17 p28hi1-p28hi17 p28Ii1-p28Ii17 p30ai1-p30ai22 p30bi1-p30bi22 p30ci1-p30ci22 p30di1-p30di22 p30ei1-p30ei22 p30fi1-p30fi22 p30gi1-p30gi22 p30hi1-p30hi22 5; %let fvar9=p28fi1; %let lvar9=p30hi22; length p30Ii1-p30Ii22 p31ai1 p31bi1 p31ci1 p31di1 p31ei1 p31fi1 p31gi1 p31hi1 p31Ii1 p32ai1 p32bi1 p32ci1 p32di1 p32ei1 p32fi1 p32gi1 p32hi1 p32Ii1 p33ai1 p33bi1 p33ci1 p33di1 p33ei1 p33fi1 p33gi1 p33hi1 p33Ii1 p34ai1-p34ai20 p34bi1-p34bi20 p34ci1-p34ci20 p34di1-p34di20 p34ei1-p34ei20 p34fi1-p34fi20 p34gi1-p34gi20 p34hi1-p34hi20 p34Ii1-p34Ii20 5; %let fvar10=p30Ii1; %let lvar10=p34Ii20; length p35ai1-p35ai20 p35bi1-p35bi20 p35ci1-p35ci20 p35di1-p35di20 p35ei1-p35ei20 p35fi1-p35fi20 p35gi1-p35gi20 p35hi1-p35hi20 p35Ii1-p35Ii20 5; %let fvar11=p35ai1; %let lvar11=p35Ii20; length pct1i1-pct1i47 pct2i1-pct2i47 pct3i1-pct3i47 pct4i1-pct4i9 pct5i1-pct5i19 pct6i1-pct6i19 pct7i1-pct7i19 pct8i1-pct8i14 pct9i1-pct9i14 5; %let fvar12=pct1i1; %let lvar12=pct9i14; length pct10i1-pct10i14 pct11i1-pct11i31 5; %let fvar13=pct10i1; %let lvar13=pct11i31; length pct12i1-pct12i209 5; %let fvar14=pct12i1; %let lvar14=pct12i209; length pct13i1-pct13i49 pct14i1-pct14i7 pct15i1-pct15i13 pct16i1-pct16i52 pct17i1-pct17i75 5; %let fvar15=pct13i1; %let lvar15=pct17i75; length pct12ai1-pct12ai209 5; %let fvar16=pct12ai1; %let lvar16=pct12ai209; length pct12bi1-pct12bi209 5; %let fvar17=pct12bi1; %let lvar17=pct12bi209; length pct12ci1-pct12ci209 5; %let fvar18=pct12ci1; %let lvar18=pct12ci209; length pct12di1-pct12di209 5; %let fvar19=pct12di1; %let lvar19=pct12di209; length pct12ei1-pct12ei209 5; %let fvar20=pct12ei1; %let lvar20=pct12ei209; length pct12fi1-pct12fi209 5; %let fvar21=pct12fi1; %let lvar21=pct12fi209; length pct12gi1-pct12gi209 5; %let fvar22=pct12gi1; %let lvar22=pct12gi209; length pct12hi1-pct12hi209 5; %let fvar23=pct12hi1; %let lvar23=pct12hi209; length pct12Ii1-pct12Ii209 5; %let fvar24=pct12Ii1; %let lvar24=pct12Ii209; length pct12ji1-pct12ji209 5; %let fvar25=pct12ji1; %let lvar25=pct12ji209; length pct12ki1-pct12ki209 5; %let fvar26=pct12ki1; %let lvar26=pct12ki209; length pct12li1-pct12li209 5; %let fvar27=pct12li1; %let lvar27=pct12li209; length pct12mi1-pct12mi209 5; %let fvar28=pct12mi1; %let lvar28=pct12mi209; length pct12ni1-pct12ni209 5; %let fvar29=pct12ni1; %let lvar29=pct12ni209; length pct12oi1-pct12oi209 5; %let fvar30=pct12oi1; %let lvar30=pct12oi209; length pct13ai1-pct13ai49 pct13bi1-pct13bi49 pct13ci1-pct13ci49 pct13di1-pct13di49 pct13ei1-pct13ei49 5; %let fvar31=pct13ai1; %let lvar31=pct13ei49; length pct13fi1-pct13fi49 pct13gi1-pct13gi49 pct13hi1-pct13hi49 pct13Ii1-pct13Ii49 pct15ai1-pct15ai13 pct15bi1-pct15bi13 pct15ci1-pct15ci13 5; %let fvar32=pct13fi1; %let lvar32=pct15ci13; length pct15di1-pct15di13 pct15ei1-pct15ei13 pct15fi1-pct15fi13 pct15gi1-pct15gi13 pct15hi1-pct15hi13 pct15Ii1-pct15Ii13 pct17ai1-pct17ai75 pct17bi1-pct17bi75 5; %let fvar33=pct15di1; %let lvar33=pct17bi75; length pct17ci1-pct17ci75 pct17di1-pct17di75 pct17ei1-pct17ei75 5; %let fvar34=pct17ci1; %let lvar34=pct17ei75; length pct17fi1-pct17fi75 pct17gi1-pct17gi75 pct17hi1-pct17hi75 5; %let fvar35=pct17fi1; %let lvar35=pct17hi75; length pct17Ii1-pct17Ii75 5; %let fvar36=pct17Ii1; %let lvar36=pct17Ii75; length h1i1 h2i1-h2i6 h3i1-h3i3 h4i1-h4i3 h5i1-h5i7 h6i1-h6i8 h7i1-h7i17 h8i1-h8i7 h9i1-h9i15 h10i1 h11i1-h11i3 h12i1-h12i3 h13i1-h13i8 h14i1-h14i17 h15i1-h15i17 h16i1-h16i19 h17i1-h17i69 h18i1-h18i3 h19i1-h19i5 h20i1-h20i5 5; %let fvar37=h1i1; %let lvar37=h20i5; length h11ai1-h11ai3 h11bi1-h11bi3 h11ci1-h11ci3 h11di1-h11di3 h11ei1-h11ei3 h11fi1-h11fi3 h11gi1-h11gi3 h11hi1-h11hi3 h11Ii1-h11Ii3 h12ai1-h12ai3 h12bi1-h12bi3 h12ci1-h12ci3 h12di1-h12di3 h12ei1-h12ei3 h12fi1-h12fi3 h12gi1-h12gi3 h12hi1-h12hi3 h12Ii1-h12Ii3 h15ai1-h15ai17 h15bi1-h15bi17 h15ci1-h15ci17 h15di1-h15di17 h15ei1-h15ei17 h15fi1-h15fi17 h15gi1-h15gi17 h15hi1-h15hi17 h15Ii1-h15Ii17 5; %let fvar38=h11ai1; %let lvar38=h15Ii17; length h16ai1-h16ai19 h16bi1-h16bi19 h16ci1-h16ci19 h16di1-h16di19 h16ei1-h16ei19 h16fi1-h16fi19 h16gi1-h16gi19 h16hi1-h16hi19 h16Ii1-h16Ii19 5; %let fvar39=h16ai1; %let lvar39=h16Ii19; *---You can comment out or delete these 2 statements if you do not want or need the variable labels. These modules created by Roy Williams, MISER, Mass. SDC from the SF1 data dictionary file. -----*; %include curpath(phlabs)/nosource2; *--include sas label statements for the P and H tables--; %include curpath(pctlabs)/nosource2; *--include sas label statements for the PCT tables (except pct12r).--; %include curpath(pct12rlabs)/nosource2; *--include sas label statements for the PCT12r tables.--; %macro readloop; %local ifile i2; %*----loop to read from all 39 input files -- at least those that are relevant----*; %do ifile=1 %to 39; %if &ifile < 10 %then %let i2=0&ifile; %else %let i2=&ifile; filename in&ifile pipe "unzip -a -p &stab.000&i2._&filesfx"; %*<==========NOTE====================; _iseg="&i2"; _isegck=' '; %*--in case we do not read the segment because it is a PCT segment for a below-tract sumlev--; infile in&ifile obs=&maxobs dsd missover lrecl=2048; %*<===lrecl may need to be increased for larger areas==; %if %substr(&&fvar&ifile , 1,3) =pct %then %let pctfile=1; %else %let pctfile=0; %*--We read the variables associated with the ith input file. Except that for certain summary levels and certain input files there will not be any data. We check for this with the _readPCT flag and generate that extra conditions when we detect that we have a file with pct variables-; if _keep %if &pctfile %then %str( and _readPCT ); then input _fileid $ _stateid $ _000 $ _isegck $2. +1 logrecnock 7. +1 &&fvar&ifile -- &&lvar&ifile ; *<--------here is where we reap reward for decaring all vars in order-; %if &pctfile %then %str( else if _readPCT then input; ); %else %str( else input;); %*then input _first12 $char12. _isegck $2. +1 logrecnock 7. +1 ; %*<=====old code, replace 11-01===; %*--to flush the record when _keep=0---; if _isegck ne ' ' then link cklogrecno; %end; %*--do ifile loop--------*; %mend readloop; options mprint; *-------------------Here is the big loop where we read up to 39 files (segments)-------------------*; %readloop ; _first=0; if _keep; *---subsetting if. No observation output if _keep=0---; *--Set tables p2 and h2 (Urban and Rural) to missing values if not the final national file--; if FileId ne 'uSF1F' then do; array p2(6) p2i1-p2i6; array h2(6) h2i1-h2i6; do _i=2 to 5; p2(_i)=.; h2(_i)=.; end; drop _i; end; *-----output the data sets. Conditionally, in the case of the 2 pct sets---------------*; output sf12000.&stab.geos; output sf12000.&stab.phng; if _readPCT then do; output sf12000.&stab.pctng; output sf12000.&stab.pct12Rng; end; drop logrecnock _isegck _iseg _readPCT _keep _fileid _stateid _000 ; **_first12; return; cklogrecno: if logrecno ne logrecnock or _isegck ne _iseg then do; file log; put ///"********Problem with file synching: LogRecNo on segment " _iseg " does not match the geo headers value " "or segment code not expected. ******" / "Reading seg file " _iseg +1 'found segment code ' _isegck / LogRecNo= logrecnock= _n_= / _all_ / '******Job is aborting*********************************' ; stop; end; *---Diagnostic print statements are being commented out here----------------------------*; *file print; *put _n_= logrecno= _isegck= _iseg= areaname=; return; run; *-----------Create views that combine the geo headers data with the table sets--------------------*; proc sql; create view sf12000.&stab.ph as select * from &stab.geos g, &stab.phng(drop=logrecno) d where d.geo_id = g.geo_id; create view sf12000.&stab.pct as select * from &stab.geos g, &stab.pctng(drop=logrecno) d where d.geo_id = g.geo_id; create view sf12000.&stab.pct12r as select * from &stab.geos g, &stab.pct12rng(drop=logrecno) d where d.geo_id = g.geo_id; quit; proc datasets library=sf12000 nolist; *<---new, 11-17-2001--; modify &stab.phng; index create SumLev; modify &stab.geos; index create SumLev; quit; proc contents data=sf12000._all_ details nods; run; *libname(sf12000,subdir=sf1test); data sumlevs/view=sumlevs; set sf1test.usgeos(keep=SumLev State); sumlev_code=sumlev; run; proc tabulate data=sumlevs; class sumlev_code sumlev; format sumlev $sumlev.; table sumlev_code*SumLev ; title2 'Frequency Report: Geography Summary Levels'; run; *<=== mo unix only==;