11-15-2017 03:00 AM - edited 11-15-2017 04:30 AM
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
11-15-2017 04:04 AM
11-15-2017 05:05 AM
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.
11-15-2017 05:18 AM - edited 11-15-2017 05:20 AM
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:
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
11-15-2017 05:37 AM
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:
11-15-2017 07:23 AM
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).