x cd /pub/data/acs2005/Tools; %let pgm=read_base_tables; filename pgm "&pgm..sas"; *----Read the base table .data files for 2005 Wave 1 base tables. John Blodgett, OSEDA as part of Missouri Census Data Center contract. Aug, 2006. Some of this code was adapted from a program written by Dan Keating, Washington Post. Rev. 8/15/2006 4:12PM Modifying to run with different waves, 8-31-06. And to run with Wave 3, 9-28-06. And again on 9-28 to create the doovers code file used to reconvert tables that had a problem because one or more variable labels had a bad character within them: a double quote or semicolon. ----*; %let year=2005; %*let wave=2; %let wave=3; *<=============================NOTE!!!!!!!!!!!!!!!!!=================; %let rawdata=%str(/pub/data/acs&year/rawdata); libname acs&year "/pub/data/acs&year"; libname acs (acs&year); *<---create alias name---; libname basetabs "/pub/data/acs&year/basetabs"; *--we write the following two files in this setup---; filename invdotab "invoke_dotabs_wave&wave.sas"; *<---Note: in current (Tools) directory----; filename allsets "allsets_wave&wave.sas"; *<---Note: in current (Tools) directory----; data acs.tablevars; set acs.tablevars; table_id=tableid; run cancel; *ran it just the once; *---define the macro that will read one table file, creating a corr sas dataset-----*; %global nlines setlabel; %macro dotab(table, /* table id value. Will be lower-cased for unix. */ setout=, /* output sas dataset. Will default to basetabs.&table */ pcts=1, /* flag (value of 0 or 1) to indicate if Pct variables are to be calculated and stored. */ format=%str(comma9.), /* format code to be associated with each table item (not marg/pct s */ debug=0); %let table=%lowcase(&table); %*--assign default value for setout----; %if %nrbquote(&setout) eq %str() %then %let setout=basetabs.&table; %local utable; %let utable=%upcase(&table); data _null_; set acs.tables&wave; *<--see read_table_shells2005.sas for creation of this set; where table_id="&utable"; call symput('nlines',left(put(nlines,4.))); length _setlabel $255; _setlabel=trim(table_title); call symput('setlabel',trim(_setlabel)); put 'Processing table ' table_id table_title nlines=; run; %***put _global_; data _null_; set acs.tablevars&wave end=last; *<--see read_table_shells2005.sas for creation of this set; where table_id="&utable" ; *--this step generates the variable labels for the tables in a series of global macro variables named varlabel1 thru varlabel&nlines. Verification takes place to make sure things jive. ----; length global_varname $24; global_varname="varlabel"||left(put(line_number,4.)); _edit_stub= translate(stub, "',", '";' ); call symput(trim(global_varname),trim(_edit_stub)); if last then do; if line_number ne &nlines then do; file log; error "***logic problem*** Value of line_number on last entry for table does not match nlines global value (&nlines) **** "/ "nlines=&nlines " line_number=; abort abend; end; end; run; %***put _global_; *<================================temp diagnostic to make sure we defined the globals as expected---; filename in pipe "unzip -p &rawdata/&utable &utable..DAT "; *--each table is contained in its own .zip file in rawdata dir--; *---Starting with Wave 3 they included 2 files in each tables zip file so had to add the extra arg to specify that we just wanted the .DAT file and not the .sas7bdat file. -----; data &setout (label="&setlabel"); infile in lrecl=4096 truncover; missing N; length geoid $20 sumlev $3 State $2 Geocomp $2 Geocode $12 _tableck $12 _margin $20 default=5; input _tableck $ geoid $ @; set acs.geography(keep=geoid -- State) key=geoid; *<---see geography.sas for creation---; if _iorc_ then do; put / '***Geography key read failed for ' geoid= ' -- Observation deleted.' ; list; _nbadgeo+1; if _nbadgeo ge 20 then abort abend; delete; end; %*--This loop is the hard part---; %do j=1 %to &nlines; %*--we process the jth line of the table, creating up to 3 vars for each. The item var, the margin var and, sometimes, the pct var.---; %let vname=&table.i&j ; %let mname=Marg&table.i&j; %let pname=Pct&table.i&j; label &vname="&&varlabel&j" &mname="+/-" %if &j ne 1 %then %str( &pname="Pct" ); ; input &vname _margin @; length &mname 5; if _margin=:'***' then &mname=.; else &mname =compress(substr(_margin,4),','); %*<--edit to remove "+/-" and commas. Convert to numeric-; %if &j=1 %then %str(_tabtotal=&vname;); %if &pcts and &j ne 1 %then %do; %*--we only do this if the pcts parm was not set to 0 and if we are not processing the first line of the table. We do not calculate a pct variable for the 1st line.--; if _tabtotal then &pname=100*&vname/_tabtotal; %end; %end; format marg: comma7. Pct: 5.1; %if &format ne %str() %then %do; format &table.: &format ; %end; %if &pcts=0 %then %do; drop pct: ; %end; if _n_=1 then if _tableck ne "&utable" then do; error '*****Table Id on input file does not agree with table parm**** ' _tableck= "Table parm=&utable *****"; abort abend; end; drop _:; run; %mend dotab; /* =======================Begin comment================ *---In this TEST run we run a test after making a fix to the table title for one of our tables and after making a change to the way we assing member labels to the sas datasets. You can invoke dotab "manually" while testing. Once it is working see the commented-out step, just below---*; options mprint; %dotab(B03002); ======================End comment================== */ /* =======================Begin comment================ *---Access the tables metadata dataset which contains the names of all the tables. Use this to write the invoke_dotab_wave1.sas file which is then %include-d to do complete set of table conversions---; data _null_; set acs.tables&wave end=last; if index(table_title,'MEDIAN') then _formatparm=',format='; if index(table_title,'MEDIAN') or nlines=1 then _pctparm=',pcts=0'; file invdotab; *--This put statement writes the sas code to invoke the dotab macro for a table--*; put +3 '%dotab(' table_id _formatparm _pctparm ')' ; file allsets; *--the allsets file is a just-in-case file that should come in handy when/if we need to reference all these 300+ datasets in a sas program.--; if _n_=1 then put ' set ' @@; if mod(_n_,6)=1 then put / +3 @ ; *--start a new line after 6 dataset references--; _set='basetabs.'||lowcase(table_id); put _set +1 @@; if last then put /' ;'; run; data _null_; infile invdotab; input ; file print; put _infile_; title "invoke_dotabs_wave&wave..sas"; run; data _null_; infile allsets; input ; file print; put _infile_; title "allsets_wave&wave..sas"; run; options NOmprint source2; %include invdotab / source2; *<=====This is the big one!===================; ======================End comment================== */ data do_overs; set acs.tablevars acs.tablevars2 acs.tablevars3 end=last; by table_id; if first.table_id then doover=0; retain doover; if indexc(stub,'";') then doover=1; if last.table_id and doover then output; keep table_id wave; run; data alltables; set acs.tables acs.tables2 acs.tables3; by table_id; run; data do_overtables; *--this is a "pickoff merge" to choose tables with one of the offending characters within a label--; merge alltables(in=intabs) do_overs(in=doover); by table_id; if doover; if not intabs then put '***Logic Error*** ' _all_; run; filename doovers "invoke_dotabs_doovers.sas"; *<---Note: in current (Tools) directory----; data _null_; set do_overtables; if index(table_title,'MEDIAN') then _formatparm=',format='; if index(table_title,'MEDIAN') or nlines=1 then _pctparm=',pcts=0'; file doovers; *--This put statement writes the sas code to invoke the dotab macro for a table--*; put ' %let wave=' wave $1. ';' ; put +3 '%dotab(' table_id _formatparm _pctparm ')' ; if _N_=2 then put 'Options NOmprint;'; run; options mprint; *----redoing the conversions of tables that were messed up due to problems with variable labels that contained double quote or semicolon characters-----*; %include doovers /source2; *---delete this line outside the mcdc environment--------;