x cd /pub/data/pl942010/Tools; %let pgm=cnvt_sgq_files; filename pgm "&pgm..sas"; *--You can delete this line-; *--- Reads the 2010 Census PL94-171 special group quarters file as posted to and downloaded from http://www2.census.gov/census_2010/02-Advance_Group_Quarters/Missouri/ Coded by John Blodgett, OSEDA, U. of Missouri, Columbia, MO. Under contract with the Missouri Census Data Center. ----*; libname pl942010 '/pub/data/pl942010'; *--This is the directory where the output data sets are saved--; libname pl94 (pl942010); *--alternate name---; %let maxobs=max; *--assign parm a value of max to do a complete run or some numeric value to limit processing to that many rows--; %let filesfx=%str(.sgq); *---ids the kind of data file---; options ls=155 ps=59 nodate nocenter ; options compress=no; x cd /pub/data/pl942010; *<-----the steps to unzip the .zip files at the start and delete them at the end wont work without this-------; %macro doit(state, stab); %*--this is the macro that will be invoked once for each state converted using the dostates driver macro--; %let stabu=%upcase(&stab); %*--upper case: used to verify that the file we are reading is correct-; %let stfips=&state; %*--FIPS state code--; data _null_; call system("unzip &stab.2010.sgq.zip"); run ; *---this did not work. Got an error msg saying it was not a valid zip archive. Had to unzip from Windows--; %if &sysrc gt 0 %then %do; %put ***Non-zero system return code while attempting to unzip files for state: &state &stab &statenm ; %goto dontdoit; %end; title "Convert 2010 PL94-171 Files for &statenm"; *---This code reflects the file-naming conventions found insided the downloaded .zip files from the FTP site---; filename in1 "/pub/data/pl942010/&stab.000012010&filesfx"; ***filename in2 "/pub/data/pl942010/&stab.000022010&filesfx"; filename ingeo "/pub/data/pl942010/&stab.geo2010&filesfx"; *-------------Step 1: Read the comma-delimited pl94 sgq (special Group Qtrs) files ------------*; data p42; retain state "&state"; *--assign FIPS state code as constant--; length LogRecNo 5 default=4; *---We read the dual input files in the following 2 parallel code sections The code for reading in1 and in2 are nearly identical in form. We just read different files into different variables.----; infile in1 lrecl=1024 obs=&maxobs missover dsd; *<---------------Read input file 1----------------; input _first13 $char13. LogRecNo 7. +1 p42i1-p42i10; if _first13 ne "GQ,&stabu.,000,01," then do; _bad13+1; put _first13=; if _bad13 ge 25 then abort 100; *--Just a precaution --; end; if p42i1 > 0 then output; *<---we save a lot of space by NOT KEEPING EMPTY (zero GQ pop) obs--; drop _: ; Label p42i1='Total GQ Pop' p42i2=' Institutionalized pop' p42i3=' Correction facilities adults' p42i4=' Juvenile facilities' p42i5=' Nursing homes' p42i6=' Other institutional facilities' p42i7=' Noninstitutionalized pop' p42i8=' College/University student hsg' p42i9=' Military Qtrs' p42i10=' Other noninstitutional facilities' ; return; run; filename in1 clear; *------------Step 2: Read the geography headers file ------*; data &stab.geos(compress=no); length LogRecNo 5 GeoId $48 SumLev $3 AreaName $90; *--establish variable order -- these go first--; retain State "&state" Stab "&stab"; length County $5 Tract $7; *--and then these... -; length esriid $20; *<---key to match esri shape files as stored in msdis online collection--; infile ingeo lrecl=512 obs=&maxobs; input @9 SumLev $3. @19 LogRecNo 7. @26 Region $1. Division $1. State $2. Cnty $3. CntyCC $2. CntySC $2. @37 CouSubFP $5. CouSubCC $2. CouSubSC $2. PlaceFP $5. PlaceCC $2. /* PlaceDC $1. */ PlaceSC $2. @55 TractIn $6. BG $1. Block $4. @69 ConCit $5. @113 CBSA $5. CBSASC $2. MetDiv $5. CSA $3. NECTA $5. NECTASC $2. NECTADiv $5. CNECTA $3. CBSAPCI $1. NECTAPCI $1. @153 UrbanRur $1. cd111 $2. (sldu sldl)($char3.) vtd $char6. vtdi $1. @169 ZCTA3 $3. ZIP $5. @184 (sdelm sdsec sduni)($5.) @199 (AreaLand AreaWatr)(14.0) AreaName $90. FuncStat $1. gcuni $1. @319 Pop100 9. HU100 9. IntPtLat 11.6 IntPtLon 12.6 LSADC $2. @362 PartFlag $1. TAZ $6. UGA $5. @374 (GNISState GNISCounty GNISCousub GNISPlace )($8.); 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; length _vtdedit $6; _vtdedit=compress(vtd); select(SumLev); *---Assign the standardized Geographic Code to uniquely ID the geographic area--; when('750') Geoid=cat(sumlev,"00US",county,tractin,block); when('140') Geoid=cat(sumlev,"00US",county,tractin); when('050') Geoid=cat(sumlev,"00US",county); when('040') Geoid=cat(sumlev,"00US",state); otherwise do; if sumlev ne lag(sumlev) then do; put '****Unexpected SumLev code encountered: No value assigned to Geoid: ' SumLev= areaname=; end; end; end; *--select group--; esriid=substr(geoid,8); *--Create land and total area values in square miles. --; *---Not really clear yet regarding the units and implied decimal places for the AreaLand and AreaWatr input fields---; LandSQMI=AreaLand/2589988; AreaSQMI=LandSQMI + (AreaWatr/2589988); format LandSQMI AreaSQMI 9.2; label SumLev='Geographic Summary Level'; label LandSQMI='Land Area Sq Mls' AreaSQMI='Total Area Sq Mls' AreaLand='Land Area Sq Meters' AreaWatr='Water Area Sq Meters'; drop cntysc placesc cbsasc nectasc cousubsc ; *<---Not filled in for pl94 files---; drop ZIP zcta3; *<---to avoid confusion since these were never included on the file---; drop _: ; run; filename ingeo clear; *-------------------Step 3: Create the pl94.&stab.gqs data set------------*; data pl942010.&stab.gqs(label="Grp Quarters Data for &statenm") ; merge &stab.geos(in=ing) p42(in=ind); by logrecno; if ind; if not ing then do; put '**Geo Header Data missing : ' _n_= LogRecNo= areaname=; _nomatch+1; if _nomatch ge 100 then abort abend; drop _nomatch; ***areaname ; delete; end; run; *---delete the unzipped input files---; data _null_; call system("rm &stab.*&filesfx"); call system("rm &stab.2010.pl.prd.packinglist.txt"); run; %if &sysrc gt 0 %then %do; %put ***Non-zero system return code while attempting to delete upl files for &state &stab ***; %end; %dontdoit: %mend doit; options mprint; %dostates(17 20 29,names=1); ods html file="gqpops_mocounties.html" style=sasweb; proc print data=pl94.mogqs(where=(sumlev in ('040','050'))) label; title 'Group Quarters Population for Missouri Counties'; id county; format county $county. ; var pop100 p42: ; format pop100 comma9. p42: comma7.; footnote .h=1 "Report generated &sysdate by setup in pl942010/Tools/cnvt_sqg_files.sas"; run; ods html close; *--You can delete this line--;