Hi
There is a location where all log files of SAS Jobs gets stored (We have LINUX Env)
Also there will be .err files created wheen the job is failed.
The log files name will be like jobname_timestamp
Eg: Job name is J_LOAD
Timestamp -when the job runs
Log file name will be J_LOAD_20171115143219.log
Error file name will be : J_LOAD_20171115143219.err
So its like everytime a job runs , the log file gets created which will be unique because of timestamp.
I have to run a job based on the log file creation of its dependent job.
How to check which is the recent log file of a particular SAS Job (All I have is SAS Job name based on which I have to find recent log file ) I dont want to parse entire logs directory as there will lots of log files created.
Using SAS 9.4
Thanks in advance 🙂
Ok, so let me re-pose your question to you:
Without looking at the directory, tell me which is the latest file in that directory?
Do you see what I mean here, without assessing the files which are present then you have to supply some logical means to guess the latest file.
Me, if I you have access to command line interaction, I would do:
filename tmp pipe 'dir "s:\temp\J_LOAD*.log" /b /o-n'; data _null_; infile tmp; length fname $2000; input fname $; if _n_=1 then put fname; run;
What this does is run a bare directory listing back into your dataset, and uses the OS to sort the filenames (as you have done it correctly and put the date in a sortable format), I use the reverse sort /o means sort, n means by name, and the - means reverse. Then the first observation read is the one you want.
Hi.
Thanks for your reply. But there will be many logs of the same SAS job (if there are multiple reruns) . I want to take the recent log of a SAS Job. And there will be many logs of different SAS Jobs.
I just gave an example of a log file. Its like below:
J_ABC_20171115143219.log
J_LOAD_20171115143019.log
J_LOAD_20171115143219.log
J_ABC_20171114143219.log
What I want is if I want to serach for recent log of J_LOAD SAS Job, then output will be J_LOAD_20171115143219.log which is the recent log.
I do have access to command line interaction
And have you tried what I posted, which does what you ask and puts to the log the latest filename sorted by filename?
Hi
Can this be used for Linux as well?
Afraid I don't use Linux so can't tell you exactly, but Linux does have the ls command line code which can do similar, so you would need to change this line:
filename tmp pipe 'dir "s:\temp\J_LOAD*.log" /b /o-n';
To match what runs on your OS. The principal is exactly the same.
Maybe (just guessing here, -r is reverse order i know that):
filename tmp pipe 'ls "//temp/J_LOAD*.log" -r';
You can find help on the ls command here:
https://www.tecmint.com/15-basic-ls-command-examples-in-linux/
Hi
I will check and update if it works here 🙂 Thanks 🙂
Since you have a nicely sorting timestamp in your filename, this should do:
%let loc=/temp;
%let jobname=J_LOAD;
filename oscmd pipe "cd &loc.; ls &jobname.*.log|sort|tail -1";
data _null_;
infile oscmd;
input;
call symput('last_job_log',trim(_infile_));
run;
Depending on your needs, you might want to operate on the contents of _infile_ (extract the timestamp, remove the filename extension).
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.