x cd /pub/data/stf903/Tools; %let pgm=genVariables; filename pgm "&pgm..sas"; *--Generate the meta-data set stf903.VariablesPH from a proc contents dataset and from parsing the text in a data dictionary file. jgb, 1/05 --*; libname stf903 '/pub/data/stf903'; libname user '/SAS_temp/user'; filename datadict "../Docs/datadict.asc"; %let upset=%upcase(); *-- Extract metadata from a sample stf903 data set. Here we get the variable name, label, type and length.--; proc contents data=stf903.mostcnty out=contents(keep=varnum type formatd formatl name label) noprint; run; proc sort data=contents(where=(type=1)); by varnum; run; data contvars; set contents; where upcase(name) in: ('P','H'); *--ignores id variables--; length TableID $8 TableCell 3 IndentLevel 3; TableID=scan(name,1,'I'); TableCell=scan(name,2,'I'); IndentLevel=verify(label,':') -1; *--position of first : less one. If no : then it is 0--; run; proc sort; by tableid TableCell; run; data stf903.tables; infile datadict; length _rectype $2 TableID $8 TableType $1 TableNo 3 TableSfx $1 TableName $128 Universe $96 Ncells 3 ; **** length Name $8 Label $96 TableCell 3 IndentLevel 3; do until (_rectype='T1'); link read1; end; length _in $132 _str $40 _dims 8; Ncells=scan(_in,-2); *--2nd to last word on the T1 input record is the # cells in the table--; _dims=scan(_in,-1); *--table dimensions--; TableId=scan(_in,2); TableType=substr(TableID,1,1); _lentabid=length(TableID); _lentabno=_lentabid -1; if substr(TableID,_lentabid,1) in ('A','B','C') then do; TableNo=substr(TableID,2,_lentabno-1); TableSfx=substr(TableID,_lentabid,1); end; else TableNo=substr(TableID,2,_lentabno); if TableType='P' and TableSfx=' ' and tableno in (17,18,30,114, 122, 128, 130, 131, 132, 133, 134, 136, 137, 138, 139, 140, 141, 142, 143, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 159, 160, 163, 164, 165, 166, 170 ) then ncells=ncells-1; ****Custom Edit of bad data dict-; if TableId='P114B' then ncells = ncells-1; link read1; if _rectype ne 'T2' then _errmsg='Missing T2 line for '||TableId; if scan(_in,2) ne TableId then _errmsg='TableID does not match current table: ' ; TableName=substr(_in, 11); mixit: %mixcase(TableName); TableName=trim(TableId)||'. '||trim(TableName) ||' ['||trim(put(ncells,4.-L))||']'; link read1; if _rectype='T2' then do; *--very long table title is continued on another line/card--; TableName=trim(TableName)||' '||substr(_in,11); goto mixit; end; if _rectype ne 'T3' then _errmsg='Missing T3 line for ' ||TableId; else Universe=substr(_in,11); if _errmsg ne ' ' then putlog / "*****ERROR : " _errmsg / _infile_ / _all_ ; else output; drop _: ; return; read1: input _rectype $2. ; _in=_infile_; _lenin=length(_in); return; run; proc print data=stf903.tables; id tableid; var tablename ncells tabletype tableno tablesfx; run; proc sort; by tableid; run; data Variables(compress=yes); merge stf903.tables(in=intabs) contvars(in=invars); by tableid; if last.tableid then do; if not (intabs and invars) then do; put '***Table not on both datasets*** ' / _all_; _nerr+1; if _nerr ge 5 then abort abend; end; else if TableCell ne ncells then do; put '***TableCell and ncells do no match*** ' ncells= TableCell= / _all_; _nerr+1; if _nerr ge 50 then abort abend; end; end; drop _:; run; proc sort data=stf903.Tables; by TableType TableNo; run; proc sort data=Variables out=stf903.Variables(label='Variable level metadata' index=(tableid) compress=yes); by TableType TableNo TableCell; run; proc contents data=stf903.variables details; run;