BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
5 REPLIES 5
David_SAS
SAS Employee
There's a Technical Support FAQ that shows how to do what you want for a single directory:

http://support.sas.com/faq/044/FAQ04451.html

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

-- David Kelley, SAS
deleted_user
Not applicable
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!!!!!
Eric_SAS
SAS Employee
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
http://support.sas.com/rnd/base/topics/odsmarkup/tagsets.html

Here's an example of recursion in a tagset.

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

proc template;
define tagset tagsets.test;

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

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;
end;
end;

run;

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

ods _all_ close;
deleted_user
Not applicable
JonT-
It sounds like something that a modification to my harvesting macro -- see my SUGI31 paper @ http://www2.sas.com/proceedings/sugi31/150-31.pdf -- 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
ChrisNZ
Tourmaline | Level 20

You may find this program of mine useful.

It runs on Win and Unix,

It can scan subdirectories, and can scan archive files (ZIP, EGP, DOCX, etc).

It can read files contents, or just retrieve file names.

It extracts file metadata.

It can filter the files and/or contents to be targeted in different ways.

Enjoy!

 

 

 

 

 

 

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2213 views
  • 1 like
  • 4 in conversation