- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
/*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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
/*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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you!
This is exactly what I need.