<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry in Advanced Programming</title>
    <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970861#M353</link>
    <description>&lt;P&gt;Why do you think the code does not work properly?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By now (480 posts by you) we really should not need to&amp;nbsp;&lt;U&gt;ask&lt;/U&gt; for the log and further information.&lt;/P&gt;</description>
    <pubDate>Wed, 16 Jul 2025 11:03:28 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2025-07-16T11:03:28Z</dc:date>
    <item>
      <title>code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970860#M352</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&lt;SPAN data-teams="true"&gt;Is there any code that checks if a lock is available on the audit table, if not, wait for n seconds and then retries, and so on.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN data-teams="true"&gt;I tried below code but doesn't look working properly.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro try_lock(base_table=, audit_table=, sleep_interval=5, max_retries=12);
 %local i lock_status;

 %let i = 1;
 %let lock_status = 1; /* non-zero means lock failed */

 %do %while(&amp;amp;lock_status ne 0 and &amp;amp;i le &amp;amp;max_retries);

   /* Attempt to lock the base audit table */
   lock &amp;amp;base_table;
   %let lock_status = &amp;amp;syslckrc;

   %if &amp;amp;lock_status ne 0 %then %do;
     %put NOTE: Lock attempt &amp;amp;i failed for &amp;amp;base_table. Sleeping for &amp;amp;sleep_interval seconds...;
     data _null_;
       call sleep(&amp;amp;sleep_interval, 1);
     run;
   %end;

   %let i = %eval(&amp;amp;i + 1);
 %end;

 %if &amp;amp;lock_status = 0 %then %do;
   %put NOTE: Successfully locked &amp;amp;base_table after %eval(&amp;amp;i - 1) attempt(s). Appending &amp;amp;audit_table...;

   proc append base=&amp;amp;base_table data=&amp;amp;audit_table force;
   run;

   unlock &amp;amp;base_table;
 %end;
 %else %do;
   %put ERROR: Unable to acquire lock on &amp;amp;base_table after &amp;amp;max_retries attempts.;
 %end;

%mend try_lock;
%try_lock(
 base_table=audit_master,
 audit_table=audit_temp_01,
 sleep_interval=10,
 max_retries=18
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Jul 2025 10:59:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970860#M352</guid>
      <dc:creator>sathya66</dc:creator>
      <dc:date>2025-07-16T10:59:08Z</dc:date>
    </item>
    <item>
      <title>Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970861#M353</link>
      <description>&lt;P&gt;Why do you think the code does not work properly?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By now (480 posts by you) we really should not need to&amp;nbsp;&lt;U&gt;ask&lt;/U&gt; for the log and further information.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jul 2025 11:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970861#M353</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-07-16T11:03:28Z</dc:date>
    </item>
    <item>
      <title>Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970862#M354</link>
      <description>&lt;P&gt;There is no such stuff like UNLOCK..., there is LOCK data.set CLEAR;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068/show-comments/false" target="_blank"&gt;Maxim 1.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data work.class;
  set sashelp.class;
  stop;
run;

%macro try_lock(base_table=, audit_table=, sleep_interval=5, max_retries=12);
 %local i lock_status;

 %let i = 1;
 %let lock_status = 1; /* non-zero means lock failed */

 %do %while(&amp;amp;lock_status ne 0 and &amp;amp;i le &amp;amp;max_retries);

   /* Attempt to lock the base audit table */
   lock &amp;amp;base_table;
   %let lock_status = &amp;amp;syslckrc;

   %if &amp;amp;lock_status ne 0 %then %do;
     %put NOTE: Lock attempt &amp;amp;i failed for &amp;amp;base_table. Sleeping for &amp;amp;sleep_interval seconds...;
     data _null_;
       call sleep(&amp;amp;sleep_interval, 1);
     run;
   %end;

   %let i = %eval(&amp;amp;i + 1);
 %end;

 %if &amp;amp;lock_status = 0 %then %do;
   %put NOTE: Successfully locked &amp;amp;base_table after %eval(&amp;amp;i - 1) attempt(s). Appending &amp;amp;audit_table...;

   proc append base=&amp;amp;base_table data=&amp;amp;audit_table force;
   run;

   lock &amp;amp;base_table clear;
 %end;
 %else %do;
   %put ERROR: Unable to acquire lock on &amp;amp;base_table after &amp;amp;max_retries attempts.;
 %end;

%mend try_lock;
%try_lock(
 base_table=work.class,
 audit_table=sashelp.class,
 sleep_interval=10,
 max_retries=18
)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Jul 2025 12:15:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970862#M354</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-16T12:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970863#M355</link>
      <description>&lt;P&gt;Sorry,&lt;/P&gt;
&lt;P&gt;think I need to use&amp;nbsp;&amp;nbsp;lock &amp;amp;base_table clear; instead of&amp;nbsp;&amp;nbsp;unlock &amp;amp;base_table ;&lt;BR /&gt;but if I schedule it via LSF. job is exiting due to below lock ERROR , how can we mask these lock ERRORs only . see the log below&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;1         %macro try_lock(base_table=, audit_table=, sleep_interval=5, max_retries=12);
32          %local i lock_status;
33         
34          %let i = 1;
35          %let lock_status = 1; /* non-zero means lock failed */
36         
37          %do %while(&amp;amp;lock_status ne 0 and &amp;amp;i le &amp;amp;max_retries);
38         
39            /* Attempt to lock the base audit table */
40         
41            lock &amp;amp;base_table;
42         
43         
44            %let lock_status = &amp;amp;syslckrc;
45         
46            %if &amp;amp;lock_status ne 0 %then %do;
47              %put NOTE: Lock attempt &amp;amp;i failed for &amp;amp;base_table. Sleeping for &amp;amp;sleep_interval seconds...;
48              data _null_;
49                call sleep(&amp;amp;sleep_interval, 1);
50              run;
51            %end;
52         
53            %let i = %eval(&amp;amp;i + 1);
54          %end;
2                                                          The SAS System                       Wednesday, July 16, 2025 01:09:00 PM

55         
56          %if &amp;amp;lock_status = 0 %then %do;
57            %put NOTE: Successfully locked &amp;amp;base_table after %eval(&amp;amp;i - 1) attempt(s). Appending &amp;amp;audit_table...;
58         
59            proc append base=&amp;amp;base_table data=&amp;amp;audit_table force;
60            run;
61         
62         
63            lock &amp;amp;base_table clear;
64         
65         
66          %end;
67          %else %do;
68            %put ERROR: Unable to acquire lock on &amp;amp;base_table after &amp;amp;max_retries attempts.;
69          %end;
70         
71         %mend try_lock;
72         %try_lock(
73          base_table=test.audit_master,
74          audit_table=test.audit_temp_01,
75          sleep_interval=10,
76          max_retries=18
77         );
ERROR: A lock is not available for TEST.AUDIT_MASTER.DATA.
ERROR: Lock held by process 130544.
NOTE: Lock attempt 1 failed for test.audit_master Sleeping for 10 seconds...

NOTE: DATA statement used (Total process time):
      real time           10.00 seconds
      cpu time            0.00 seconds
      

ERROR: A lock is not available for TEST.AUDIT_MASTER.DATA.
ERROR: Lock held by process 130544.
NOTE: Lock attempt 2 failed for test.audit_master Sleeping for 10 seconds...

NOTE: DATA statement used (Total process time):
      real time           10.00 seconds
      cpu time            0.00 seconds
      

NOTE: TEST.AUDIT_MASTER.DATA is now locked for exclusive access by you.
NOTE: Successfully locked test.audit_master after 3 attempt(s). Appending test.audit_temp_01..

NOTE: Appending TEST.AUDIT_TEMP_01 to TEST.AUDIT_MASTER.
NOTE: There were 19 observations read from the data set TEST.AUDIT_TEMP_01.
NOTE: 19 observations added.
NOTE: The data set TEST.AUDIT_MASTER has 152 observations and 5 variables.
NOTE: PROCEDURE APPEND used (Total process time):
      real time           0.07 seconds
      cpu time            0.00 seconds
      

NOTE: TEST.AUDIT_MASTER.DATA is no longer locked by you.&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Jul 2025 12:15:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970863#M355</guid>
      <dc:creator>sathya66</dc:creator>
      <dc:date>2025-07-16T12:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970864#M356</link>
      <description>&lt;P&gt;BTW. read this article:&amp;nbsp;&lt;A href="https://www.lexjansen.com/wuss/2015/60_Final_Paper_PDF.pdf" target="_blank"&gt;https://www.lexjansen.com/wuss/2015/60_Final_Paper_PDF.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Interesting approach to locking.&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jul 2025 12:17:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970864#M356</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-07-16T12:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970865#M357</link>
      <description>&lt;P&gt;Definitely read Troy's paper that yabwon linked to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looks like there is also a NOMSG option for the lock statement that could be helpful.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jul 2025 12:36:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970865#M357</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2025-07-16T12:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970874#M358</link>
      <description>&lt;P&gt;What is it that LSF is testing that is causing the job to fail?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is it actually reading the SAS log and seeing the ERROR: lines?&amp;nbsp; If so then perhaps re-route the SASlog using PROC PRINTTO.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or is it checking the return code from the SAS process?&amp;nbsp; If so what is the return code and can't you just change that with your SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or did it perhaps set the system option ERRORABEND and that is causing the SAS job to stop at the first lock failure?&amp;nbsp; If so then just change the system option before trying to get the lock.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jul 2025 15:24:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970874#M358</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-07-16T15:24:24Z</dc:date>
    </item>
    <item>
      <title>Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970936#M359</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/93352"&gt;@sathya66&lt;/a&gt;&amp;nbsp;I've tried to use use this %try_lock macro in the past but even after tweaking it got never to a fool-proof solution that only relies on SAS code. In a Unix/Linux environment you could use flock for such purposes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With SAS what always worked for me both under Windows and Unix/Linux is libname option filelockwait. If you don't need to wait for more than 10 minutes then that's what I'd be going for.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DL class="xisDoc-listTermDef"&gt;
&lt;DT id="p05hy8aspe32q5n1u2qs53l7n1kt" class="xisDoc-term"&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/hostunx/p0bnp6asvws4don1jxyxe9cc91wb.htm#p1hdlx60r0vkh2n1m8xcp22eolpb" target="_self"&gt;FILELOCKWAIT=&lt;EM class="xisDoc-userSuppliedValue"&gt;n&lt;/EM&gt;&lt;/A&gt;&lt;/DT&gt;
&lt;DD class="xisDoc-definition"&gt;
&lt;P class="xisDoc-paragraph"&gt;specifies the number of seconds SAS waits for a locked file to become available to another process.&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;If the locked file is released before the number of seconds specified by&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;n&lt;/EM&gt;, then SAS locks the file for the current process and continues. If the file is still locked when the number of seconds has been reached, then SAS writes a locked-file error to the log and the DATA step fails.&lt;/P&gt;
&lt;P class="xisDoc-paragraph"&gt;The valid values range from 0 to 600. The default value is 0.&lt;/P&gt;
&lt;/DD&gt;
&lt;/DL&gt;</description>
      <pubDate>Thu, 17 Jul 2025 00:14:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970936#M359</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-07-17T00:14:23Z</dc:date>
    </item>
    <item>
      <title>Re: code that checks if a lock is available on the table, if not, wait for n seconds and retry</title>
      <link>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970964#M360</link>
      <description>I have added NOMSG option to the lock statement and worked.&lt;BR /&gt; lock &amp;amp;base_table NOMSG;</description>
      <pubDate>Thu, 17 Jul 2025 06:50:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Advanced-Programming/code-that-checks-if-a-lock-is-available-on-the-table-if-not-wait/m-p/970964#M360</guid>
      <dc:creator>sathya66</dc:creator>
      <dc:date>2025-07-17T06:50:56Z</dc:date>
    </item>
  </channel>
</rss>

