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 .
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
Hi, is there a particular reason you want to use the x command?
and did you try to search in the Forum?
- Cheers -
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.
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 !
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").
@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"'
EDIT : it's work prefectly with double quote " " :
filename x pipe "for /r ""&server_sas/"" %i in (*.sas) do @findstr /i /m ""data"" ""%i"" "
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.