%MACRO readudir ( /* This macro reads a Unix directory system as specified by the toppath parameter. It creates a series of SAS data sets (files, dirs, and pts2fils in metapath data library) containing information about the subdirectories and files within the specified directory system. Being written primarily as a tool for managing the mcdc public data archive and it has code that is specific to that beast. Modeled on the readudir module in the sascode directory, originally coded in the mid-90s. It was "macrofied" and modified Dec. 2001 as part of the MCDC work plan project to build a metadata base for the mcdc data archive. Developed and tested on an AIX platform running sas8.2 and sas9.1 Modified 7/08 to comment out code related to sasfiles. We already have this kind of data being captured in our dataasets datasets and we would need to merge those data with what is being captured here. For now the data we get here is more about space and file stats than about true metadata where we require custom intelligence to be added. John Blodgett, OSEDA */ toppath=%str(/pub/data), /* The directory to be explored and about which we want to capture data about the files and subdirectories thereof. */ metapath=%str(&toppath./meta), /* By default the output sas data sets are stored in this data library directory. Default says to put it in the meta subdirectory of &toppath */ subpaths=, /* the null default says to explore ALL subdirectories of &toppath. A list of paths can be specified for this parm, in which case processing will be limited to just those paths. They are assumed to be subdirectories of &toppath and should not include that part, nor should they contain leading '/'s. */ update=0, /* if update=1 is specified then the meta data sets are assumed to already exist and we shall just be updating them. Usually specified in conjunction with subpaths */ excldirs=%str(obsolete meta), /* Any directory with a name in this list will be ignored while processing */ exclexts=asv sas7indx snx01 arc temp, /* Any file with an extension in this list will be ignored */ hidden=0, /* Specify hidden=1 to process hidden files (e.g. ".profile") */ test=0, /* If test=1 is specified then output goes to &metapath/test */ debug=0); %*-----See sample invocation in /pub/data/meta/Tools/metadata.sas ----------; %local _revdate; %let _revdate=24JUL08; *<----date of last revision; /* END */ %PUT %STR( ); %PUT ****************************************************************; %put * readudir macro Rev &_revdate begin execution *; %put * Missouri State Census Data Center *; %PUT ****************************************************************; %PUT %STR( ); %if &test %then %let metapath=&metapath/test; %put ------ Value of toppath parameter is: &toppath; %put ------ Output data sets will be written to &toppath/&metapath ; libname meta "&metapath"; %local lspath; %if %superq(subpaths) ne %str() %then %do; x cd &toppath; %let lspath=&subpaths; %end; %else %let lspath=&toppath; %local a; %if &hidden %then %let a=a; %else %let a=; filename in pipe "ls -lR&a &lspath"; data meta.dirs(keep=toppath--filetype ) meta.files (keep=subdir filename libref--filesize ) /* ---commenting this out for now---- meta.sasfiles(label='Information specific to SAS data files (data sets & views)' keep=libref filename setlabel -- datayears) --------end comment----------- */ ; *<---end of data statement---; *----Do not alter the order of these attrib statements since we count on this order when using the double-dash variable lists in the keep options of the data statement----; attrib toppath length=$16 label='Top Level Data Directory' ; attrib dirgroup length=$16 label='Directory Group'; *<--??--; attrib subdir length=$32 label='Subdictory (within Path)'; attrib libref length=$8; *<--unique libref/"nickname" to reference subdir. Key variable, dirs data set-; attrib dirdesc length=$800 label='Dir Description' format=$char120.; attrib dirlvl length=3 label='Dir level: # of subdirs'; attrib nsubdirs length=3 label='# subdirectories'; attrib nfiles length=3 label='# files in subdir'; attrib dirsize length=8 label='Directory Size in Bytes' format=comma12.; *<--from ls command, includes all subdirs-; attrib datemod length=8 label='Date Last Modified' format=date7. informat=date7. ; *--prints as ddMMMyy -; attrib timemod length=5 label='Time Last Modified' format=hhmm5. informat=time5.; attrib filetype length=$8; *--Usually the same as subdir and libref, it is the first level directory in subdir. If subdir is sf12000/xxgeos then filetype is sf12000.--; attrib filename length=$80 label='File Name '; *-without extension. So cnvtsf1.sas has filename=cnvtsf1 . Note that on the sasfiles set filename is also the SAS set name--; attrib fileext length=$8 label='File extension'; *<---portion of filename following last period, e.g. sas7bdat, sas, pdf, html, etc.---; attrib filegrp length=$16 label='File Group'; *--e.g. geos, ph and pct would be file groups under sf12000 filetype-; attrib fileprio length=$1 label='File Priority'; *<--'0' for files beginning with upper case letters, '1' for most important files (they will be listed in this order by uexplore or its successor), '2' for medium important and '3' for least important--; attrib filedesc length=$800 label='File Description' format=$char120.; attrib linksto length=$200 label='Soft Link To'; *<--if it is a soft link this is the file it links to (fully qualified file name)--; attrib filesize length=5 label='Size of File' format=comma9.; /* *----sas dataset metadata vars start here. Get these from Datasets datasets? ----; attrib setlabel length=$64 label='SAS data set label'; attrib nobs length=3 label='# observations on SAS data set'; attrib nvars length=3 label='# variables on SAS data set'; attrib engine length=$8 label='SAS engine'; attrib indices length=$40 label='SAS Data Set Indices'; attrib keyvars length=$80 label='SAS Set Key Variables'; attrib sortedby length=$80 label='Sorted By'; attrib meanvars length=$320 label='List of Vars that are Means, Ratios, Pcts, Etc.'; attrib meanwts length=$320 label='List of Vars Corresponding to meanvars to use as weights for Agg Macro'; *<--to assist in automating aggregation we specify another variable IN SAME DATA SET that can be used to take weighted avg.--; attrib guniverse length=$16 label='Geographic Universe'; *--Values are 'Missouri', 'United States', 'Illinois', 'Kansas', etc.--; attrib geounits length=64 label='Geographic Summary Units'; *--keywords such as state county tract-; attrib datayears length=$20 label='Years for which data applies'; */ retain toppath "&toppath"; infile in missover length=ll col=curcol end=lastline; *--specify some variable lengths and formats-; length inrec $200 datein $8 timein $5 owner group $8; retain keepflag 1; *<--see comment below. potential use for filtering subdirectories-; retain dirno -1; *--used as a simple numeric key corresponding to the directories. 0 for the &toppath dir, then numbered seq. allows easy linking between dirs and files; *--we key off the first column. If its a blank we are between dirs. directory header lines begin with "/". For file lines the 1st char is critical; retain lpath; if lpath=. then lpath=length(toppath)-1; retain dirlvl subdir; input col1 $1 @; if col1=' ' then do; input; return; end; *--flush record--; if col1='/' then do; if dirno gt -1 then link dirout; *<--write the dirs obs--; dirno+1; input inrec $; *<--this reads starting in col 2. You should get the value of toppath, a /, the subdir name and a :. We edit to keep only the subdir name; /* if dirno eq 0 then do; subdir=' '; dirlvl=0; end; */ *--we were losing the first subdir so we edit code to elim check for 1st dir. guess we thought we got the parent dir listed but no...; if dirno eq -1 then do; subdir=' '; dirlvl=0; end; else do; subdir=substr(inrec,lpath+1); *--remove the trailing colon--; subdir=substr(subdir,1, length(subdir)-1); *--Here is where you COULD put code to filter on selected dirs--; **keepflag= subdir in ('this','that','thother'); **But for now just assume we want to keep everything!--; keepflag=1; dirlvl=0; ic=index(subdir,'/'); temp=subdir; if ic then do until(ic=0); temp=substr(temp,ic+1); dirlvl+1; ic=index(temp,'/'); end; end; return; end; if col1='t' then do; input next $ @; if next='otal ' then do; input dirsize; dirsize=dirsize* 01024 ; *--convert to bytes from kbyte chunks; retain dirsize; end; else dirsize=.; *-should not happen--; return; end; if col1='d' then do; nsubdirs+1; return; end; *--the literals in next line specify the kind of "files" you want to keep. We keep regular files (-) and links (l) only--; if col1 not in ('-','l') or not keepflag then do; *--notice that we do not keep directories as files, but could easily; input; *--flush the line; if lastline then link dirout; return; end; *--read the critical ls line with file info--; fileno+1; *<---assign unique sequential file key---; nfiles+1; *--count the files within the subdirectory-; input @1 inrec $varying160. ll @; *-read the whole record into a string; ft=col1; *--filetype flag-; *--now go back and read the line again storing specific variables; input permits $2-10 links owner $ group $ size @; length dtn $144; *-date time name-; dtn=left(substr(inrec,curcol-1)); date=substr(dtn,1,6); time=substr(dtn,8,5); *<--can be a yr or hh:mm; dtn=left(substr(dtn,13)); ic=index(dtn,'->'); if ic then do; filename=substr(dtn,1,ic-1); linksto=substr(dtn,ic+3); end; else filename=dtn; if scan(filename,2,'.') ne ' ' then do; fileext=scan(filename,-1,'.'); *--last word in filename string as delimited by periods--; *--remove last word from filename using utility macro-; %dlastwd(filename,dlms=%str(.)) end; if fileext ne ' ' and index("&exclexts",trim(fileext)) then do; if lastline then link dirout; return; end; length _first2 $2; if substr(filename,1,1) ne lowcase(substr(filename,1,1)) then fileprio='0'; else _first2=substr(filename,1,2); if fileext=:'sas' or filext in ('dat','zip','csv','dbf','xls') then select ( _first2 ); when ('mo','us') fileprio='1'; when ('il','ks') fileprio='2'; otherwise fileprio='3'; end; filetype=scan(subdir,1,'.'); output meta.files; *<---here is where we finally write an observation for each file--; /* <=======================comment out======================= *--Now check to see if its a SAS data file and, if so, output to the sasfiles set as well---; if fileext in ('sas7bdat','ssd01','sas7bvew','ssv01') then do; select ( _first2 ); when ('mo') guniverse='Missouri'; when ('us') guniverse='United States'; when ('il') guniverse='Illinois'; when ('ks') gunvierse='Kansas'; otherwise guniverse=' '; end; *--....etc...; output meta.sasfiles; end; ========================end comment========================== */ if lastline then link dirout; *<--after last file process last dir-; return; * keep toppath subdir ft filename permits links owner group size date time; * keep nfiles nsubdirs dirsize dirlvl; *--dirs only vars; * keep fileno pts2path pts2file; dirout: nsubdirs=nsubdirs-2; *<---less confusing when you do not count . & ..; output meta.dirs; nfiles=0; nsubdirs=0; dirsize=.; return; run; %mend readudir;