%MACRO zbpcnv2( revdate=05/21/2016, /* *<=========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. */ unzipopts=pqa, /* options specified when we unzip the downloaded .zip files. The "a" varies by year. 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_; %if &yr ge 14 %then %let unzipopts=pq; %*---drop the ascii spec for 2014 at least--; x cd &datadir; filename zbptots pipe "unzip -&unzipopts zbp&yr.totals.zip" ; data cbp.usziptots&yr(label="ZIP Bus Patterns Totals, &year"); infile zbptots firstobs=2 dsd obs=&obsin lrecl=512; **termstr=crlf did not apply to tots file for 2012 data but DID for details!??--; 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 %If &yr ge 13 %then %do; ZIPcityname :$40. ZIPstab :$2. ZIPCtyName :$40. %end; ; if _n_=1 then do; put '****list first record on zbptots***'; list; end; _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; *--termstr=crlf removed because we used the -a option on unzip--; 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_=1 then do; put '****list first record on zbpdetl***'; list; end; 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;