I am using the below macro to check for a lock every 15 seconds for 30 minutes.
%macro trylock(member=, timeout=);
%local starttime;
%let starttime = %sysfunc(datetime());
%do %until (&syslckrc = 0
or %sysevalf(%sysfunc(datetime()) > (&starttime + &timeout)));
%put trying to open ...;
%put trying lock ...;
lock &member;
%if &syslckrc ne 0 %then %let rc=%sysfunc(sleep(15));
%put syslckrc=&syslckrc;
%end;
%mend trylock;
if the file is locked the macro will keep running either for 30 minutes or until a lock is available. It will write to the log every time it attempts to lock the file
Below is an example
ERROR: A lock is not available for <Data>
syslckrc=70031
trying to open ...
trying lock ...
ERROR: A lock is not available for <Data>
syslckrc=70031
trying to open ...
trying lock ...
NOTE: <Data> is now locked for exclusive access by you.
syslckrc=0
17
Is there a way I can just get the last entry written to the log rather than all entries?
There is a NOMSG option for the LOCK statement that seems to do what you want:
Tip | NOMSG is useful if you want a LOCK statement resubmitted in a code loop until a lock is available, but you do want error messages displayed in the SAS log each time that an exclusive lock is not available. |
Thanks will investigate!
Why do you need to do this in the first place. Errors are in the log to tell you something is going wrong, therefore the best solution is to fix what is going wrong.
The code is trying to lock a dataset in a loop so it may have errors but eventually it may work, if it does work I don't need to know about the errors which is why I am looking for something that only writes the last try to the log rather all versions.
LIBNAME statement has an option FILELOCK something like . Maybe could apply to your scenario .
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.