DATA Step, Macro, Functions and more

testing if unstructured text file contains a string?

Accepted Solution Solved
Reply
Highlighted
Occasional Contributor
Posts: 18
Accepted Solution

testing if unstructured text file contains a string?

I need to determine if an unstructured text-file contains a specific string. This is different than the usual objective when reading a file (creating dataset records). Is it possible to simply read the entire file into one long string which could be parsed using FIND()?

 

Could anyone please suggest the proper way to do this?

 

BTW - the file is a log generated by another application, and I'll be searching for a short alphanumeric (<20 bytes) string consisting of only numbers 0-9 and letters A-Z. The string may be surrounded by whitespace or other symbols. Performance is not a consideration, and it would be exceedingly rare for the log-file to exceed more than a few hundred KB. Additionally, it will be rare that the string is not found in the first line of the file, however that is not guaranteed.

 

Thanks!

 

 


Accepted Solutions
Solution
a week ago
Super User
Posts: 23,776

Re: testing if unstructured text file contains a string?

/*This code will search text files for a single word, search_string
Originally via @schmuel here:
https://communities.sas.com/t5/Base-SAS-Programming/Searching-SAS-code-for-keywords/m-p/390472#M93671
*/

%let search_string = rename;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.sas_help/*.&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;     

@desertsp wrote:

I need to determine if an unstructured text-file contains a specific string. This is different than the usual objective when reading a file (creating dataset records). Is it possible to simply read the entire file into one long string which could be parsed using FIND()?

 

Could anyone please suggest the proper way to do this?

 

BTW - the file is a log generated by another application, and I'll be searching for a short alphanumeric (<20 bytes) string consisting of only numbers 0-9 and letters A-Z. The string may be surrounded by whitespace or other symbols. Performance is not a consideration, and it would be exceedingly rare for the log-file to exceed more than a few hundred KB. Additionally, it will be rare that the string is not found in the first line of the file, however that is not guaranteed.

 

Thanks!

 

 


 

View solution in original post


All Replies
Solution
a week ago
Super User
Posts: 23,776

Re: testing if unstructured text file contains a string?

/*This code will search text files for a single word, search_string
Originally via @schmuel here:
https://communities.sas.com/t5/Base-SAS-Programming/Searching-SAS-code-for-keywords/m-p/390472#M93671
*/

%let search_string = rename;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.sas_help/*.&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;     

@desertsp wrote:

I need to determine if an unstructured text-file contains a specific string. This is different than the usual objective when reading a file (creating dataset records). Is it possible to simply read the entire file into one long string which could be parsed using FIND()?

 

Could anyone please suggest the proper way to do this?

 

BTW - the file is a log generated by another application, and I'll be searching for a short alphanumeric (<20 bytes) string consisting of only numbers 0-9 and letters A-Z. The string may be surrounded by whitespace or other symbols. Performance is not a consideration, and it would be exceedingly rare for the log-file to exceed more than a few hundred KB. Additionally, it will be rare that the string is not found in the first line of the file, however that is not guaranteed.

 

Thanks!

 

 


 

Occasional Contributor
Posts: 18

Re: testing if unstructured text file contains a string?

Thank you!

 

This is exactly what I need.

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 43 views
  • 0 likes
  • 2 in conversation