/************************************************************************************/ /*** ***/ /*** Search for any given text string in all *.sas files in a directory ***/ /*** and all sub-directories. ***/ /*** ***/ /************************************************************************************/ /*** ***/ /*** Sample call: %findit(lookin = c:\mycode\reports ***/ /*** ,lookfor = data _null_) ***/ /*** ***/ /*** %findit(lookfor = %nrstr(%printit)) ***/ /*** ***/ /************************************************************************************/ %macro findit( lookin = c:\code ,lookfor = ???? ) ; %local count lookin lookfor i j ; options nonotes nomlogic nomprint nomprintnest nosymbolgen ; *** Assign a fileref to the directory and open it *** ; %let rc = %sysfunc(filename(filrf,&lookin)) ; %let did = %sysfunc(dopen(&filrf)) ; ** Ensure Directory is available *** ; %if &did eq 0 %then %do ; %put ERROR: Directory &lookin cannot be opened or does not exist. ; %goto endmac ; %end; *** Close the directory and clear the fileref *** ; %let rc = %sysfunc(dclose(&did)) ; %let rc = %sysfunc(filename(filrf)) ; %let lookin = %lowcase(&lookin) ; %let lookfor = %lowcase(&lookfor) ; *** Get Directories and subdirectories from location *** ; filename dirlist pipe "dir &lookin /b /s /a:d" ; data dirs (drop = count); length dir $ 100 ; if _n_ = 1 then do ; count + 1 ; dir = "&lookin" ; call symputx('dcount',put(count,6.)) ; output ; end ; infile dirlist end = last ; *** Handle empty Directory List *** ; if _n_ = 1 and last then goto endstep ; input dir :$100. ; count + 1 ; if last then call symputx('dcount',put(count,6.)) ; output ; endstep: run ; data _null_ ; set dirs ; call symputx('dir'!!strip(put(_n_,3.)),dir) ; run ; %do i = 1 %to &dcount ; *** Get *.sas files in directory *** ; filename saslist pipe "dir &&dir&i /b" ; data sasfiles_&i (drop = count) ; infile saslist end = last ; *** Handle empty directories *** ; if _n_ = 1 and last then do ; call symputx('fcount',put(count,6.)) ; stop ; end ; input sasprog :$100. ; count + 1 ; if not find(lowcase(sasprog), '.sas') then do ; count + (-1) ; if last then call symputx('fcount',put(count,6.)) ; delete ; end ; if last then call symputx('fcount',put(count,6.)) ; run ; %if &fcount > 0 %then %do ; proc sql noprint ; select sasprog into :sas_&i._1 %if &fcount >1 %then - :sas_&i._&fcount ; from sasfiles_&i ; quit ; %do j = 1 %to &fcount ; data found_&i._&j ; infile "&&dir&i\&&sas_&i._&j" scanover missover lrecl = 256 pad ; _infile_ = lowcase(_infile_) ; input @"&lookfor" found $ ; if length(found) then do ; put ; put "LOOKFOR: Program file: &&dir&i.\&&sas_&i._&j.." ; put _infile_ ; end ; run ; %end ; proc datasets lib = work nolist ; delete dirs sasfiles_: found_: ; quit ; %end ; %end ; %endmac : options notes ; %mend findit ; %findit(lookin = c:\myfiles\reports ,lookfor = data _null_) %findit(lookfor = %nrstr(%printit))
How can I search for a phrase in all SAS files in a directory and subdirectories?
- Author:
- Alan D Rudland
- Revision:
- 1.4
You cannot comment on this entry