BookmarkSubscribeRSS Feed
SAS_INFO
Quartz | Level 8

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?

8 REPLIES 8
russt_sas
SAS Employee

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;

SAS_INFO
Quartz | Level 8

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.

russt_sas
SAS Employee

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)

SAS_INFO
Quartz | Level 8
Yes, i tried your macro with more strings and it worked fine, As we are using prxmatch it returns the position of the string that we are searching for. I am looking for the name of the job (.sas) from which the string is found. The code i shared did that but couldn't search more than one string. I need the code to search for more than one string,(which your code does wonderfully) and the output should include the name of the job.
Reeza
Super User

Doesn't the output file, new, contain a variable called filename with the filename/job name?

SAS_INFO
Quartz | Level 8
No, It has only X1 as output
Reeza
Super User
Eriks code modified to add the filename?
ErikLund_Jensen
Rhodochrosite | Level 12

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 8 replies
  • 924 views
  • 2 likes
  • 4 in conversation