Hi,
I have a requirement to find if a particular file exists in the SAS Server using a SAS EG Program which should have unix command and need to schedule the command using Cron Job.
Can any one give me a sample X commnad do thid search ? Other than X Command do we have any other base sas coding which can be used to search for a file in a particular loaction and if the file is found open the file and read the content ?
Thanks in Advance!!!
Anumol Antony
What do you know about the file? Do you just have a name and need to search in a directory tree, or do you have an absolute path name to it?
There are a bunch of SAS external file function that might suit your needs better. They might not a easy to use like a UNIX shell command, but then you'll be "all SAS" in your program.
Another option is to use FILENAME PIPE.
Hi.
That really depends on what you are searching and how you need to read that file.
Without much more detail than you are providing, you could search the system from current directory using the system command FIND, then if found access the file and read contents using sas own IO functions.
Here's a simple example bellow with not much error handling, but It might give an idea of how to do it.
data _null_;
infile 'find . -name "MYFILE.SAS"' pipe; * search for file;
input;
_RC=filename('INFILE',_INFILE_); * try to assign a fileref;
if not _RC then do;
_FID=fopen('INFILE'); * open file;
do until (not _RC); * while there's anything to read;
_RC=fread(_FID); * read to buffer;
_RC=fget(_FID,_INFILE_,200); * retrieve from buffer;
putlog _INFILE_; * show in log;
end;
_RC=fclose(_FID); * close the file;
end;
stop; * process only first found;
run;
Daniel Santos @ www.cgd.pt
It is not clear to me what are you looking for (maybe because my limitted english):
- are you lookig for a program using a spcific file ?
then search for FILENAME with the file name and path.
- are you looking for a program that executes unix/linux (or any other OS) comand ?
then upcase(compress()) the line and search for: 'X"' or "X'" or for 'CALLSYSTEM('
If you need a code to search all programs in a path you can addapt next code:
%let search_string = ..any text ..;
%let suffix = sas;
%let root=/folders/myshortcuts/My_Folders/;
filename finp ("&root.source/*.&suffix");
data results;
length fname _filepath $200;
infile finp filename = _filepath eov=_eov truncover;
input a_line $200.;
fname = _filepath;
if _eov=1 then do;
_n=0;
_eov=0;
end;
_n+1;
if find(a_line,"&search_string",'i')
then output;
keep _n a_line fname;
run;
Thank you all for your reply.
To be precise please find my requirement.
1. I have a control file ( in CSV Format) in server folder that will be placed as a result of the camapign from SAS CI
2. I need to constalatly serach for the contraol file , if the contraol file is present I need to Open the file and read the file and capture the details into a SAS Data Set.
3. This Program needs to be scheduled so that every 10 minutes this runs.
I have written the programe which finds the file
%let folder=/sasdata/export_files;
/* Check for presence of a KNOWN Filr */
data _null_;
if fileexist("&folder./abc.csv") then
call symputx('file_found','1');
else
call symputx('file_found','0');
run;
%put FILE_Found = &file_found;
Now I need to write a contnuation of this program if FILE_FOUND = 1 the read the file. After completing the file I need to Schedule this Program.
The two Option we have is
1. Write this SAS Program and Schedule
2. Write a UNIX program in SAS EG ( X Command ) and schedule the same as Cron Job.
Coudl you please let me know how I can complete this and which option suites here better?
Thanks !!
Anumol Antony
I'd prefer to write the program so it can run in batch, and then use cron to run it every 10 minutes.
For convenience you can create a shell script that handles the SAS execution, and use that in the crontab.
I'm not familiar with chrone but with other scheduler (Control-M).
Anyhow you can do it in same job, Here is the sas program:
%macro check;
%let folder=/sasdata/export_files;
/* Check for presence of a KNOWN File */
data _null_;
if fileexist("&folder./abc.csv") then
call symputx('SYSCC',0);
else
call symputx('SYSCC',1);
run;
%if &syscc = 0 %then %do;
%include " .. program to read the control file etc. ...";
%end;
%mend check;
%check;
Dear All,
Can anyone give us pointesr in acheiving the requirement using X Commans in SAS EG. We need to write a program in SAS EG using X Commands which should read a file from a particular location and archive the files to another location.
Thanks and Regards,
Anumol Antony
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.