SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desertsp
Obsidian | Level 7

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
/*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

2 REPLIES 2
Reeza
Super User
/*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!

 

 


 

desertsp
Obsidian | Level 7

Thank you!

 

This is exactly what I need.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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