x cd /pub/data/stf904/Tools; %let pgm=metadata05; filename pgm "&pgm..sas"; *---New and improved metadata (files and datasets) being created in 2005. Supercedes earlier efforts at creating these files and sets. Inputs include the Census Bureau data dictionary ascii file and sas labels code from ICPSR. Outputs include SAS datasets stf904. variables / tables and files (in stf904/Varlabes) palabels.txt, pblabels.txt, halabels.txt and hblabels.txt . There will also be the alabels.sas and blabels.sas files in Tools that will be used to apply the labels to the SAS datasets. jgb, OSEDA, under a contract with the Missouri Census Data Center, begin work 12-2005. ---*; title "&pgm: Generate metadata for 1990 STF4 Data"; libname stf904 '/pub/data/stf904'; libname user '/SAS_temp/user'; **filename reca '/pub/data/stf904/Tools/sa6308.recorda'; ***filename recb '/pub/data/stf904/Tools/sa6308.recordb'; filename cendict '/pub/data/stf904/stf904.cendict'; *----Step 1: Read the cendict file to create the tables dataset-----; data stf904.tables; infile cendict firstobs=1011 truncover lrecl=80; length Tableid $8 TableName $240 Universe $140 TableType $3 AorB $1 TableSfx $1 TableNo 3 Ncells 4; retain; input @1 _rt $2. @; if _rt=' ' then stop; if _rt='T1'; Universe=' '; TableName=' '; TableSfx=' '; TableNo=.; input Tableid $4-10 Ncells 34-38; TableType=substr(Tableid,1,2); Tableno=substr(tableid,3); if TableNo=. then do; _error_=0; tableSfx=substr(tableid,length(tableid),1); Tableno=substr(tableid,3,Length(tableid)-3); end; *---read the T2 record(s) with table name (title)----; input _rt $2. _ckTableid $4-10 TableName $11-80; if _rt ne 'T2' or _ckTableid ne Tableid then do; file log; put '***Logic Error reading cendict - expected T2 line not found**** ' / _all_; abort abend; end; if TableName='FILLER' then delete; *--Now read either a continuation line of the table name or the T3 universe record--; readt3: input _rt $2. _ckTableid $4-10 Universe $11-80 ; if not (_rt in ('T2','T3')) or _ckTableid ne Tableid then do; file log; put '***Logic Error reading cendict - expected T2 or T3 line not found**** ' / _all_; abort abend; end; if _rt='T2' then do; if length(tablename) < 70 then TableName=trim(TableName)||' '||Universe; else TableName=trim(TableName)||Universe; goto readt3; end; *---At this point we have all the information we need. So we ouput and let the program cycle. Note that it will loop thru the remainder of the lines looking for the next T1 line at the top of the step.---; %mixcase(TableName); *<--this is Ian Whitlocks utility macro stored in our macro library--; *---Enhance the table name by pre-appending the Id and post-appending the size (ncells) in brackets--; TableName=trim(tableid) ||'. '|| trim(TableName)||' ['|| trim(left(put(ncells,4.)))||']'; AorB=substr(TableType,2,1); output; return; drop _: ; run; proc sort data=stf904.tables; by tabletype tableno tablesfx; run; options ls=160 ps=80; proc print data=stf904.tables(keep=tablename); where length(tablename) ge 108; title2 'stf904.tables - Where length(tablename) ge 108 '; run; proc print data=stf904.tables(obs=100); by tabletype ; id tableid; title2 'stf904.tables'; run cancel ; /* =======================Begin comment================ *-----Step 2a: Create a variables dataset from a combination of running proc contents and capturing most characteristics and then merging with data gathered from the ICPSR code files to get var labels. Tricky part of this is that our varnames and those used by ICPSR are not the same. So we need to convert. -----; proc contents data=stf904.usatbls out=acontents noprint; proc contents data=stf904.usbtbwhi out=bcontents noprint; data variables1; length Name $32 Varnum 4 formatl formatd 4 TableID $8 TableCell 3 TableType $2 Tableno 3 TableSfx $1; set acontents bcontents; length _cellno $4; _cellno=scan(Name,-1,'I'); if _cellno=' ' or anyalpha(_cellno) then delete; *<---it is not a table cell variable unless it ends with the letter "I" followed by the cell number----; TableCell=_cellno; TableId=substr(name,1,index(name,'I')-1); *--The portion of the name before the I is the table identifier. E.g. if Name is HA12I4 then HA12 is the tableid and 2 is the tablecell number.---; TableType=substr(Tableid,1,2); if anyalpha(substr(tableid,3))=0 then Tableno=substr(tableid,3); else do; tableSfx=substr(tableid,length(tableid),1); Tableno=substr(tableid,3,Length(tableid)-3); end; keep Name -- TableSfx ; run; proc sort data=variables1; by tableid tablecell; run; proc print data=variables1 (obs=100); title2 'Sample observations from variables1 - proc contents based data with no label values'; by tableid; id tableid; run; *-----Step 2b: read the labels statement code from ICPSR and edit the names. Create a SAS dataset and write a pair of varlabs files. We do the same thing twice so we use a macro. ----*; %macro genlabs(type); %let utype=%upcase(&type); data labels&type; infile "/pub/data/stf904/Tools/sa6308.record&type" truncover; length label $80 _word1 $10 altname $8; do until ( _word1='LABEL'); *<--we edited the code just a teeny bit so that we could easily ID this spot; link read1; end; link read1; do until (_word1 =';'); label=scan(_infile_,2,'='); label=compress(label,'"'); *--remove the quotes on either end of the label value---; length TableId $8 TableCell 3; tableid=scan(_word1,1,'_'); if tableid=_word1 then do; if tableid=:'PB10' or tableid=:'PB11' or tableid=:'PB12' then tableid=substr(_word1,1,5); else tableid=substr(_word1,1,4); if tableid='P91C' then tableid='PB91C'; else if tableid='P115' then tableid='PB115'; else if tableid='P100' then tableid='PB100'; end; if tableid='D' then tableid='H58'; *<---very special case--; if substr(tableid,2,1) not in ('A','B') then if tableid='P100' then tableid='PB100'; else tableid=substr(tableid,1,1)||'B'||substr(tableid,2,2); *--in a few cases on the B tables they omitted the table-type code, e.g. H5711111 was their name for HB57I1 --; if index(_word1,'_')=0 and (substr(_word1,5,1) in ('A','B','C')) then tableid=trim(tableid)||substr(_word1,5,1); if tableid ne lag(tableid) then TableCell=1; else TableCell+1; altname=_word1; if tableid ne 'HA4' then output; *<--table HA4 became "filler" when a table was cancelled-; link read1; end; stop; *---we controlled our own looping this step---; read1: input _word1 $10.; _word1=compress(_word1,'='); return; keep tableid tablecell label altname; run; proc sort; by tableid tablecell; run; data stf904.variables&type; length Name $9 label $80; *--we have 1 variable - pb115I100 with a 9-character name!--; merge variables1 (where=( substr(TableType,2,1)="&utype" ) in=invars ) labels&type(in=inlabs); by tableid tablecell; if not (invars and inlabs) then do; _nerr+1; if _nerr ge 200 then abort abend; else put '**something is amiss with this variable: ' _N_= tableid= tablecell= invars= inlabs=; end; drop _: ; run; proc sort; by tabletype tableno tablesfx; run; proc print data=stf904.variables&type(obs=100); title2 "Some Observations on stf904.variables&type"; run; %mend genlabs; options mprint; %genlabs(a); %genlabs(b); data stf904.variables(label='Metadata for table cell vars: A and B table types combined'); set stf904.variablesa stf904.variablesb; by tabletype tableno tablesfx; run; proc datasets nolist library=stf904; modify variables; *index create tabletype; *index create tableid; index create tabids=(tabletype tableno tablesfx); quit; *-----Step 3: Generate the Varlabs modules which are in the form of SAS labels statements but also have the table titles and universes interspersed as comments to make it read more like a data dictionary. We also include the alternate (ICPSR) names, commented. ----*; %macro genlabs2(type); %let utype=%upcase(&type); filename sasout "/pub/data/stf904/Varlabs/&type.tables.sas"; data _null_; merge stf904.tables stf904.variables end=lastvar; by tabletype tableno tablesfx; where substr(tabletype,2,1)="&utype"; file sasout; retain _oQ '/_' _cQ '_/' _tic "'" _eqtic "='"; if _n_=1 then do; substr(_oQ,2,1)='*'; substr(_cQ,1,1)='*'; *--had to do it because says took the chars * and / within the quotes as an end-comment string---*; end; if _n_=1 then do; put @12 _oQ "Statement for variables in &utype tables, 1990 Summary Tape File 4. " _cQ / @12 _oQ "Alternate variables names (ICPSR) shown as comment on right." _cQ /; end; if first.tablesfx then do; put / _oQ tableid +(-1) '.' TableName '[' ncells +(-1) ']' _cQ / _oQ 'Universe: ' universe _cQ / @5 'Label' ; end; put @7 name _eqtic label _tic @72 _oQ altname _cQ @; if last.tablesfx then put ';'; else put ; *--release trailing @ --; run; %mend genlabs2; %genlabs2(a); %genlabs2(b); ======================End comment================== */