x cd /pub/data/cbp/Tools; %let pgm=cnvtzbp0709; filename pgm "&pgm..sas"; /*--Read ZIP business patterns data (it is the same kind of data as cbp, but with less detail and for ZIP codes). There are two files each year, one with just totals incl payrolls and employees, the other just establishment counts by industry (naics). jgb, Nov 2011 ---*/ libname cbp '/pub/data/cbp'; options mprint; *<====while debugging only--; options compress=no; %MACRO zbpcnv2( revdate=11/15/2011, /* *<=========Date of last revision======== */ yr=, /* REQUIRED. specifies 2-digit year of data */ datadir=%str(/pub/data/cbp/downloads), /* path of input files. The directory where we store .zip files downloaded from the Bureau web site -- http://www.census.gov/econ/cbp/download/07_data/index.htm, for example */ saslib=cbp, /* libref of output datasets. SAS output dataset names are: ustots&yr, states&yr, uscntys&yr, uscbsas&yr and usziptots&yr/uszipsdetail&yr. The invoking program obviously should contain a libname statement to define this libref.*/ obsin=9999999, /* # of input records to be processed. For test runs. */ obs=, /* alias for obsin */ debug=0); /* debug=1 triggers some extra output msgs */ %local year; %let year=20&yr; %local dsd; %*--assign value of null or dsd depending on wether we want to declare input as a CSV file; %let naicsfmt=naics02_; x cd &datadir; filename zbptots pipe "unzip -apq zbp&yr.totals.zip" ; data cbp.usziptots&yr(label="ZIP Bus Patterns Totals, &year"); infile zbptots firstobs=2 dsd obs=&obsin lrecl=512; length ZIP $5 Areaname $40 State Stab $2 default=5; retain Year "&year"; input zip areaname EmpFlag :$1. Emp_NF :$1. TotEmp QP1_NF :$1. Q1Payroll AP_NF :$1. AnPayroll Nestabs; _nfips=zipfips(zip); State=put(_nfips,z2.); Stab=put(state,$fipstab.); *<----you could also invoke the zipstate function here with zip as the arg---; if EmpFlag ne ' ' then do; %do j=1 %to 12; %let flag=%substr(ABCEFGHIJKLM,&j,1); %*--if the flag has a value of M (for example) then we assing the Emp&size variable a (missing) value .M --; if EmpFlag="&flag" then TotEmp=.&flag ; %if &j ne 12 %then %str ( else ); %end; end; if EmpFlag=' ' then do; if Emp_NF='D' then TotEmp=.D; else if Emp_NF='S' then TotEmp=.S; end; if QP1_NF='D' then Q1Payroll =.D; else if QP1_NF='S' then Q1Payroll =.S; if AP_NF='D' then AnPayroll=.D; else if AP_NF='S' then AnPayroll=.S; drop _: ; run; filename zbpdetl pipe "unzip -apq zbp&yr.detail.zip"; data cbp.uszipdetails&yr(label="ZIP Bus Patterns NAICS detail, &year"); length ZIP $5 state stab $2 default=4; length naics $6 nlevel $1 Industry $6; format industry $&naicsfmt..; *--so internally they are similar, but industry prints as the name-; retain Year "&year"; infile zbpdetl firstobs=2 dsd obs=&obsin lrecl=1024; input zip naics Nestabs Nestabs_1_4 Nestabs_5_9 Nestabs_10_19 Nestabs_20_49 Nestabs_50_99 Nestabs_100_249 Nestabs_250_499 Nestabs_500_999 Nestabs_1000; if _n_ gt &obsin then stop; /** Limit the number of observations **/ if zip ne lag(zip) then do; _nfips=zipfips(zip); State=put(_nfips,z2.); Stab=put(state,$fipstab.); *<----you could also invoke the zipstate function here with zip as the arg---; if zip='99999' then do; state='99'; stab=' '; end; retain state stab; end; if naics='------' then industry=naics; else industry=compress(naics,'-/'); if naics='------' then nlevel=0; else do; nlevel= length( industry)-1; end; label nestabs='# of establishments: total' Nestabs_1_4='# of establishments: with 1-4 employees' Nestabs_5_9=': with 5-9 employees' Nestabs_10_19=': with 10-19 employees' Nestabs_20_49=': with 20-49 employees' Nestabs_50_99=': with 50-99 employees' Nestabs_100_249=': with 100-249 employees' Nestabs_250_499=': with 250-499 employees' Nestabs_500_999=': with 500-999 employees' Nestabs_1000=': with 1000+ employees' ; drop _: ; run; %mend zbpcnv2; %zbpcnv2(yr=07); %zbpcnv2(yr=08); %zbpcnv2(yr=09); proc print data=cbp.usziptots09(obs=50); by year; id zip; title "cbp.usziptots09"; run; proc print data=cbp.uszipdetails09(obs=50); by year; id zip; title "cbp.uszipdetails09"; run;