SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
amager
Obsidian | Level 7

Hi, 

How to search for a string in files , i use find or findstr command x but no result.  

Anyone have a solution to this ? 

 

options noxwait; 
x " find ""data"" ""path/*.sas"""; 

/*or*/

sysexec(find "data" "path/*.sas"); 

Thanks .

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Have you tried the /s option of findstr (i.e. search all subfolders, in the below example: subfolders of C:\Temp, and the folder itself)?

findstr /s/i/c:"proc print" C:\Temp\*.sas

View solution in original post

6 REPLIES 6
Oligolas
Barite | Level 11

Hi, is there a particular reason you want to use the x command?

and did you try to search in the Forum?

________________________

- Cheers -

Kurt_Bremser
Super User

Since you are using forward slashes, I take it your SAS runs on a UNIX platform. In that case, the tool of choice is grep:

grep "string" path/*.sas

To retrieve the result in SAS, do this:

filename oscmd pipe 'grep "string" path/*.sas 2>&1';

data found_strings;
infile oscmd truncover;
input found_line $200.;
run;

In case something fails, the 2>&1 will make sure that error messages end up in your dataset in place of the result.

amager
Obsidian | Level 7

Sorry @Kurt_Bremser , i'ts windows environment : we have just find or findstr choice instead of grep. it's work in a specific repository and not in all the workspaces files . 

 

@Oligolas : i found this solution in  https://communities.sas.com/t5/SAS-Procedures/Searching-SAS-files-including-key-words-in-Windows-7/t...

for /r H: %i in (*.sas) do @findstr /i /m "obs=max" "%i"

but to replace H: by my macro-variable environment and var didn't work. i continue my resarch  ! 

FreelanceReinh
Jade | Level 19

Hi @amager,

 

Here's a simple example that works in my Windows 7 environment: A case-insensitive search for the phrase "proc print" in all *.sas files in directory C:\Temp\SAS. The search results are stored as character strings in dataset WANT.

filename test pipe 'findstr /i/c:"proc print" C:\Temp\SAS\*.sas';

data want;
infile test truncover;
input c $300.;
run;

If there were, e.g., *.sas7bdat files (i.e. SAS datasets) in the same directory, they would be searched as well (annoying Windows "feature").

amager
Obsidian | Level 7

@FreelanceReinh  Thanks for this answer like a said it's work for me but the problem with 'findstr' don't go in recursive to fetch all repository; i don't know way. 

Eg. if another file in : "C:\Temp\SAS1\" , didn't go back to fetch this repository. so i go for the second solution  :

 

for /r Z: %i in (*.sas) do @findstr /i /m "data" "%i"

I can run this command in windows cmd , but i can't run it in my SAS environment if a replace it with something like : 

filename x pipe 'for /r "&server_sas/" %i in (*.sas) do @findstr /i /m "data" "%i"'

Smiley Sad

 

EDIT : it's work prefectly with double quote " " : 

 

filename x pipe "for /r ""&server_sas/"" %i in (*.sas) do @findstr /i /m ""data"" ""%i"" "

 Smiley Very Happy

 

 

FreelanceReinh
Jade | Level 19

Have you tried the /s option of findstr (i.e. search all subfolders, in the below example: subfolders of C:\Temp, and the folder itself)?

findstr /s/i/c:"proc print" C:\Temp\*.sas

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 8117 views
  • 2 likes
  • 4 in conversation