x cd /home/riceg/ACS_SAS_processing; %let pgm=create_basetbls_meta; filename pgm "&pgm..sas"; /* We combine here what used to be (pre-2014) 2 modules - 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. For 2016 we add a new wrinkle. We read the acs metadata tables from the Washington State Data Ctr to get info regarding whether a table is available for the BG level and for the 5yr data. (The 5yr indicator was not available when running in October. See the acs5yrflag global variable. When that data becomes available for the 2016 cycle we need to download the new meta files, change the value of that flag to 1, and then rerun this setup. In the meantime we assign the Avail5yr based in its value for last cycle (2014). See the xlsx file at https://www.ofm.wa.gov/sites/default/files/public/dataresearch/pop/ACS/wa_acs_detailed_table_availability.xlsx. We download that Excel workbook and save the 2 sheets as csv files in the acs subdirectory. */ %include '/pub/sas/autoexec.sas'; %let year=2017; %let yr=%substr(&year,3,2) ; %let datadir=%str(/pub/data/acs&year./base_tables_5yr); %let btpath=&datadir; %let yearm1=%eval(&year-1); %let bgflag=; %let bgflag=AvailBGs; *-- set the parm equal to the variable that tells us (Y/N) whether the table is avail at bg level ; %let btabmax=28; *-- set to 27 or 28 depending on whether there is topic 28 data; libname btabs&yr "&datadir"; libname btabs (btabs&yr); libname basetbls (btabs&yr); libname user '/home/riceg/ACS_SAS_processing/temp'; filename txtin "&datadir./ACS_5yr_Seq_Table_Number_Lookup.txt"; filename text '/pub/sas/formats/Sitabns.sas'; /* The files from the Washington State Ofice of Financial Management are re-downloaded for each data cycle. These were rewritten late 2018. It appears the BG restriction info is there now. */ filename wameta1 '/pub/data/acs/wa_acs_detailed_table_availability.csv'; *-- replace when later version available ; filename wameta2 '/pub/data/acs/wa_acs_detailed_table_availability_geoglimits.csv'; *-- this is a sheet within the xlsx workbook ; %let acs5yrflag=1; *-- set to 1 when the wameta1 file has info for latest year regarding available for 5yr data ; data btabs.seqtab1(label="&year 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 index(tblid, 'PR')=0; 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_&btabmax"; *-- for earlier cycles 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='RaceCat suffix' 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 listing close; options mprint; *========= Now we read the 2 wa_meta files to get info re table availability for 5yr and bg =========; data wameta1; infile wameta1 firstobs=2 dsd termstr=crlf lrecl=1024; /* During 4th qtr of processing year there will not be a column indicating 5yr data for latest year. That column goes after Universe in the input file once it becomes available. Then we need to rerun this. In the meantime we use availability in the PREVIOUS year to predict availability in the current acs vintage year. */ length _subject $40 TableID $12 _title $100 ncellsck 8 _universe $100 %macro shortone(); %if &acs5yrflag %then %str( Avail5yr $1 ); _acs&year.1yr $1 _acs&yearm1.5yr $1; input _subject -- _acs&yearm1.5yr; ***if index('ABCDEFGHI',trim(substr(tableid,length(tableid),1))) then delete; *--ignore alpha-suffix tables--; %if &acs5yrflag=0 %then %do; Avail5yr=_acs&yearm1.5yr; %end; if Avail5yr=' ' then avail5yr='N'; %mend shortone; %shortone ; length bc $1 Tableno $5 tblsfx $2; bc=substr(tableid,1,1); tableno=substr(tableid,2,5); tblsfx=substr(tableid,7); keep bc tableno tblsfx ncellsck Avail5yr; drop _: ; run; proc sort; by tableno bc tblsfx; run; data wameta2; infile wameta2 firstobs=2 dsd termstr=crlf lrecl=1024; length _subject $40 TableID $12 _title $100 ncellsck2 8 _noblkgrps $24 tblsfx $2; input _subject -- _noblkgrps; length AvailBGs $1 RestrictedGeo $24; if _noblkgrps='No Blockgroups' then AvailBGs='N'; else AvailBGs='Y'; *-- we assing it a value in next step ; if index(_noblkgrps,'Only') then do; if _noblkgrps=:'Place of Work' then RestrictedGeo='POW only'; else if _noblkgrps=:'Migration' then RestrictedGeo='Migr only'; else RestrictedGeo=_noblkgrps; end; else RestrictedGeo=' '; length bc $1 Tableno $5; bc=substr(tableid,1,1); tableno=substr(tableid,2,5); tblsfx=substr(tableid,7); keep bc tableno tblsfx ncellsck2 AvailBGs RestrictedGeo; drop _: ; run; proc sort; by tableno bc tblsfx; run; data btabs.tables1(label='1-year Table Summary Data'); merge btabs.seqtab1(in=inseqtab where=(type in ('TITLE','UNIVERSE') and (tblsfx ne 'PR'))) wameta1(in=inmeta1) wameta2(in=inmeta2); **where type in ('TITLE','UNIVERSE') ; by tableno bc tblsfx; retain; if inseqtab; if not (inmeta1 and inmeta2) then do; put /'Missing metadata for this table: ' tblid= tblsfx= tableno= bc= inmeta1= inmeta2=; _nbad+1; * if _nbad ge 75 then abort abend; end; else if first.tblsfx and (tblsfx ne 'PR') and not (cells=ncellsck and cells=ncellsck2) then do; put /"Table cell count check(s) fail: " tblid= cells= ncellsck= ncellsck2= ; _nbad2+1; length NcellsLastYr 4; NcellsLastYr=ncellsck; *-- this appears to be why these do not match ; * if _nbad2 ge 75 then abort abend; end; length TableTitle $200 Universe $120 Ncells 4 AlphaSfxs $10; length ncellsAlpha 4; label ncellsAlpha='# cells in alpha-suffix tables'; if first.bc then do; *-- Start table break processing ; TableID=tblid; if tblsfx=' ' then do; Alphasfxs='_'; _avail5yrtot=avail5yr; _availBGsTot=availBGs; nsfxs=0; end; else do; TableID=tblidbase; _avail5yrTot='N'; _availbgTot='N'; Alphasfxs=' '; _ix=index(title,'(WHITE'); if _ix then title=substr(title,1,_ix-1); nsfx=1; end; TableTitle=translate(title,'`',"'"); *-- Replace quotes with pseduo-quotes ; Ncells=cells; ncellsAlpha=.; Universe=' '; end; *-- first.bc processing ; if not first.bc then do; 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; end; *-- not first.bc processing ; *-----end of table group processing-------------------*; if last.bc then do; if nsfxs=0 then alphasfxs=' '; if restrictedGeo ne ' ' then TableTitle=trim(TableTitle)||'('||trim(restrictedGeo)||')'; if nsfxs then do; *-- we have to assign different availability flags for total and alpha-sfx tabs ; if Avail5yr='N' then Avail5yrAlpha='N'; else Avail5yrAlpha='Y'; if AvailBGs='N' then AvailBGsAlpha='N'; else AvailBGsAlpha='Y'; end; Avail5yr=_Avail5yrtot; Availbgs=_AvailbgsTot; *-- these were assigned at first.bc processing ; if avail5yr='N' then Availbgs='N'; if avail5yralpha='N' then availbgsslph='N'; output; NcellsLastYr=.; end; end; label Alphasfxs='Alpha suffixes (A,B,..,I) available for this table'; label nsfxs='# suffix versions of table' TableID='Table ID wo Sfx' Ncells='# cells in table'; keep TableID TableTitle Universe Ncells NCellsAlpha Topic TopicGrp BC TableNo nsfxs Alphasfxs; keep Avail5yr Availbgs NcellsLastyr; *RestrictedGeo; keep Avail5yrAlpha AvailbgsAlpha; run; ods html file="&datadir./basetables_meta.html" style=analysis; proc print data=btabs.tables1; by TopicGrp Topic; id tableid; format tabletitle $130.; title "btabs&yr..tables1"; footnote2 h=.5 "Some data here based on https://www.ofm.wa.gov/sites/default/files/public/dataresearch/pop/ACS/wa_acs_detailed_table_availability.xlsx"; run; ods html close; *------ 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 Avail:) 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; *-- Adding a "b" suffix to the variables data sets because we already have datasets with those name that we got from GR. Do we replace those? They are needed by dexter to turn tables selects into variable selects so we need to be careful. ; data basetbls.Variables&set.B(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=$5 label='5-digit Table #' TableRSfx length=$2 TableCell length= 4 IndentLevel length= 4 Name length=$11 Label length=$100 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' Avail5yr length=$1 label='Available in 5yr tables?' AvailBGs length=$1 label='Available for BG level data?'; ; keep TableSeqNo--ncellsAlpha Avail:; *<-----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; *-- We assign availabilty flags separately for alpha-sfx table cells ; if TableRsfx ne ' ' then do; Avail5yr=Avail5yrAlpha; AvailBGs=AvailBGsAlpha; end; run; proc sort data=basetbls.variables&set.B ; by TableNo TableType TableRSfx TableCell; run; proc datasets library=basetbls nolist; modify Variables&set.B; index create tableid; quit; options ls=150; proc print data=basetbls.Variables&set.B(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 Avail:; format TableName $60.; run; proc print data=basetbls.Variables&set.B(obs=200); title3 "Listing of basetbls.Variables&set.B: 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.B end=last; by tableno tabletype; file varlabs; if _n_=1 then put ' /' '* Generated at' " &systime on &sysdate by &pgm..sas for ACS vintage &year ." '*' '/' / 'LABEL '; if first.tabletype then do; put / '/* ' TableID +(-1)'. ' TableName '[' ncellsintable +(-1) '] */' / '/* Universe: ' universe ' */'; *if bgflag then put @3 '/* Available at Block Group Level on 5yr Data Sets */' / ; if avail5yr='N' or availBGs='N' then do; put '/* *** Important Note ***: This table is NOT available for ' @; if avail5yr='N' then put '5-year data at any level */'; else if availBGs='N' then put 'block group level data */'; end; if nsfxs then do; put @3 " /* *** This table is repeated for the following Race/Hispanic 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; 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; %doset(00_07) ; %doset(08); %doset(09_16); %doset(17_20); %doset(21_24); %doset(25_&btabmax); /*=========================Comment out===================================== *-- 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_07b basetbls.variables08b basetbls.variables09_16b basetbls.variables17_20b basetbls.variables21_24b basetbls.variables25_&btabmax.b; 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/sasctlgs/v9'; %genfmat(fmatname=itabns, sasin=format2, argvars=tableid, arglen=9, library=formats, labelvar=cellcount,labellen=3, other=0, savedoc=1, runproc=1); ===========================End commented out================================ */ %include sascode(notify);