Hi All,
I need to search a list of strings across sas jobs(.sas) in a library . I have used the following
filename abc pipe 'findstr "string" C:\Users\folder_name\*.sas 2>&1';
data Result;
infile abc truncover;
input ColName $200.;
run;
proc print data=Result;
run;
It works well. But the the list of strings i have is more than 100 , so i need to use macro to pass the string. Could some one help please?
Here is a technique I use that uses PRXMATCH to search for multiple strings. Hope you find it helpful.
%macro search(dir,string);
filename listing pipe "dir &dir\*.sas /b";
data new;
infile listing truncover ;
input filename $80.;
filename="&dir\" || filename;
infile dummy filevar=filename end=done truncover;
do while (not done);
input x1 $100.;
if prxmatch("m/&string/oi",x1) > 0 then output;
end;
run;
%mend search;
/** macro and print are the two strings I am searching for. You need the | between each string you are searching for **/
%search(c:\my_folder,macro|print)
proc print;
run;
Thank you very much.
I should be more clear . The ultimate aim of the task is to search for the list of strings among all sas jobs in a library and if any of the string is available, it should throw out the name of the job. Th code which is have posted is unable to search for more than one string at a time, while i have more than 100 string s to search. Would you please help in that.
Not sure what you mean when you say 'unable to search for more than one string at a time'.
My macro call below searches for the string 'macro' and 'print' (I could list as many strings I want here as long as it is under ~64K) :
%search(c:\my_folder,macro|print)
Doesn't the output file, new, contain a variable called filename with the filename/job name?
Hi @SAS_INFO
May I suggest a small improvement of the code supplied by @russt_sas .
I have added three lines to the code, so the output now contains the file name and line number besides the lines containing one of the search strings.
(+ changes in 2 input formats to handle longer strings based on my actual test input),
%macro search(dir,string);
filename listing pipe "dir &dir\*.sas /b";
data new;
infile listing truncover ;
input filename $255.;
file = filename;
filename="&dir\" || filename;
infile dummy filevar=filename end=done truncover;
obsnum = 0;
do while (not done);
input line $char255.;
obsnum + 1;
if prxmatch("m/&string/oi",line) > 0 then output;
end;
run;
%mend search;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.