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!
/*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!
/*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!
Thank you!
This is exactly what I need.
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.
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.