x cd /pub/data/acs2006/Tools; %let pgm=basetables_metadata; filename pgm "&pgm..sas"; /*--Read the metadata as derived from the Ch. 6 codebook Excel file that came with the summary files for acs2006. It contains info regarding tables (titles, universes, etc) as well as table cells (cell #, label info, implicit indent levels based on leading blanks, and segment # indicating the numbered CSV file from which the data cell can be read. Output from this setup includes: acs2006.basetables acs2006.basevariables BasevarLabels.sas - file with SAS label statements to use in creating sas datasets basecnvtcode.sas - file with SAS length statements and fvar/lvar parm assignments used in sas conversion setup. J. Blodgett, OSEDA, Oct., 2007 -- */ title "&pgm.: Create acs2005.basetables and acs2005.basetablevars"; libname acs2006 '/pub/data/acs2006'; libname acs (acs2006); libname temp '/pub/data/acs2006/temp'; filename csvin '/pub/data/acs2006/summary/Chapter_6_acs_2006_tables_Sum_file_shells.csv'; filename varlabs '/pub/data/acs2006/BasevarLabels.sas'; filename basecode '/pub/data/acs2006/Tools/basecnvtcode.sas'; data acs.basetables(keep=TableId TableTitle Universe Topic nlines TableType TableNo TableRSfx Segno WorkGeo) acs.basevariables(keep=tableId TableNo Topic name varlabel indentlevel cellno Segno NwithinSeg); length _TableID TableID $10 TableTitle $256 Universe $208 Topic $2 Nlines 4 TableType $3 TableNo 3 TableRSfx $1 Segno 3 WorkGeo $1; length name $16 varlabel $128 indentlevel 3 cellno 3 NwithinSeg 3; format topic $acstopc.; infile csvin dsd lrecl=512 missover firstobs=2 end=eof; length _varlabel $512; informat _varlabel $char.; input _TableID cellno _varlabel Segno ; if _TableID=:'B99011' then do; *---we have decided to skip tables starting with these imputation tables. So we emulate an end-of-cfile here ---; eof=1; go to ckeof; end; if cellno ne . and (cellno ne round(cellno)) then delete; *--get rid of the dumb X.5 lines associated with medians--; retain NWithinSeg 1; if cellno=. then do; if length(_varlabel) gt 256 then put / "Edit table title: too long " _varlabel=; if _varlabel=:'Universe:' then Universe=left(substr(_varlabel,11)); else do; if _n_ gt 1 then do; _savesegno=segno; segno=lag(segno); if topic ne '99' then output acs.basetables; *<============NO imputation tables!!!===; if segno ne _savesegno then NwithinSeg=1; else NwithinSeg+1; segno=_savesegno; end; TableID=_tableID; TableTitle=_varlabel; Universe=' '; Nlines=0; TableType=substr(tableID,1,1); Topic=substr(tableid,2,2); TableNo=substr(tableid,2,5); tablersfx=substr(tableid,7,1); if index(tabletitle,'WORKPLACE GEOGRAPHY') then workgeo='1'; else workgeo='0'; retain table: Topic Universe workgeo; end; delete; end; *---we have a detail variable input line if we get here; Nlines+1; if Nlines ne cellno then do; file log; put "***Problem with line # " _n_= nlines= cellno=; _nbad+1; if _nbad gt 50 then abort abend; delete; end; if segno ne lag(segno) and cellno gt 1 then do; put "***segment changes for this table: " _all_; end; varlabel=translate(_varlabel,",`",";'"); *--replace semicolons in labels with commas and tics with backtics---; name=trim(tableid)||'i'||left(put(cellno,4.)); indentlevel=length(varlabel)-length(left(varlabel)); *--counts leading blanks--; if indentlevel then substr(varlabel,1,indentlevel)='090909090909'x; *<---replace leading blanks with tabs--; if length(_varlabel) gt 128 then substr(varlabel,125)='...+'; *<====NOTE===; ***put '***Variable label too long: ' _N_= _varlabel= tableid= cellno= varlabel=; if topic ne '99' then output acs.basevariables; ckeof: if eof then do; output acs.basetables; stop; end; drop _: ; run; options ls=172; proc print data=acs.basetables(obs=50); title2 'acs.basetables'; by topic notsorted; id tableid; run; proc print data=acs.basevariables(obs=100); title2 'acs.basevariables'; by topic notsorted tableid notsorted segno notsorted; id cellno; run; proc freq data=acs.basetables; table tabletype topic nlines tablersfx segno workgeo; run; proc freq data=acs.basevariables; table indentlevel; run; proc sort data=acs.basetables; by tableno tableid; run; proc sort data=acs.basevariables; by tableno tableid; run; *----Generate the varlabs file. Will double as include-able sas code and online metadata-----*; data _null_; merge acs.basetables(in=intables drop=segno) acs.basevariables(in=invars) end=last; by tableno tableid; file varlabs; retain _comstart '/*' _star '*' _slash '/'; if _n_=1 then put _comstart " Variables on 2006 basetables data sets. Generated on &sysdate at &systime " (_star _slash)($1.); *----At topic breaks end the current label statement and start a new one----; if topic ne lag(topic) then put ' ;' // +1 'Label ' ; retain _dot '.'; if first.tableid then do; if length(tabletitle) le 132 then put / +1 '/* ' tableid +(-1) _dot $1. +1 tabletitle (_star _slash)($1.) ; else do; length _tt1 $145 _tt2 $132; do _i=128 to 145; *---we look for a blank character in the title starting at pos 128 and break the line in two at that loc. -; if substr(tabletitle,_i,1)=' ' then do; _tt1=substr(tabletitle,1,_i); _tt2=substr(tabletitle,_i+1); put / +1 '/* ' tableid +(-1) _dot $1. +1 _tt1 / +12 _tt2 (_star _slash)($1.) ; _i=145; end; end; end; put +1 '/* Universe: ' universe (_star _slash)($1.); end; put +3 name $char12. "='" varlabel "'" ; if last then put ';'; run; *-----Generate the basecode file with SAS code used as the heart of the conversion program----; proc sort data=acs.basevariables out=basevarsbyseg; by segno NWithinSeg ; run; data _null_; file basecode lrecl=132; if _n_=1 then do; if 0 then set acs.basetables nobs=ntables; do _tableno_=1 to ntables; set acs.basetables; by topic; file basecode; if first.topic then put / +1 'LENGTH ' @@; _t=trim(tableid)||'i'; put _t +(-1) '1 - ' _t +(-1) nlines @@; if last.topic then put '5;'; end; put /; end; set basevarsbyseg; by segno tableid notsorted; *---The generated readseg macro invocation specifies 2 parms: the segno value and then a list of the variables to be read from that segment. The latter uses single dash lists to specify all the vars from one table on the segment. Note that the input dataset is not sorted by tableid and this reflects how the data are stored on the segments--; if first.segno then do; put '%let svars' segno z3. '=%str(' @@; end; if first.tableid then put name @@; if last.tableid then if cellno > 1 then put '-' name @@; if last.segno then do; put ');' ; end; run;