DATA Step, Macro, Functions and more

Search for a text from diffrent directories

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

Search for a text from diffrent directories

I have too Many SAS codes in more than three Directories. How do I search for a Specific text in Side the SAS codes.

 

For instance: I want to search FNAME,LNAME in  All my SAS codes.

 

thanks

 


Accepted Solutions
Solution
‎12-30-2016 01:21 PM
Trusted Advisor
Posts: 1,378

Re: Search for a text from diffrent directories

I made a variant based on @Patrick's code:

You may change SUFFIX to LOG, TXT, or any other one.

You can add more directories in filename path.

Result is given in sas dataset, including: 

- fname = path and file name

- a_line = line that contains the string

_n = line nomber withein the input file

%let search_string = libname;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.source/*.&suffix"); 

data results;
     length fname _filepath $200;
     infile finp filename = _filepath eov=_eov truncover;
     input a_line $200.;
     fname = _filepath;
     
     if _eov=1 then do;
       _n=0;
       _eov=0;
     end;
     _n+1;
     
     if find(a_line,"&search_string",'i')
     then output;
     keep _n a_line fname;
run;     
   

View solution in original post


All Replies
Respected Advisor
Posts: 4,649

Re: Search for a text from diffrent directories

In Windows, I use the Windows Explorer search or the multi file searching feature in Notepad++.

PG
Contributor
Posts: 33

Re: Search for a text from diffrent directories

 

I am looking for SAS programme.

Respected Advisor
Posts: 3,892

Re: Search for a text from diffrent directories

[ Edited ]

One way to go:

%let search_string=libname;

filename sascode ('c:\temp\*.sas' 'c:\test\*.sas');

data _null_;
  file print;
  length _filepath $200;
  retain _found_flg 0;
  infile sascode filename=_filepath eov=_eov;
  input;

  if _eov=1 then 
    do;
      _n=0;
      _found_flg=0;
      _eov=0;
    end;
  _n+1;

  if find(_infile_,"&search_string",'i') then 
    do;
      if _found_flg=0 then 
        do;
          put / 'File is: ' _filepath;
          _found_flg=1;
        end;
      put @1 _n z4. @5 _infile_;
    end;
run;

 

Contributor
Posts: 33

Re: Search for a text from diffrent directories

[ Edited ]

Patrick,

 

I like your approach it is easy to understand. Is there away i can create the unique files name from all the directories.

 

something like this one.

FileNAME

search_string

c:\desktop\X1.SAS

Fname

c:\document\X2.SAS

Fname

c:\desktop\file\X3.SAS

Fname

 

 

 

 

Thanks,

Solution
‎12-30-2016 01:21 PM
Trusted Advisor
Posts: 1,378

Re: Search for a text from diffrent directories

I made a variant based on @Patrick's code:

You may change SUFFIX to LOG, TXT, or any other one.

You can add more directories in filename path.

Result is given in sas dataset, including: 

- fname = path and file name

- a_line = line that contains the string

_n = line nomber withein the input file

%let search_string = libname;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.source/*.&suffix"); 

data results;
     length fname _filepath $200;
     infile finp filename = _filepath eov=_eov truncover;
     input a_line $200.;
     fname = _filepath;
     
     if _eov=1 then do;
       _n=0;
       _eov=0;
     end;
     _n+1;
     
     if find(a_line,"&search_string",'i')
     then output;
     keep _n a_line fname;
run;     
   
Contributor
Posts: 33

Re: Search for a text from diffrent directories

Dear Shmuel;

 

Is there a way we can search for more than one text.

 

Like if i want to search "FNAME" or "LNAME" or "MNAME" at the same time

 

thanks

 

Trusted Advisor
Posts: 1,378

Re: Search for a text from diffrent directories

A fast, non efficient way is to do:

 

search for string-1, copy results to results-1

search for string-2, copy results to results-2

search for string-3

merge all results by: fname, _n

 

To give you a better and more efficient way (one thread), I'll try to convert the

code into a macro - next week.

 

happy new year 2017.

Trusted Advisor
Posts: 1,378

Re: Search for a text from diffrent directories

More efficient way, without mcaro programing, just add as many as you want search_strings:

 

%let search_string_1 = lname;

%let search_string_2 = fname;

%let search_string_3= mname;


%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;


filename finp ("&root.source/*.&suffix");

data results;
    length fname _filepath $200;
    infile finp filename = _filepath eov=_eov truncover;
    input a_line $200.;
    fname = _filepath;

if _eov=1 then do;
   _n=0;

   _eov=0;
end;
_n+1;

if find(a_line,"&search_string_1",'i')

or find(a_line,"&search_string_2",'i')

or find(a_line,"&search_string_3",'i')
then output;
keep _n a_line fname;
run;

 

 

Trusted Advisor
Posts: 1,378

Re: Search for a text from diffrent directories

[ Edited ]

In unix or linux xterminal you can do:

 

cat    *.sas  | grep any_string > lising.txt

 

where: cut + grep functions scan all sas programs for "any_string" 

when found line is written to file "listing.txt"

 

It is possible to do it by sas program using functions: filename, dopen, dread, fopen, fread, close; 

Would you like to struggle it yourself ? It may take quite a time to write it and debug it.

 

here is an example of exploring one directory. (try expanding it to search a string )

 

%let path = ...;
filename mydir "&path";

data dir_info;
    length fname $40 type $12 ;
    did = dopen('mydir');
    members = dnum(did);   /* nomber of files in directory */
    if did then
       do 1 to members;
             fname = dread(did,i);
             if indexc(fname,'.') = 0 then type='directory';
                                      else type='file';
             output;
/* if type = file: open it and scan each line for the string */
/* using similar functions: fopen, fread, index, close */ end; did = close(did) keep i fname type; run;

Contributor
Posts: 52

Re: Search for a text from diffrent directories

You can try the below code to first read all the contents of the directory:

this will read the content of each file row by row and check the  contents of the file and match it against the keywords you want and then it will output.

Along with the full record, you will also get the actual file path of the file and the record number in the file, so that you always know where that record came from.

 

Cheer!

 


filename INP "c:\users\xxxxx\desktop\*.sas";

DATA SRC;
	 ;
LENGTH _filepath FILEPATH1 $550;
INFILE INP filename = _filepath LRECL=80 PAD END = DONE ;
DO WHILE(NOT DONE);
	INPUT REC $CHAR80.;
	filepath1 = STRIP(_filepath);
 	RECNO+1;
	IF INDEX(UPCASE(REC),'FILENAME') THEN OUTPUT;
END;
RUN;

Cheers from India!

Manjeet
Contributor
Posts: 33

Re: Search for a text from diffrent directories

[ Edited ]

Dear mnjtrana;

 

 I tried your code and managed to call all the SAS codes in my directories:

 

  1. My bigest problem is how to search a specific text inside the SAS codes. I am trying to search if there is "FNAME" Text inside my SAS codes.
  2. Where do you specify the search button inside your code.  

 

Thanks

 

Contributor
Posts: 52

Re: Search for a text from diffrent directories

[ Edited ]

Hi @tekish

 

In my code below, you can use the statement and replace the 'FILENAME' statement to whatever you want to search in your code.

See the undelined.

filename INP "c:\users\xxxxx\desktop\*.sas";

DATA SRC;
	 ;
LENGTH _filepath FILEPATH1 $550;
INFILE INP filename = _filepath LRECL=80 PAD END = DONE ;
DO WHILE(NOT DONE);
	INPUT REC $CHAR80.;
	filepath1 = STRIP(_filepath);
 	RECNO+1;
	IF INDEX(UPCASE(REC),'FILENAME') THEN OUTPUT;
END;
RUN;

 

 

Let me know, if that helps!

Manjeet 


Cheers from India!

Manjeet
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 12 replies
  • 441 views
  • 4 likes
  • 5 in conversation