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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

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
  • 2 replies
  • 2051 views
  • 0 likes
  • 2 in conversation