x cd /pub/data/sf12010/Tools; %let pgm=read_metadata; filename pgm "&pgm..sas"; *---Read the data dictionary file (saved as a tab-delimited file from the xls file provided by the Census Bureau). We generate several outputs: 1. A series of SAS data sets comparable to one created for sf32000 filetype: variablesph, variablesphct, variablephctr and variablespco (new for this filetype). 2. A set of txt files in the Varlabs subdirectory to serve as data dictionay files for SAS users, and which double as SAS code file to assign labels to the cell variables. 3. cnvtcode.sas containing code that will be incorportated into the cnvtsf1.sas setup. Declares variable intervals and assigns the first and last variable parms used to read the 47 data files ("segments"). jgb, OSEDA for Missouri Census Data Center. June, 2011. Rerunning 11-7-11 after finding that table P12A (for example) was getting put in variablesphctr instead of variablesph. ----*; %let path=%str(/pub/data/sf12010); filename txtin "&path/sf1tables.txt"; libname sf12010 "&path"; libname sf1 (sf12010); *<---alternate shorter name---; data _null_; file "&path/Varlabs/PHlabels.txt" ; put ' Label '; file "&path/Varlabs/PHCTlabels.txt" ; put ' Label '; file "&path/Varlabs/PHCTRlabels.txt" ; put ' Label '; file "&path/Varlabs/PCOlabels.txt" ; put ' Label '; stop; run; data sf1.variables(label='Metadata descbribing all data cell variables in these table data sets') sf1.variablesph sf1.variablesphct sf1.variablesphctr sf1.variablespco; *---Attrib statement(s) generated by genattr macro using sf32000.variables as template set.---; Attrib TableSeqNo length= 4 TableID length=$8 TableName length=$240 Universe length=$140 TableType length=$3 TableNo length= 3 TableRSfx length=$1 NCellsinTable length= 8 NAME length=$32 label='Variable Name' LABEL length=$250 label='Variable Label' format length=$8 IndentLevel length= 3 TableCell length= 3; Retain TableSeqNo -- TableRSfx NCellsInTable format; attrib Segment length=3 label='Input segment'; retain _tab '09'x _dquote '"'; retain _varlabs "&path/Varlabs/TTTTTlabels.txt"; length _line _firstline $255 _lineout $255; retain _slash '/' _star '*' _beginq _endq '***'; _beginq=_slash||_star; _endq=' '||_star||_slash; link readline; if _line=:'File' then do; *--------We have a line telling us that a NEW SEGMENT is beginning----------; *length Segment 8; Segment=substr(_line,6,2); retain Segment; NewSegment=1; retain NewSegment; if Segment <=14 then TableType='P'; else if Segment <=39 then TableType='PCT'; else if Segment <=41 then TableType='PCO'; else if Segment <=46 then TableType='H'; else tabletype='HCT'; file log; put / _line / _inlineno= Segment= TableType=; if TableType ne lag(TableType) then do; length _ttype $5; *--we assign _varlabs here but we need to edit it if we have a phctr table later--; if tabletype in ('PCT','HCT') then _ttype='PHCT'; else if tabletype in ('P','H') then _ttype='PH'; else _ttype=tabletype; *--which at this point should be PCO --; _varlabs="&path./Varlabs/"||trim(_ttype)||"labels.txt"; put '**New tabletye detected**** ' tabletype= _ttype= _varlabs= ; file varlabs mod filevar=_varlabs; *** put 'Label '; end; delete; retain Segment tabletype; end; retain _varlabs; length _word1 $8; _word1=scan(_line,1,_tab); *** _word1EqualsTableType= (_word1=: trim(tabletype)); *** _lastCharWord1=substr(_word1,length(_word1),1); *** if _n_ le 50 then put _word1= _word1EqualsTableType= _lastCharWord1=; if _word1=: trim(tabletype) and substr(_word1,length(_word1),1)='.' then do; *-----------We think we have a TABLE TITLE line, which should be followed by a universe line--------; TableID=substr(_word1,1,length(_word1)-1); put tableId=; TableSeqNo+1; label TableSeqNo='Table Seq #'; TableName=left( compress( substr(_line,length(_word1)+1) ,_tab) ); NCellsinTable=scan(tablename,-2,'[]'); _ix=index(_line,'expressed decimals'); if _ix=0 then format=' '; else do; length _ndp $1; _ndp=substr(_line,_ix-2,1); format='5.'||_ndp; substr(_line,_ix)=' '; end; length _lastchar $1; _lastchar=substr(tableid,length(tableid),1); if index('ABCDEFGHIJKLMNO',_lastchar) then do; *--the table has an alpha suffix indicating a race / origin subcategory---; TableRsfx=_lastchar; end; else TableRsfx=' '; _iphct=index(_varlabs,'PHCT'); if _iphct then do; *--we segregate labels for those pct and hct tables with an alpha suffix since we store in separate data set-; if TableRsfx=' ' then substr(_varlabs,_iphct)='PHCTlabels.txt'; else substr(_varlabs,_iphct)='PHCTRlabels.txt'; end; TableNo=compress(tableid,'PCTHCOABDEFGHIJKLMNO'); length _varnameTablepart $10; retain _varnameTablepart; _varnameTablepart=trim(tabletype)||trim(left(put(tableno,4.)))||trimn(TableRsfx); TableCell=0; ***** if tableseqno le 5 then put / _all_; file varlabs mod filevar=_varlabs lrecl=132; _lineout=_beginq||trim(compress(_line,_tab))||_endq; put / +1 _lineout; *--and now read the universe line and display it on varlabs--; link readline; _line=compress(_line,_tab); if left(_line)=:'Universe:' then do; file varlabs mod filevar=_varlabs lrecl=132; _lineout=_beginq||trim(_line)||_endq; put +1 _lineout; Universe=substr(_line,index(_line,':')+2); end; else do; file log; put "ERROR: Expected Universe line not found for " TableId= _inlineno=; end; return; end; if scan(_line,-2,_tab)='9' then do; *--------we appear to have a DETAILED DATA CELL descriptor line---------; _segck=scan(_line,-3,_tab); if Segment ne _segck then do; file log; put "ERROR: Unexpected segment code. While reading file " Segment " we find " _segck= ; _nerr+1; if _nerr gt 100 then abort abend; delete; end; *---parse the detail line to get the variable name, label and indentation level---; _i=1; _leadtabs=' '; do while(substr(_line,_i,1)=_tab); _i+1; _leadtabs=trim(_leadtabs)||'__'; end; IndentLevel=_i-2; *<--because we have an extra leading tab even when it is level 0 and because we increment i one past the number counted in the loop---; length _bureauName $10; _bureauName=scan(_line,-4,_tab); TableCell+1; if substr(_bureauname,length(_bureauname)-2) ne put(mod(TableCell,1000),z3.) then do; file log; put '***Expected table cell not found in detailed data cell line: ' TableCell= _line= _bureauname= ; _nerr+1; if _nerr gt 50 then stop; ******* abort abend; end; Name=trim(_varnameTablePart)||'i'||left(put(TableCell,4.)); *--Edit the label, replacing the leading tabs with blanks (2 per indent level) and removing any trailing tabs--; LABEL= substr(_line,2,index(_line,trim(_bureauName))-2); label=tranwrd(label,'Pacific Isl','Pacific Isl'); label=trimn(_leadtabs)||substr(label,IndentLevel+1); *--replace leadings tabs with blanks--; label=compress(label,_tab); *--and remove any trailing tabs--; label=tranwrd(label,'__',' '); _nvarout+1; *****if _nvarout le 25 then put _nvarout= TableId= TableName= tablecell= label= name= _bureauname=; output sf1.variables; if TableRsfx ne ' ' and TableType in ('PCT','HCT') then output sf1.variablesphctr; else select(tabletype); when('P','H') output sf1.variablesph; when('PCT','HCT') output sf1.variablesphct; when('PCO') output sf1.variablespco; otherwise put '***Error: unexpected TableType: ' tabletype=; end; NewSegment=0; *---Gets set to 1 when we first encounter a new file (aka "segment")---; file varlabs mod filevar=_varlabs lrecl=132; _lineout= trim(name)||'="'||trim(label)||'"'; put @3 _lineout; end; *----detailed data cell processing----; ELSE delete; return; readline: infile txtin lrecl=255 dsd dlm='09'x missover; *** length=_Lenline; _looking_for_continue=0; r1: input; _inlineno+1; if index(_infile_,'NOTE: ') or substr(_infile_,3,5)='NOTE ' then goto r1; *--ignore it---; _Lenline=length(_infile_); *--because there were lines that ended with trailing blanks!----; _line=compress(_infile_,'"'); *---remove the double quotes which are sometimes used to enclose titles and labels--; if _Lenline=(length(_line)+1) then do; *--the line has an unmatched double quote. We assume it is continued or a continuation...--; if _looking_for_continue then _line=trim(_firstline)||' '||_line; else do; _firstline=_line; _looking_for_continue=1; **** if 181 <= _inlineno <=183 then put / _inlineno= _line= _firstline= _looking_for_continue=; *---temp diagnostic---; goto r1; end; end; else if _looking_for_continue then do; file log; put "*** Error: Looking for continuation line with unmatched quote but not found: " _firstline= / _line= / _inlineno=; end; return; drop _: ; run; data _null_; file "&path/Varlabs/PHlabels.txt" mod; put ';'; file "&path/Varlabs/PHCTlabels.txt" mod; put ';'; file "&path/Varlabs/PHCTRlabels.txt" mod; put ';'; file "&path/Varlabs/PCOlabels.txt" mod; put ';'; stop; run; *---and now generate the sas code that we shall incorporate into the sf1cnvt.sas module. It is the part where we "declare" all the table cell variables and then specify the &fvar and &lvar macro variables used in the macro loop that reads all the CSV files ---*; data _null_; set sf1.variables end=_last; by segment tableID notsorted; filename code "&path/Tools/temp_generated_cnvtcode.sas"; file code column=_c; if first.segment then do; put @3 'length ' @; fvar1=name; retain fvar1; end; if first.tableid then put name @; if last.tableid and ncellsintable > 1 then put +(-1)'-' name @; if last.segment then put '5;'; if _c >100 then put / @5 ' ' @; if last.segment then do; length _seg $2; if segment < 10 then _seg=put(segment,1.); else _seg=put(segment,2.); put @3 '%let fvar' _seg $char. '=' fvar1 '; %let lvar' _seg $char. '=' name ';' / ; end; run; data tables; set sf1.variables(where=(tablecell=1) keep=tabletype tableid tablersfx ncellsintable name tablecell tableseqno); if tabletype in ('P','H') then tgroup='PH '; else if tabletype in ('PCT','HCT') then if tablersfx=' ' then tgroup='PHCT '; else tgroup='PHCTR'; else if tabletype ='PCO' then tgroup='PCO'; else put '**whoops###' tabletype=; run; proc sort; by tgroup tableseqno; run; *----the little bit of sas code generated here will be included in the cnvtsf1.sas module--; data _null_; set; by tgroup; file 'tablevars.sas'; length _line $64; if first.tgroup then do; _line= ' %let ' ||trim( tgroup)||'vars=' ; put / _line @; n=0; end; _line= trim(name)||'-'||trim(tableid)||'i'||left(put(ncellsintable,4.)); put +1 _line @; n+1; if n=6 and last.tgroup=0 then do; n=0; put ; *--just 6 variable intervals per line---; end; if last.tgroup then put ';'; *---ends the %let stmt and the line---; run;