x cd /pub/data/acs2007/Tools; %let pgm=create_basetables_metadata; filename pgm "&pgm..sas"; *----We access the strngely named basetbls.merge_5_6_final dataset (from the xls file of the same name provided on the Census Bureau summary FTP server) and generate a pair of metadata sets describing the tables and variables in the detailed basetables collection. JGB, Nov, 2008. ****Logic for calculating indent_level needs to be replaced with an importing of indent levels from another source. BCE will be working on creating such a data source by running a special macro within Excel against the acs2007 tables xls file** ----*; libname acs '/pub/data/acs2007/'; %let btpath=%str(/pub/data/acs2007/basetbls); libname basetbls "&btpath"; libname user '/SAS_temp/user'; title "&pgm: Generate Variable level metadata for basetbls data sets"; /* =======================Begin comment================ *---Read the special CSV file created from the acs_2007_tables.xsl file by BCE to add an indent level field which we shall use later-; filename csvin '/pub/data/acs2007/basetbls/acs_2007_tables.csv'; data indents; infile csvin dsd lrecl=300 truncover ; length tableId $8 TableCell 4 _c1 $1 _ctablecell $4 IndentLevel 4; input tableid _ctablecell; _c1=substr(tableid,1,1); if _c1 in ('B','C') and _ctablecell ne 'Tabl'; if verify(_ctablecell,' 1234567890')=0; _subject=substr(tableid,2,2); if '00' <= _subject <= '26'; *--flush tables in 98 and 99 series here--; if tableid='B02003' or tableid=:'B12007' then delete; if index(tableID,'PR') or substr(tableid,8,1)='P' then delete; *--This is like B06004APR, where we truncated the R--; tablecell=_ctablecell; indentlevel=scan(_infile_,-1,','); drop _: ; run; proc sort data=indents nodupkeys; by tableid tablecell; run; data meta_edit1(label='Edited extract of Merge_5_6_Final data set') ; set basetbls.Merge_5_6_final(keep=tblid order position cells total title rename=(order=TableCell)) ; by tblid notsorted; length TableID $8 subject $2 tabset $5; if index(tblid,'PR') then delete; TableID=tblid; if length(tblid) > 8 then put tblid= ' ***longer than 8 characters! **********'; drop tblid; subject=substr(tableid,2,2); if '00' <= subject <= '26'; *--flush tables in 98 and 99 series here--; if tableid='B02003' or tableid=:'B12007' then delete; if subject le '07' then tabset='00_07'; else if subject='08' then tabset='08'; else if subject <='16' then tabset='09_16'; else if subject <='20' then tabset='17_20'; else if subject <='24' then tabset='21_24'; else if subject <='26' then tabset='25_26'; else do; put "***logic error*** " / _all_; abort abend; end; length NcellsInTable 3; if total ne . then NcellsInTable=total; else if cells ne ' ' then NcellsInTable=scan(cells,1); if first.Tblid then linetype=1; else if title=:'Universe:' then linetype=2; else linetype=3; drop _: cells total; retain NcellsInTable; run; proc sort data=meta_edit1 ; by tableid linetype tablecell; run; data meta_edit2; merge meta_edit1(in=in1) indents(in=in2); by tableId tablecell; if tablecell and (int(TableCell)=TableCell) and (not (in1 and in2)) then do; _nerr+1; if _nerr le 50 then put _all_; else abort abend; end; run; proc sort data=meta_edit2 out=basetbls.meta_edit nodupkeys; by tabset tableId linetype TableCell title; run; ======================End comment================== */ %macro doset(set); %let upset=%upcase(&set); /* =======================Begin comment================ *-- Extract proc-contents-based metadata from a basetbls (acs2007) data set. Here we get the variable name, label, type and length.--; proc contents data=basetbls.ustabs&set out=contents&set(keep=varnum type formatd formatl name label) noprint; run; proc sort data=contents&set; by varnum; where type=1; run; data vars&set; set contents&set; where varnum ge 10; *--ignores id variables--; length TableSeqNo 3 TableID $8 TableType $3 TableNo $5 TableRSfx $1 TableCell 3 ; label TableRSfx='Table Racial Suffix letter'; retain; length format $8; if formatd then format=trim(left(put(formatl,3.)))||'.'||trim(left(put(formatd,2.))); length _id $8 _norsfx $6; _id=scan(name,1,'i'); TableCell=scan(name,2,'i'); if _id not in: ('B', 'C') then delete; _nvar+1; if _id ne TableID then do; *--We have a new table. ---; _ntable+1; _ix=0; TableID=_id; TableSeqNo+1; _ix2=verify(_id,'BCADEFGHI'); *--look for first non-alpha, i.e where the number starts-; TableType=substr(_id,1,_ix2-1); _norsfx=substr(_id,_ix2); _l=length(_norsfx); if verify(substr(_norsfx,_l,1),'1234567890') then do; TableNo=substr(_norsfx,1,_l-1); TableRSfx=substr(_norsfx,_l,1); end; else do; TableNo=_norsfx; TableRSfx=' '; end; end; drop _: formatl formatd type; run; proc sort data=vars&set; by tableId TableCell; run; data vars&set.2; length TableSeqNo 3 TableID $8 TableName $240 Universe $140 TableType $3 TableNo $5 TableRSfx $1 TableCell 3 IndentLevel 3; merge vars&set (in=invars) basetbls.meta_edit(in=inmeta where=(tabset="&set")); by tableId TableCell; retain TableName Universe NCellsInTable ; if first.tableid then do; *---we expect to find 2 obs on the meta unmatched on the vars data set: the first with the table title and the 2nd with the table universe--; _nobsthistable=1; TableName=propcase(title); return; end; _nobsthistable+1; if _nobsthistable=2 then do; if title=:'Universe:' then Universe=left(substr(title,11)); else do; put "***Universe not found for 2nd obs within this table group*** " tableid= title= ; _nerr+1; end; return; end; if not invars and (tablecell ne int(tablecell)) then do; length Subtitle $40; subtitle=title; return; retain subtitle; end; if not invars and inmeta then do; put "****Non-match here: " tableid= tablecell= invars= inmeta= ; _nerr+1; *** if _nerr ge 50 then abort abend; end; label=title; *---the metadata label supercedes the label assigned on the data set. But they should be the same---; *--Determine indentation level by looking for colon (:) characters at the end of variable labels. They indicate that subsequent items are to indent 1 level---; if indentlevel then label=repeat(' ',indentlevel-1)||label; *--Add 2 leading blanks per level--; format label $char.; output; Subtitle=' '; drop _: position title ; run; proc sort data=vars&set.2 out=basetbls.Variables&upset(label="Metadata describing table variables on &upset dataset" index=(TableID) compress=yes); by TableNo TableType TableRSfx TableCell; run; proc datasets library=basetbls nolist; modify Variables&upset; index create tableids=(TableNo TableType TableRSfx); quit; options ls=150; proc print data=basetbls.Variables&upset(where=(tablecell=1)); title3 "Listing of sf3.Variables&upset: Table Level Data"; by tabset subject; id TableId; var TableSeqNo TableType TableNo TableRSfx TableName Universe Ncellsintable ; format TableName $60.; run; proc print data=basetbls.Variables&upset(obs=200); title3 "Listing of sf3.Variables&upset: Variable Level Data - (first 200 obs)"; by tableID notsorted; id tableID; var tablecell name indentlevel label varnum ; ***format subtitle; format label $char72.; run; ======================End comment================== */ *---Finally, we generate the custom txt file that documents the variables within the tables, 1 file per set---; filename varlabs "&btpath/Varlabs/&set.labels.txt"; data _null_; set basetbls.Variables&set end=last; by tableID notsorted; file varlabs; if _n_=1 then put 'LABEL '; if first.tableid then put / '/* ' TableID +(-1)'. ' TableName ' */' / '/* Universe: ' universe ' */' /; length qlabel $260; qlabel=quote(trim(label)); put @5 name "=" qlabel; if last then put ' ;'; run; filename varlabs clear; %mend doset; options mprint; %doset(00_07) %doset(08) %doset(09_16) %doset(17_20) %doset(21_24) %doset(25_26)