x cd /pub/data/pums2000/Tools; %let pgm=puma5_topuma1; filename pgm "&pgm..sas"; *----Create the pums2000.puma5_topuma1 equivalency file relating puma5 codes to puma1 (superpuma) codes. Use to create the $puma15_ conversion table lookup. (We also read appendix N and M files from Bureau relating puma codes to migpuma and powpuma codes used in the 5 pct sample to create format tables $pumapow and $pumamig, which are then used to add 2 new varsiables to puma5_puma1 dataset.) We do it by just reading the raw data files straight from the zip archives via a pipe---; filename out '/pub/data/corrlst/puma5_topuma1.txt'; libname pums2000 '/pub/data/pums2000'; filename text '/pub/sas/formats/Spuma51_.sas'; title "&pgm: Create 2000 puma to superpuma correspondence file / dataset / format table"; x cd /pub/data/pums2000/zipfiles5; *<---so that our pipe runs from this directory--; *--------------------Part 1: Read all pums2000 data and exract puma1/puma5 combos. Write a txt file--------------; /*<=========================Comment out======================================================== data _null_; file out; put @; run cancel; *<-----!!!CAUTION!!!: for start-overs: blank the file---; %macro doit(state,stab); filename in clear; %local i; %do i=1 %to %length(&statenm); %*put i= &i; %if %substr(&statenm,&i,1)=%str( ) %then %let statenm=%substr(&statenm,1,%eval(&i-1))_%substr(&statenm,%eval(&i+1)); %end; %put statenm= &statenm; filename in pipe "unzip -p all_&statenm PUMS5_&state..TXT"; data _null_; infile in missover end=eod; input rectype $1 state $10-11 puma5 $14-18 puma1 $19-23 ; if rectype='H'; if state ne "&state" then do; file stderr; put "*****Error: State code does not check out while reading data for &state &stab " state=; abort abend; end; retain curpuma1 curpuma5 ' '; if puma1 ne curpuma1 or puma5 ne curpuma5 or eod then do; if curpuma1 ne ' ' then link output; curpuma1=puma1; curpuma5=puma5; end; return; output: file out mod; put state $2. +1 puma5 +1 puma1; return; run; %mend doit; %*dostates(01 35,names=1); %dostates(names=1); data _null_; infile out obs=250; input; list; run; *--------------------------Part 2: Create 2 format codes we can use to look up the pow puma and mig pumas-----; *---Read the CSV files from Excel spreadsheets provided as part of 5 percent pums tech doc showing relationship between regular puma codes and those used for report migration and place of work. We create the $pumamig and $pumapow table-lookup format codes which will then be used to add the migpuma and powpuma variables to the pums2000.puma5_puma1 dataset.---; x cd /pub/data/pums2000/cendocs5; *<---this is where the input CSV files are stored---; %macro genfmt(type); data cntlin&type; infile "&type.puma5.csv" dsd firstobs=5 truncover; length _state $3 _stname $40 &type.puma5 $5 _pumasin $80 _pumas $16; length State $2 start end $7; retain State; input _state -- _pumasin; if length (_state)= 2 then State=_state; if length(&type.puma5)=5; *----Parse the _pumasin field using comma as the delimiter. There are only a few cases where there is more than one value or value interval but we have to allow for the mutltiple specs. E.g. it could have values such as "06001, 06003" or "04800-05000, 05401-05424, 06101-06126"-----*; retain _comma ','; _pumasin=compress(_pumasin,' '); *--remove any embedded blanks--; _pumas=scan(_pumasin,1,_comma); _i=1; loop: if _i gt 8 then stop; else if _i gt 1 then put _all_; start=state||_pumas; if length(_pumas)=11 then end=state||substr(_pumas,7,5); else end=start; *---Special edit *** to correct apparent error in the data provided for PA--; *if start gt end and end='4203202' then end='4203302'; *--multiple errors were found so we decided to just edit the input file instead---; output; _i+1; _pumas=scan(_pumasin,_i,_comma); if _pumas ne ' ' then goto loop; retain fmtname "puma&type" type "C" length 5; rename &type.puma5=label; drop _:; run; proc format cntlin=cntlin&type library=library; run; %mend genfmt; %genfmt(pow); data _null_; set cntlinpow; where start gt end; put _all_; run; %genfmt(mig); ===========================End commented out================================ */ *----------------Part 3: Create a SAS dataset from the txt file written in Part 1-------------------; *----Add the migpuma5 and powpuma5 variables using the format codes created in step 2---; data puma5_puma1; infile out; input State $2. +1 puma5 $5. +2 puma1 $5.; length _arg $7; _arg=state||puma5; length powpuma5 migpuma5 $5; powpuma5=put(_arg,$pumapow.); migpuma5=put(_arg,$pumamig.); if powpuma5=_arg or migpuma5=_arg then put '***check puma lookup codes: ' _all_; drop _arg; run; proc sort out=pums2000.puma5_puma1(label='US 2000 puma to superpuma correspondence' index=(state)) nodupkeys; by state puma5 puma1; run; proc datasets nolist library=pums2000; modify puma5_puma1; index create stpuma=(state puma5) /unique; quit; proc print data=pums2000.puma5_puma1(where=(state='29')); by state; title2 "Missouri portion of pums2000.puma5_puma1 correspondence file"; run; proc freq data=pums2000.puma5_puma1; table state; format state $state.; title2 "Frequency of State on pums2000.puma5_puma1"; run; *---and now the SAS format version---*; %genfmat(setin=pums2000.puma5_puma1, fmtname=puma51_, argvars=state puma5,arglen=7, labelvar=puma1,labellen=5, other=99999, runproc=1 ) run; *--and test---; data _null_; set pums2000.puma5_puma1; where state='29'; length _arg $7; _arg=state||puma5; superpuma=put(_arg,$puma51_.); if superpuma=puma1 then checks='****Checks*****'; else checks='***Failed***'; put _all_; run;