filename myfile "u:\logfile_text4.log"; proc printto log=myfile new; run; options symbolgen mprint mlogic ; %let path=v:\Development\finance_dev; /*location of the directory you want to search*/ %let outpath=u:\; *location of where to put your output files; %let string = '\apps\can_idb_data\gidb'; /*string you want to search for. Needs to be enclosed in single quotes*/ %let ext = SAS; /*file extension*/ libname perm "&outpath"; /*create a listing of all the folders and subfolders in a directory*/ %macro paths (path); filename DIRLIST pipe "dir /-c /q /s /t:c ""&PATH""" ; data perm.dirlist (keep=path); length path line $1024 ; retain path ; infile DIRLIST length=reclen ; input line $varying1024. reclen ; if reclen = 0 then delete ; if scan( line, 1, ' ' ) = 'Volume' | /* beginning of listing */ scan( line, 1, ' ' ) = 'Total' | /* antepenultimate line */ scan( line, 2, ' ' ) = 'File(s)' | /* penultimate line */ scan( line, 2, ' ' ) = 'Dir(s)' /* ultimate line */ then delete ; dir_rec = upcase( scan( line, 1, ' ' )) = 'DIRECTORY' ; /* parse directory record for directory path */ if dir_rec then path = left( substr( line, length( "Directory of" ) + 2 )) ; run ; /*keep only a unique value of the directory*/ proc sort data=perm.dirlist nodupkey; by path; run; %mend; %paths(&path.); data _null_; set perm.dirlist; call symput("path"||compress(_n_),trim(path)); call symput("numpath",compress(_n_)); run; /*delete the all dataset if it already exists else results will be continuously appended to it*/ proc datasets library=work; delete all; quit; %macro readraw; %do i = 1 %to &numpath; %let varlist2 = ; %let varlist2 = %bquote(&&path&i); %local fileref rc did dnum dmem memname dsn didc; %let dir=&varlist2; %let rc=%sysfunc(filename(fileref,%superq(dir))); %let did=%sysfunc(dopen(&fileref)); %if &did=0 %then %do; *--if it cant fnd the directory it will stop the macro; %put ERROR: Directory does not exist; %put ERROR- The macro will terminate.; %return; %end; %let dnum=%sysfunc(dnum(&did)); %do dmem=1 %to &dnum; %let memname=%Qsysfunc(dread(&did,&dmem)); *--this is telling the code that we are only interested in files that have an extension of SAS; %if %qupcase(%qscan(%superq(memname),-1,.))=&ext. %then %do; *--depending on the file type you are looking at, you may need another method to read in the file contents; data temp; length program $3600.; infile "&dir.\&memname." truncover; input @01 line $char2500. ; if index(lowcase(line), lowcase(&string.)) > 0 then do; line_num=_n_; program="&dir\&memname."; output; end; run; proc append data=temp base=all; run; %end; %end; %let dsid = %sysfunc(open(all)); *-- If the data set exists, then grab the number of observations and variables; *-- then close the data set ; %if &dsid %then %do; %let nobs =%sysfunc(attrn(&dsid,nobs)); %let rc = %sysfunc(close(&dsid)); %put &nobs; %if &nobs > 0 %then %do; ods listing close; ods tagsets.excelxp file="&outpath.search results.xml" style=statistical options(autofilter='all'); proc print data=all noobs; title "Looking for &string."; title2 " &string found in the following programs"; run; ods tagsets.excelxp close; ods listing; %end; %else %put NOTE: &string not found in &dir.; %end; %let didc=%sysfunc(dclose(%nrbquote(&did))); %let rc=%sysfunc(filename(fileref)); %end; %mend readraw; %readraw; title; proc printto; run;