I have a folder containing 5 .sas files, I would like to search particular word (Ex: proc) in the files and I want to know how many times the word is present in each file, if not present display a message not present.
EX: Folder name: SAS files
.sas files: 1. data.sas 2. proc.sas 3. data2.sas 4. freq.sas 5. data3.sas
I would like to know the word "proc" is present in each file.
Your question actually has two twists: 1) stream in all of the files 2) find and count WORD 'proc'. For 1), there are many ways to do it, the following code 's approach is just one of them. Do a google search, you will find many. The solution below does not require to know the file names, but it will investigate all of the files inside the same folder. 2). Since you mentioned WORD, so 'proc1' or 'xxproc' seems not be qualified, and I assume case is not important here. Otherwise, COUNT() will be very straightforward. So both FINDW() and some PRX functions will complete the task, here only FINDW()is showing.
%let folder=h:\sas files\;
filename inf pipe 'dir /b "h:\sas files\"';
data want;
length fileloc _file $ 300;
infile inf truncover;
input fileloc $ ; /* read instream data */
/* The INFILE statement closes the current file
and opens a new one if FILELOC changes value
when INFILE executes */
_file="&folder."||fileloc;
infile dummy filevar= _file
end=done truncover;
/* DONE set to 1 when last input record read */
do while(not done);
/* Read all input records from the currently */
input var $ 100.;
start=1;
do while (1);
rc=findw(var,'proc',' ',start,'i');
if rc=0 then
leave;
ct+1;
start=rc+4;
end;
end;
output;
ct=0;
drop rc start var;
run;
The code has been tested on 9.3 M2, modify the folder address etc to accommodate your own settings.
Good luck,
Haikuo
Hi Haikuo,
Thank you very much for your answer, actually in my question one more twist is there, that is I would like to display how many times the word "proc" is available in each file.
Ex; Final result will be displayed like the below in log/output
Word proc existed in data.sas: 57 times
Word proc existed in proc.sas: 11 times
Word proc existed in data1.sas: 21 times
Word proc existed in means.sas: 31 times
Word proc existed in data3.sas: 41 times
If not existed in any of the files it would display like the below
word proc does not existed in data3.sas
/*if you have SAS 9.4*/
data test;
infile cards truncover;
input file :$ 20. ct;
call symputx('file',file);
if ct>0 then do;
call symputx('ct',ct);
_n_=dosubl('%put Word proc existed in &file.: &ct times;');
end;
else _n_=dosubl('%put word proc does not existed in &file.;');
cards;
data3.sas 15
date4.sas 0
;
/*if no SAS 9.4*/
data test;
infile cards truncover;
input file :$ 20. ct;
call symputx('n',_n_);
call symputx('file'||strip(_n_),file);
if ct>0 then do;
call symputx('ct'||strip(_n_),ct);
call execute('%put Word proc existed in &&file&n.: &&ct&n. times;');
end;
else call execute('%put word proc does not existed in &&file&n.;');
cards;
data3.sas 15
date4.sas 0
;
Please note, the 'test' is showing the data structure you get from my previous answer.
Good luck,
Haikuo
Thank you very much Haikuo.
Best
Venkatesh Y
filename inf pipe 'dir /b "&folder"';
Hi Haikuo,
I want use the filename statement in the Macro, for that one I have written like the above but unfortunately I'm getting the error, can you please guide me in this regard.
Best
Venkatesh
&folder won't resolve inside single quotes so you need to apply the single quotes differently. Here is one way:
filename inf pipe %str(%')dir /b "&folder"%str(%');
Beside what SASkiwi pointed out, the problem here is that, DIR command requires double quotes if folder names contain 'blank'. So a working syntax may look like the following:
filename inf pipe "dir /b %str(%")&folder.%str(%")";
Good luck,
Haikuo
A UNIX solution looks like that, done with system tools (no SAS needed):
cd wherever_your_files_are
for FILE in *.sas
do
echo "$FILE:"
grep -i -w proc $FILE|wc -l
done
It only assumes that you do not have more than one proc statement in one line of program text.
Just read the files word for word and let SAS do the math. If you do not want the total count across all files then add the NWAYoption to PROC SUMMARY.
%let path=C:\My SAS Files;
data step1 / view=step1 ;
length fname filename word $200 ;
infile "&path\*.sas" filename=fname dlm=' ;' ;
input word @@ ;
filename=scan(fname,-1,'\/');
proc_count=upcase(word)='PROC';
keep filename proc_count;
run;
proc summary data=step1 noprint ;
class filename ;
var proc_count ;
output out=proc_count sum= ;
run;
proc print;
run;
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.