x cd /pub/data/sf32000/Tools; %let pgm=create_variables_metadata; filename pgm "&pgm..sas"; *----We attempt to recreate the lost variablesXXX metadata datasets that were lost in the crash of 2-24-09. We read the 4 Varlabs files as our source. We use a setup from the acs2007 tools lib as a template as we try to recapture the same variables. ----*; libname sf3 '/pub/data/sf32000/'; libname sf32000 '/pub/data/sf32000'; libname user '/tmp/scratch/user'; title "&pgm: Generate Variable level metadata for sf32000 data sets"; /* =======================Begin comment================ proc contents data=sf3.moph(drop=_character_) noprint out=cph(keep=name label formatl formatd); proc contents data=sf3.mophct(drop=_character_) noprint out=cphct(keep=name label formatl formatd); proc contents data=sf3.mophctr(drop=_character_) noprint out=cphctr(keep=name label formatl formatd); ======================End comment================== */ *data variablesph; * length TableID $8 TableName $240 Universe $140 TableType $3 TableNo 3 TableRSfx $1 Name $32 Label $256 format $8 TableCell IndentLevel NcellsInTable 3; * set cph; %macro tabmeta(type); %local typelen typelenp1; %if &type=ph %then %str( filename in ('/pub/data/sf32000/Varlabs/Hlabels.txt', '/pub/data/sf32000/Varlabs/Plabels.txt');); %else %if &type=phct %then %str( filename in ('/pub/data/sf32000/Varlabs/HCTlabels.txt', '/pub/data/sf32000/Varlabs/PCTlabels.txt');); %else %do; data _null_; put "&type is not a valid parm value for tabmeta macro ************************* "; abort abend; run; %end; data table_titles&type; infile in lrecl=255 length=_lrec; link get1; length TableSeqNo 4 TableID $8 TableName $240 Universe $140 TableType $3 TableNo 3 TableRSfx $1; length _l $255; retain; %if &type=ph %then %do; %let typelen=1; if substr(_l,1,1) in ("P","H") then do; %end; %else %do; %let typelen=3; if substr(_l,1,3) in ("PCT","HCT") then do; %end; %let typelenp1=%eval(&typelen +1); *--we have a table title (first line) spec---; TableId=scan(_l,1,'.'); TableType=substr(TableID,1,&typelen); length _tspec $8; _tspec=substr(TableId,&typelenp1); if verify(trim(_tspec),'0123456789') then do; TableNo=substr(_tspec,1,length(_tspec)- 1); TableRSfx=substr(_tspec,length(_tspec),1); end; else do; TableNo=substr(TableID,&typelenp1); *--convert on assignment--; TableRSfx=' '; end; Universe=' '; NCellsinTable=.; _idot=index(_l,'.'); TableName=substr(_l,_idot+2); _ib=index(TableName,'['); **if _ib=0 then do; _nex=0; do while (_ib=0 and _nex <=4 ); link get1; _nex+1; TableName=trim(TableName)||' '||left(_l); _ib=index(TableName,'['); end; if _ib then NCellsinTable=scan(substr(TableName,_ib+1),1,']'); end; else if _l=:'Universe:' then do; Universe=left( substr(_l, 11) ); input @@; if _infile_=:'/* ' then do; *---continuation line recognized---; Universe=trim(universe)||left( substr(_infile_,3,length(_infile_)-4)); input; *--release trailing @@ so we start with a new input line next time. Otherwise we hold this line--; end; TableSeqNo+1; output; _nout+1; if _nout le 5 then put / _all_; *<====================; end; return; get1: do until(_first2='/*'); input ; if _infile_ eq ' ' | _lrec=1 then _first2=' '; else _first2 =substr(_infile_,1,2); end; _l=substr(_infile_,4); *--remove the leading 3 chars--; _l=substr(_l,1,length(_l)-2); *---remove asterisk slash at the end of line--; *** put '***get1 returning: ' _l $char90.; *<====temp=========; return; drop _: ; run; %mend tabmeta; /* =======================Begin comment================ options nomprint; %tabmeta(ph) ; %tabmeta(phct) ; ======================End comment================== */ data vars; set cph cphct cphctr ; length TableId $8 format $8 IndentLevel 3; if name=:'P' or name=:'H'; _i=index(name,'i'); if _i; tableid=substr(name,1,_i-1); _nblanks=length(label) - length( left(label)); IndentLevel=_nblanks/2; if mod(_nblanks,2)=1 then do; _nbad+1; if _nbad <=10 then put _all_; else abort abend; end; drop _: ; if formatl then format= left (put(formatl,3.))||'.'||left(put(formatd,2.)); *--e.g. "5.1" --; drop formatl formatd; run; proc sort data=vars; by tableid; run; data table_titles; set table_titlesph table_titlesphct; run; proc sort data=table_titles; by tableId; run; data sf3.variablesph sf3.variablesphct sf3.variablesphctr; merge table_titles (in=intables) vars (in=invars); by tableId; length TableCell 3; if not (intables and invars) then do; _nbad+1; put "**Match failure: " intables= invars= tableID= name= ; if _nbad ge 20 then abort abend; end; if first.tableid then TableCell=1; else TableCell+1; if length (tabletype)=1 then output sf3.variablesph; else if TableRsfx ne ' ' then output sf3.variablesphctr; else output sf3.variablesphct; drop _: ; run; proc print data=sf3.variablesph(obs=250); title2 "sf3.variablesph"; by tableid tableName universe; format tableName universe $40.; id tableCell; run; data sf3.variables; set sf3.variablesph sf3.variablesphct sf3.variablesphctr; run; %include sascode(notify);