*---------/mscdc/sas/formats/Tools/nemc2ma.sas----------*; x cd /mscdc/sas/formats/Tools; options mprint; %pgm(nemc2ma) options nomprint; /* This creates the nemc2ma format which relates fips county and census mcd code to metro areas for New England states where metros split counties: ct, me, ma, nh, ri, vt. A text file downloaded from the census bureau web site is used as input: 96mfips - county/mcd to metro (cmsa, pmsa, msa) URL www.census.gov/population/estimates/metro-city/96mfips.txt Like Scomet the cmsa or msa is returned followed by 'C' or 'M' if county/mcd is the argument. if the character 'P' follows the county/mcd argument the pmsa or msa is returned followed by 'P'. Format will be tested during creation of index values for MSAs and PMSAs for the crahmda income category products. JWS 7/97 */ %let yr=96; *******Year for the codes (updated as of July); libname library clear; libname library '/mscdc/sasctlgs'; %libname(gics90); filename comcdmet '/uic/products/crahmda/96mfips.txt'; filename text '/mscdc/sas/formats/Snemc2ma.sas'; title 'Creation of July 1996 New England Census County Subdivision ' 'to Metro Area Format'; data nemcdmet; length cmsa2 lcmsa2 state $2 msapmsa $5 manm maname $50 cmsanm cmsaname $60 cntynm cntyname citytown $30; retain maname cmsaname cntyname; infile comcdmet pad firstobs=20; *obs=250; input @3 cmsa2 $char2. @6 msapmsa $char4. @11 county $char5. cousubfp $char5. @; *put _infile_; *put _all_; lcmsa2=lag(cmsa2); if cmsa2 ne ' ' and msapmsa eq ' ' then do; input @22 cmsanm $char60.; if lcmsa2 eq cmsa2 then cmsaname=trim(cmsaname) || ' ' || left(cmsanm); else cmsaname=cmsanm; *put cmsaname=; delete; end; else if cmsa2 eq ' ' then cmsaname=' '; state=county; if msapmsa ne ' '; if county eq ' ' then do; if cmsa2 eq ' ' then input @22 manm $char50.; else input @24 manm $char50.; maname=manm; *put maname=; delete; end; else if cousubfp=' ' then do; input @26 cntynm $char30.; cntyname=cntynm; *put cntyname=; if state in('23','50','25','44','09','33') then delete; *---there are no county assignment records for these states---*; end; else input @28 citytown $char30.; if state in('23','50','25','44','09','33'); if cmsa2 eq ' ' then do; msapmsa=trim(msapmsa) || 'M'; output; end; else do; *---assign 4 digit cmsa code later---*; msapmsa=trim(msapmsa) || 'C';*---value replaced by cmsa later---*; output; substr(msapmsa,5,1)='P'; output; end; drop cmsanm manm cntynm lcmsa2; RUN; proc sort data=nemcdmet(keep=cmsaname cmsa2 where=(cmsa2 ne ' ')) out=cmsa2 nodupkey;by cmsa2; RUN; data cmsa2;length namekey $8;set cmsa2;namekey=substr(cmsaname,1,8); RUN; proc sort data=cmsa2;by namekey;run; proc print data=cmsa2; title2 'Listing of Unique 2-Digit CMSA Codes and Names'; run; data cmsa4nm;*---retrieve 4-digit cmsa codes---*; length namekey $8 mcode $4 metronm $40; do n=12 to 9992 by 10; mcode=put(n,z4.); metronm=put(mcode,$metro.); namekey=substr(metronm,1,8); if index(metronm,'(C)') gt 0 and index(metronm,'-X') eq 0 then output; end; keep mcode metronm namekey; run; proc sort data=cmsa4nm;by namekey;run; data cmsa24_;*---create correspondence of 2 to 4 digit cmsas---*; retain fmtname 'cmsa24_' type 'C'; length start end $2 label $4; merge cmsa2(in=in2) cmsa4nm(in=in4); by namekey; if in2 and in4; start=cmsa2; end=cmsa2; label=mcode; keep fmtname type start end label; run; proc format cntlin=work.cmsa24_ library=work;run; proc print data=cmsa24_(obs=10); title2 'Links for 2 and 4 digit CMSA codes'; run; proc print data=nemcdmet(obs=50); title2 'Correspondences read from text file'; title2 'First 50 obs of correspondences selected from text file'; run; /*---this format already exists---* data newcfmts; set gics90.cousubs(keep=state cnty cousubce cousubfp); by state; if state in('23','50','25','44','09','33'); retain type 'C'; length start $10 label $3; if _n_=1 then do; *<--output other specs; hlo='O'; fmtname='fmcdcnv'; output; end; fmtname='fmcdcnv'; start=state||cnty||cousubfp; label=cousubce; output; run; proc format cntlin=newcfmts library=work; *<--gnerate new format codes; run; *---end of code for existing format---*/ data genfmtin;length arg $9 argfp $10 cousubce $3; set nemcdmet; if county='09009' and cousubfp='47500' then cousubfp='47590'; if county='23019' and cousubfp='55565' then cousubfp='55540'; if county='25001' and cousubfp='03600' then cousubfp='03695'; if county='25009' and cousubfp='37995' then cousubfp='37945'; if county='25009' and cousubfp='40710' then do; cousubfp='40675'; citytown=translate(citytown,' town',' city'); end; if county='25013' and cousubfp='00765' then do; cousubfp='00800'; citytown=translate(citytown,' town',' city'); end; argfp=county || cousubfp; if index(citytown,' city') gt 0 or index(citytown,' borough') gt 0 then do; cousubfp=put(argfp,$nepl2mc.); argfp=county || cousubfp; end; cousubce=put(argfp,$fmcdcnv.); if index(msapmsa,'C') gt 0 then do; msapmsa=put(cmsa2,$cmsa24_.) || 'C'; arg=county || cousubce; end; else if index(msapmsa,'P') gt 0 then arg=county || cousubce || 'P'; else arg=county || cousubce; if county='09001' then put _all_; keep arg msapmsa; run; proc sort data=genfmtin; by arg msapmsa; run; data dups;set genfmtin;by arg;if not first.arg or not last.arg;run; proc print;title2 'dups in genfmtin';run; %genfmat(setin=genfmtin,fmatname=nemc2ma,arglen=9, argvars=arg,labelvar=msapmsa,labellen=5,other=' ',savedoc=0); title2 'Partial Print of Format Source'; data _null_; infile text obs=50; input; file print; put _infile_; run; *---test the format---*; data _null_;length arg $9 result $5 metro $4; set gics90.cousubs(where=(state in('09','25','44') and cnty='009') keep=state cnty cousubce); arg=state || cnty || cousubce; result=put(arg,$nemc2ma.); metro=result; put state= cnty= cousubce= arg= result= ' Metro: ' metro $metro.; if substr(result,5,1)='C' then do; arg=trim(arg) || 'P'; result=put(arg,$nemc2ma.); metro=result; put state= cnty= cousubce= arg= result= ' Metro: ' metro $metro.; end; run; run;