Last active
October 31, 2023 12:48
-
-
Save statgeek/26cf789d6da0ce941e447022fdccf0f4 to your computer and use it in GitHub Desktop.
SAS - search text files for a single word
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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; | |
/*Here's another variation that will search multiple files for multiple words at one time. You do need a data set with the | |
list of file names to start off with, in this example it's referred to as HAVE | |
Based on this post and Tom's original solution here: | |
https://communities.sas.com/t5/SAS-Programming/SAS-macro-variable-to-hold-all-values-of-a-field-in-a-SAS/m-p/628370 | |
*/ | |
%let searchWords = apple, banana, pears; | |
data want ; | |
set have ; | |
fname=codename; | |
nWords = countw("&searchWords"); | |
infile code filevar=fname end=eof; | |
found=0; | |
do while (not eof and not found); | |
input; | |
do i=1 to nWords while (not found); | |
str = scan("&searchWords", i, ","); | |
if find(_infile_, str ,'i') then found=1; | |
end; | |
end; | |
keep codepath found; | |
run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You're a Life Saver <3