x cd /pub/data/acs2014/Tools; %let pgm=create_basetbls_meta; filename pgm "&pgm..sas"; *---We combine here what used to be 2 modiuls - cnvt_table_number_lookup and create_basetables_varlabsYY. We begin by reading the "Sequence Table" txt file provided as part of the tech doc by the Bureau - http://www2.census.gov/programs-surveys/acs/summary_file/2014/documentation/user_tools/ACS_1yr_Seq_Table_Number_Lookup.txt jgb, 9/15. *<===*Check to make sure the $itabns format code works. Depends on length/type of var ncellsintable on input*** * ---*; %let datadir=%str(/pub/data/acs2014/basetbls); %let btpath=&datadir; *--we used this name in the old create_basetables module---; %let year=2014; %let yr=%substr(&year,3,2) ; %let yearm1=%eval(&year-1); %let bgflag=; %let btabmax=28; *---set to 27 or 28 depending on whether there is topic 28 data ---; libname btabs&yr "&datadir"; libname btabs (btabs&yr); *---create an alias----; libname basetbls (btabs&yr); *---create another alias----; filename txtin "&datadir./ACS_1yr_Seq_Table_Number_Lookup.txt"; filename text '/pub/sas/formats/Sitabns.sas'; /*<=========================Comment out===================================== data btabs.seqtab1(label="2014 vintage 1-year tables metadata" compress=yes); infile txtin lrecl=1000 dsd missover firstobs=2; length fileid $5 tblid $12 SeqNum 4 position 4 StartPos 4 _cells $10 CellsinSeq 4 title $200 subject_area $100; length Cells 4 Type $8; *--we assign this code to help us ID the type of metadata contained in the line/obs-; length Topic $2 TopicGrp $5 BC $1 tblidbase $6 TableNo $5 TblSfx $2 IndentLvl 3; retain Topic--TblSfx IndentLvl; input fileid--subject_area; if fileid='ACSSF'; if tblid ne lag(tblid) then do; IndentLvl=0; _lastLineEnded='-'; Topic=substr(tblid,2,2); TableNo=substr(tblid,2,5); label TableNo='Numeric part of tblid'; if Topic le '07' then TopicGrp='00_07'; else if Topic='08' then TopicGrp='08'; else if Topic le '16' then TopicGrp='09_16'; else if Topic le '20' then TopicGrp='17_20'; else if Topic le '24' then TopicGrp='21_24'; else TopicGrp='25_28'; *<----last cycle this was 25_27 ----; BC=substr(tblid,1,1); *--B or C table--; tblidbase=substr(tblid,1,6); tblsfx=substr(tblid,7); end; if index(_cells,'CELL') then do; cells=scan(_cells,1); type='TITLE'; end; else if title=:'Universe' then type='UNIVERSE'; else if position then do; type='DATALINE'; end; else type='OTHER'; retain _lastlineEnded ' ' _thislineEnds ' '; if type='DATALINE' then do; _thislineEnds=substr(title,length(title),1); if _lastlineEnded=':' then IndentLvl+1; if _thislineEnds=':' and _lastLineEnded ne ':' and IndentLvl>0 then IndentLvl=IndentLvl-1; _lastlineEnded=_thislineEnds; end; drop fileid _: _: ; label seqnum='Input file #' CellsinSeq='# cells in seqnum' position='Cell #' StartPos='Position on input file' ; label TableNo='5-digit Table #' BC='B or C (table type)' tblidbase='Table ID wo Suffix' tblsfx='Tab R suffix or PR' IndentLvl='Indent Level for display' ; run; proc sort; by tableno bc tblsfx position; run; proc print data=btabs.seqtab1(obs=100); by tblid; id tblid; run; ods output close; ods html file="&pgm..html" style=minimal; data btabs.tables1(label='1-year Table Summary Data'); set btabs.seqtab1; where type in ('TITLE','UNIVERSE') ; by tableno bc tblsfx; retain; length TableTitle $200 Universe $100 Ncells 4 AlphaSfxs $10; length ncellsAlpha 4; label ncellsAlpha='# cells in alpha-suffix tables'; if first.bc then do; TableID=tblid; if tblsfx=' ' then Alphasfxs='_'; else do; TableID=tblidbase; Alphasfxs=' '; _ix=index(title,'(WHITE'); if _ix then title=substr(title,1,_ix-1); end; TableTitle=translate(title,'`',"'"); *--Replace quotes with pseduo-quotes--; nsfxs=0; Ncells=cells; ncellsAlpha=.; Universe=' '; end; else if type='TITLE' and tblsfx ne ' ' then do; ncellsAlpha=cells; *--added 11-25-14 after we discovered that the alpha-suffix tables could be shorter than non-alpha table-; end; else if type='UNIVERSE' then do; if Universe=' ' then Universe=title; if tblsfx ne ' ' then do; nsfxs+1; Alphasfxs=trimn(alphasfxs)||tblsfx; end; if last.bc then do; if nsfxs=0 then alphasfxs=' '; output; end; end; label Alphasfxs='Alpha suffixes (A,B,..,I, PR) available for this table'; label nsfxs='# suffix versions of table' TableID='Table ID wo Sfx' Ncells='# cell in table'; keep TableID TableTitle Universe Ncells NCellsAlpha Topic TopicGrp BC TableNo nsfxs Alphasfxs; run; proc print data=btabs.tables1; by TopicGrp Topic; id tableid; title "btabs&yr..tables1"; run; ods html close; ===========================End commented out================================ */ *--------------------------And now generate the Varlabs modules (previously a separate module)------------------------*; %macro doset(set); %let wherec=%str(TopicGrp="&set"); proc sort data=basetbls.tables1(where=( &wherec ) keep=topicgrp bc tableno tabletitle universe alphasfxs nsfxs ncells ncellsAlpha) out=tables1&set ; by tableno bc ; run; proc sort data=basetbls.seqtab1(where=(&wherec and position) ) out=cells1&set; by tableno bc tblsfx position; run; data basetbls.Variables&set(label="Metadata describing table variables on a &set dataset" compress=No); *---Attrib statement(s) (originally) generated by genattr macro---; Attrib TableSeqNo length= 4 TableID length=$8 TableName length=$123 Universe length=$120 TableType length=$1 TableNo length=$8 label='5-digit Table #' TableRSfx length=$2 TableCell length= 4 IndentLevel length= 4 Name length=$11 Label length=$64 format=$CHAR. informat=$CHAR. Subject length=$2 TabSet length=$5 NcellsInTable length=$4 nsfxs length= 4 label='# suffix versions of table' AlphaSfxs length=$10 label='Alpha suffixes (A,B,..,I, PR) available for this table' ncellsAlpha length=4 label='# cells in alpha-sfx tables'; ; keep TableSeqNo--ncellsAlpha; *<-----Note----; * length TableSeqNo 4 TableID $8 TableType $1 Tableno $5 TableRSfx $1 Bgflag 3; * The 1s in these set names refer to the fact that these are 1yr tables as opposed to 3 or 5 yr tables--; merge cells1&set (in=invars ) tables1&set (in=intabs ); by tableno bc; if invars; if position=1 then TableSeqNo+1; TableId=tblid; TableName=tableTitle; if Universe=:'Universe: ' then universe=left(substr(universe, 11)); else Universe=Universe; TableType=BC; Tableno=Tableno; TableRSfx=TblSfx; TableCell=position; IndentLevel=IndentLvl; Name=trim(TableID)||'i'||left(put(TableCell,3.)); if indentlvl then label=repeat(' ',indentlvl-1)|| title; else label=title; *<--insert 2 leading blanks per level of indent--; Subject=Topic; TabSet=TopicGrp; if Tblsfx=' ' then NCellsInTable=ncells; else NCellsInTable=ncellsAlpha; *<---new, 11-25-14---; run; proc sort data=basetbls.variables&set ; by TableNo TableType TableRSfx TableCell; run; proc datasets library=basetbls nolist; modify Variables&set; index create tableid; quit; options ls=150; proc print data=basetbls.Variables&set(where=(tablecell=1)); title3 "Listing of basetbls.Variables&set: Table Level Data"; by tabset subject notsorted; id TableId; var TableSeqNo TableType TableNo TableRSfx TableName Universe Ncellsintable ncellsAlpha &bgflag nsfxs; format TableName $60.; run; proc print data=basetbls.Variables&set(obs=200); title3 "Listing of basetbls.Variables&set: Variable Level Data - (first 200 obs)"; by tableID notsorted; id tableID; var tablecell name indentlevel label ; ***format subtitle; format label $char72.; run; *---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 tableno tabletype; file varlabs; if _n_=1 then put " /* Generated at &systime on &sysdate */ " / 'LABEL '; if first.tabletype then do; put / '/* ' TableID +(-1)'. ' TableName ' */' / '/* Universe: ' universe ' */'; ***if bgflag then put @3 '/* Available at Block Group Level on 5yr Data Sets */' / ; if nsfxs then do; put @3 " /* *** This table is repeated for the following Race/Hispanic/PR suffixes: " AlphaSfxs "*** */"; if ncellsAlpha ne ncellsInTable then put @5 "/* the alpha-suffix versions of this table has fewer cells (" ncellsAlpha ") */" / @5 "/* The A-suffix table cell labels are shown at the end of the cells for this table. */" ; _ncellsInBaseTable=ncellsintable; retain _ncellsinBaseTable; end; _writethisone=1; _firstsfx=tableRSfx; retain _firstsfx; end; length qlabel $260; ****replacing with stmt below 11-25-14. if TableRSfx=_firstsfx then do; if TableRSfx=_firstsfx or (TableRsfx='A' and (ncellsAlpha lt _ncellsInBaseTable)) then do; qlabel=quote(trim(label)); put @5 name @16 "=" qlabel ; end; if last then put ' ;'; run; filename varlabs clear; %mend doset; options mprint; /* ========================begin comment==================================== %doset(00_07) ; %doset(08) %doset(09_16) %doset(17_20) %doset(21_24) %doset(25_28) ==========================end comment====================================== */ *---Rerunning this 2-6-16 to fix problem caused by change in input for variable ncellsIndata -------*; *---generate the $itabns format code that is used to look up the number of cells in tables with I suffixes. We use this format in dexter. Coded 9-1-10, jgb---; data format; set basetbls.variables00_07 basetbls.variables08 basetbls.variables09_16 basetbls.variables17_20 basetbls.variables21_24 basetbls.variables25_&btabmax; where tablersfx='I'; keep tableid ncellsintable; proc sort; by tableid; data format2; set format; by tableid; if last.tableid; length cellcount $3; *---ncellsintable used to be (??) numeric but is now a character string. in the 2014 cycle it was $4--; if length(ncellsintable)=4 then cellcount=substr(ncellsintable,2,3); else if length(ncellsintable)=3 then cellcount=ncellsintable; ** else cellcount=put(ncellsintable,3.); *--if it is numeric. Worked in earlier cycles--; tableid=substr(tableid,1,length(tableid)-1); keep tableid cellcount; run; proc print data=format2(obs=50); title "format data"; run; libname formats '/pub/sas/catalogs'; %genfmat(fmatname=itabns, sasin=format2, argvars=tableid, arglen=9, library=formats, labelvar=cellcount,labellen=3, other=0, savedoc=1, runproc=1);