The SAS Output Delivery System and reporting techniques

SAS Code Search

Posts: 0

SAS Code Search

I have successfully designed a SAS macro to search through any number of text files (*.sas, *.log, *.txt etc) and search for a specified string in a specified directory and all related sub directories.

I cheated (in a way) by using a DOS command to generate the file list: "dir *.&ext /s /b >dirlist.txt". Then I used the generated dirlist.txt to run through the FDB to quickly scan all the files.

I had an idea of using the dopen command to do much the same in SAS, but it just seemed a whole lot easier to do it from DOS. If I wanted to do the same on another OS I would like to know how to do it in SAS. Any suggestions? And if possible: KISS!

The idea of using the FDB method over reading all the lines into a dataset (one per program), lent to running through 3000+ programs in under two minutes.
SAS Employee
Posts: 174

Re: SAS Code Search

There's a Technical Support FAQ that shows how to do what you want for a single directory:

I don't know how to easily extend this to work recursively.
Users, help me out here!

-- David Kelley, SAS
Posts: 0

Re: SAS Code Search

That's the trick. It’s easy for a single directory. I was getting my head in a loop trying to do it for the sub-directories. I figured that the fileinfo function would return that a directory member was not a file - and therefore a sub directory - and thus run through the file read macro again. Just didn't want to go down the line of a looping do while statement.

I put this on the forum more out of interest, than need, so don't put too much effort into solving it. Just wondered if anyone had tried doing something similar and what their result was.

Or turning the problem on its head, if you were trying to do this in SAS, then the DOS solution appears to be easier!!!!!
SAS Employee
Posts: 95

Re: SAS Code Search

It would be fairly easy to adapt that macro code to work within a tagset.

An event with the same functionality of the macro could recursively trigger
itself when a directory is found.

I seem to recall that the maximum recursion depth is 200. That should
be plenty deep enough.

There are various examples of using data step functions in tagsets at

Here's an example of recursion in a tagset.

/*-- Recursion limit is 200. If the limit is hit it will unwind --*/
/*-- automatically. --*/

proc template;
define tagset tagsets.test;

define event doc;
set $count 1;
trigger loop;
put "Count: " $count nl;

define event loop;
put "Count: " $count nl;
putlog "Count: " $count;
eval $count $count+1;
trigger loop /if $count<100;
eval $count $count-1;
put "Unwind:" $count nl;
putlog "Unwind:" $count;


ods tagsets.test file="recurse.txt";

ods _all_ close;
Posts: 0

Re: SAS Code Search

It sounds like something that a modification to my harvesting macro -- see my SUGI31 paper @ -- could accomplish.
I'm toying with the idea of implementing such searching features for SUGI32...err, i mean SAS Global Forum 2007.
-richard just fixed the link
Message was edited by: rk at Apr 14, 2006 11:53 AM
Ask a Question
Discussion stats
  • 4 replies
  • 3 in conversation