<?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: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441187#M110323</link>
    <description>&lt;P&gt;Is it a timing issue?&lt;/P&gt;
&lt;P&gt;Looks like you have a data _NULL_ step that is reading the data set (so has a lock) and it is generating batch jobs (other SAS programs?).&amp;nbsp; If those programs start and try to write to that data set and the current data step has not finished closing it will be locked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you read/writing to the same dataset in multiple jobs?&lt;/P&gt;</description>
    <pubDate>Thu, 01 Mar 2018 13:51:25 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-03-01T13:51:25Z</dc:date>
    <item>
      <title>BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441144#M110314</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I try to automatize the whole process in customer environment so the code should be scheduled and works a batch job.&amp;nbsp;&lt;SPAN&gt;The following BATCH data set is updated everyday, but&amp;nbsp;it is unclear when the data set will be updated so we need to check&amp;nbsp; every half an hour. For this reason, I prepared the following code and scheduled by the help of WindowsTask scheduler for every half an hour.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data BATCH;
Length FINAL_DATE 8 ERROR 8;
Infile Datalines Missover;
Input FINAL_DATE ERROR ;
FORMAT FINAL_DATE  DATE9.;
Datalines;
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
;
LIBNAME PORT "C:\Users\...\Desktop";
Data PORT.STATUS;
Start_Date="Y";
Complete_Date="01MAR2018"D;
Format Complete_Date DATE9.;
Run;
Options dbidirectexec bufno=100 bufsize=128k;
LIBNAME SQL ODBC DATAsrc=proddb SCHEMA=sas USER=user PASSWORD="*******" INSERT_SQL=YES INSERTBUFF=32767 readbuff=10000 bulkload=yes dbcommit=10000;
%MACRO main_loop;
PROC SQL;
SELECT MAX(RESULT) INTO:errcheck FROM(SELECT *,
CASE WHEN FINAL_DATE="&amp;amp;SYSDATE9."D AND ERROR=0 THEN 0 ELSE 1 END AS RESULT
FROM BATCH);
QUIT;
%IF &amp;amp;errcheck %THEN %DO;      
DATA _NULL_;
ABORT CANCEL;
RUN;
%END;
%ELSE %DO;
DATA _NULL_;
SET PORT.STATUS;
IF Start_Date="Y" OR Complete_Date="&amp;amp;sysdate9."d THEN ABORT CANCEL;
ELSE Call system("...Batch.bat");
RUN;
%END;
%MEND;

%main_loop&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;When the above code executes the bat file the following code is triggered but I need to update PORT.STATUS data set because the code run time can take an hour so when the Windows Task Scheduler trigger the code it shouldn't re-execute the following code if it is already running. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The problem is if I execute the following code on Enterprise Guide, it works without an error but when it is triggered by Batch job it gives an LOCK error for PORT.STATUS.&amp;nbsp;&lt;/SPAN&gt;Actually, both Batch and EG session running on the same exact environment and I am trying to access the exact same data set and I am also pretty sure the PORT.STATUS data set does not open anywhere beacuse I'm the only who knows the PORT.STATUS data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;LIBNAME PORT "C:\Users\...\Desktop";/**/
DATA PORT.STATUS;/*It gives the LOCK error here*/
SET PORT.STATUS;
Start_Date="Y";
Run;
/*MAIN CODE IS HERE BUT I DO NOT TYPE IT BECAUSE IT CAN TAKE UP A LOT OF SPACE*/
DATA PORT.STATUS;
SET PORT.STATUS;
Complete_Date="&amp;amp;sysdate9."d;
format Complete_Date date9.;
Start_Date="N";
Run;
 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Firstly, do you have an idea what can be the reason for?&lt;/P&gt;
&lt;P&gt;Secondly, do you have an alternative solution to prevent the lock error?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 09:28:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441144#M110314</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2018-03-01T09:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441187#M110323</link>
      <description>&lt;P&gt;Is it a timing issue?&lt;/P&gt;
&lt;P&gt;Looks like you have a data _NULL_ step that is reading the data set (so has a lock) and it is generating batch jobs (other SAS programs?).&amp;nbsp; If those programs start and try to write to that data set and the current data step has not finished closing it will be locked.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are you read/writing to the same dataset in multiple jobs?&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 13:51:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441187#M110323</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-01T13:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441347#M110394</link>
      <description>&lt;P&gt;Before running the code check if the Dataset is locked or not. If it is locked then sleep for some time and again check the Lock on it until a specific timeout period.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro trylock(member=, timeout=);
%local starttime;
%let starttime = %sysfunc(datetime());
%DO %UNTIL (&amp;amp;syslckrc = 0 or %sysevalf(%sysfunc(datetime()) &amp;gt; (&amp;amp;starttime + &amp;amp;timeout)));
 lock &amp;amp;member NOMSG;
%if &amp;amp;syslckrc ne 0 %then %do;
			filename outbox email;
			DATA _NULL_;
			file outbox to=("suryakiran@abc.com") 
						cc=("suryakiran@abc.com")    
                        subject="Dataset Locked" ;
  
			PUT "Thanks";
			RUN;
			%end;
%if &amp;amp;syslckrc ne 0 %then %let rc=%sysfunc(sleep(1000*60*1));/* One Minute */
%END;
%mend trylock;
%trylock(member=MYWORK.TEST, timeout=3550);&lt;BR /&gt;&lt;BR /&gt;&amp;lt;My code&amp;gt;&lt;BR /&gt;LOCK MYWORK.TEST CLEAR;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This will check if there is a lock on the dataset. If there is a look then it will sleep for some time (1 min in above code) and send you a mail and loop until lock is released or timeout (1hr in above code 3550). If there is no lock then the dataset will be locked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Don't forget to clear the lock after your code is done.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;LOCK MYWORK.TEST CLEAR;&amp;nbsp;&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 20:01:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441347#M110394</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-01T20:01:11Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441400#M110423</link>
      <description>&lt;P&gt;I also try it by not using Data _NULL_ step but it still gives same error. Also I change&amp;nbsp; the some part of trigger code as below;&lt;/P&gt;
&lt;P&gt;Instead of calling another bat file, I prefer to call SAS code but it still gives same error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _NULL_;
SET PORT.STATUS;
IF Start_Date="Y" OR Complete_Date="&amp;amp;sysdate9."d THEN ABORT CANCEL;
ELSE %Include "...Main_Code.sas";
RUN;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You told me this -&amp;gt;&amp;nbsp;&lt;SPAN&gt;Why are you read/writing to the same dataset in multiple jobs?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I told,&amp;nbsp;&lt;SPAN&gt;Tthe BATCH data set is updated everyday, but&amp;nbsp;it is unclear when the data set will be updated so we need to check&amp;nbsp; every half an hour and the process should work just one time in a day.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Actually, I am open to listen your suggestions. Can you share me another possible structure for my process?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:08:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441400#M110423</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2018-03-01T22:08:33Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441401#M110424</link>
      <description>&lt;P&gt;Hello SuryaKiran,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your code, actually, it is little bit complicated is it possible for you to show me in a sample data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On the other hand, is it possible to kill the data set which is already open to prevent to Lock error? Any code for this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:11:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441401#M110424</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2018-03-01T22:11:01Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441408#M110429</link>
      <description>&lt;P&gt;You can try LOCK &amp;lt;Dataset&amp;gt; CLEAR; before your update query to the table. This will release the lock only if it is locked by you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Mar 2018 22:35:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441408#M110429</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-01T22:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441416#M110433</link>
      <description>&lt;P&gt;Adding comments for your understanding.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%PUT &amp;amp;SYSLCKRC.;
/* Unlock if the dataset need to be updated is locked by me previously */
LOCK MYWORK.TEST CLEAR;

%macro trylock(member=, timeout=);
%local starttime;
%let starttime = %sysfunc(datetime());/* Current time */
/* Loop until lock is released */
/* Initially &amp;amp;syslckrc value is 0 */
%DO %UNTIL (&amp;amp;syslckrc = 0 or %sysevalf(%sysfunc(datetime()) &amp;gt; (&amp;amp;starttime + &amp;amp;timeout)));
/* Try to lock the Dataset, If it is previously locked then &amp;amp;syslckrc value will be &amp;gt;0, Lets assume Its locked*/
lock &amp;amp;member NOMSG;
 /*Since &amp;amp;syslckrc &amp;gt;0 you will get email saying its locked. You can ignore this if you don't wish for email */
%if &amp;amp;syslckrc ne 0 %then %do;
			filename outbox email;
			DATA _NULL_;
			file outbox to=("suryakiran@abc.com") 
						cc=("suryakiran@abc.com")    
                        subject="Dataset Locked" ;
  
			PUT "Thanks";
			RUN;
			%end;
/* *Since &amp;amp;syslckrc &amp;gt;0 it will sleep for 1 min */
%if &amp;amp;syslckrc ne 0 %then %let rc=%sysfunc(sleep(1000*60*1));/* One Minute */ /* change 1 to 10 for 10 min sleep */
/* Since the &amp;amp;syslckrc value is &amp;gt;0 it will loop after sleep */
%END;
%mend trylock;

%trylock(member=MYWORK.TEST, timeout=3550);

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Mar 2018 23:04:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441416#M110433</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-01T23:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441482#M110460</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why the following statement gave an error in log? -&amp;gt;&amp;nbsp;&lt;FONT color="#FF0000"&gt;ERROR: MYWORK.TEST.DATA is not locked by you.&lt;/FONT&gt; Shouldn't it better if it would give a warning?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;LOCK MYWORK.TEST CLEAR;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And what exactly&amp;nbsp;&amp;amp;SYSLCKRC. macro do?&lt;/P&gt;
&lt;P&gt;And why&amp;nbsp;timeout=3550?&lt;/P&gt;
&lt;P&gt;This code will work for just one data set? -&amp;gt;&amp;nbsp;member=MYWORK.TEST&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't want to send an email to myself so I think I can ignore that part. If I put the following code at the begining of my code, it won't be locked, right?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%PUT &amp;amp;SYSLCKRC.;
/* Unlock if the dataset need to be updated is locked by me previously */
LOCK MYWORK.TEST CLEAR;
%macro trylock(member=, timeout=);
%local starttime;
%let starttime = %sysfunc(datetime());/* Current time */
/* Loop until lock is released */
/* Initially &amp;amp;syslckrc value is 0 */
%DO %UNTIL (&amp;amp;syslckrc = 0 or %sysevalf(%sysfunc(datetime()) &amp;gt; (&amp;amp;starttime + &amp;amp;timeout)));
/* Try to lock the Dataset, If it is previously locked then &amp;amp;syslckrc value will be &amp;gt;0, Lets assume Its locked*/
lock &amp;amp;member NOMSG;
/* *Since &amp;amp;syslckrc &amp;gt;0 it will sleep for 1 min */
%if &amp;amp;syslckrc ne 0 %then %let rc=%sysfunc(sleep(1000*60*1));/* One Minute */ /* change 1 to 10 for 10 min sleep */
/* Since the &amp;amp;syslckrc value is &amp;gt;0 it will loop after sleep */
%END;
%mend trylock;
%trylock(member=MYWORK.TEST, timeout=3550);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 07:57:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441482#M110460</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2018-03-02T07:57:48Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441589#M110495</link>
      <description>&lt;P&gt;Does anyone have a different opinion?&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 15:28:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441589#M110495</guid>
      <dc:creator>turcay</dc:creator>
      <dc:date>2018-03-02T15:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: BATCH Job Gives a Lock ERROR While Enterprise Guide Gives No Error for Same Code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441600#M110496</link>
      <description>&lt;P&gt;Place the LOCK PORT.STATUS CLEAR; in the 1st code that you run.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data BATCH;
Length FINAL_DATE 8 ERROR 8;
Infile Datalines Missover;
Input FINAL_DATE ERROR ;
FORMAT FINAL_DATE  DATE9.;
Datalines;
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
21244 0
;
LIBNAME PORT "C:\Users\...\Desktop";
Data PORT.STATUS;
Start_Date="Y";
Complete_Date="01MAR2018"D;
Format Complete_Date DATE9.;
Run;
Options dbidirectexec bufno=100 bufsize=128k;
LIBNAME SQL ODBC DATAsrc=proddb SCHEMA=sas USER=user PASSWORD="*******" INSERT_SQL=YES INSERTBUFF=32767 readbuff=10000 bulkload=yes dbcommit=10000;
%MACRO main_loop;
PROC SQL;
SELECT MAX(RESULT) INTO:errcheck FROM(SELECT *,
CASE WHEN FINAL_DATE="&amp;amp;SYSDATE9."D AND ERROR=0 THEN 0 ELSE 1 END AS RESULT
FROM BATCH);
QUIT;
%IF &amp;amp;errcheck %THEN %DO;      
DATA _NULL_;
ABORT CANCEL;
RUN;
%END;
%ELSE %DO;
DATA _NULL_;
SET PORT.STATUS;
IF Start_Date="Y" OR Complete_Date="&amp;amp;sysdate9."d THEN ABORT CANCEL;
ELSE Call system("...Batch.bat");
RUN;
%END;
%MEND;

%main_loop;
LOCK PORT.STATUS CLEAR;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now when the above program running your .bat file your triggering the next program (Try triggering after the .bat run completed).&lt;/P&gt;&lt;P&gt;So before the next program runs you need to check whether the lock is released or not. If the above program still running then the dataset is locked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%PUT &amp;amp;SYSLCKRC.;
/* Unlock if the dataset need to be updated is locked by me previously */

%macro trylock(member=, timeout=);
%local starttime;
%let starttime = %sysfunc(datetime());/* Current time */
/* Loop until lock is released */
/* Initially &amp;amp;syslckrc value is 0 */
%DO %UNTIL (&amp;amp;syslckrc = 0 or %sysevalf(%sysfunc(datetime()) &amp;gt; (&amp;amp;starttime + &amp;amp;timeout)));
/* Try to lock the Dataset, If it is previously locked then &amp;amp;syslckrc value will be &amp;gt;0, Lets assume Its locked*/
lock &amp;amp;member NOMSG;
/* *Since &amp;amp;syslckrc &amp;gt;0 it will sleep for 10 min */
%if &amp;amp;syslckrc ne 0 %then %let rc=%sysfunc(sleep(1000*60*10/*Minutes*/));
/* Since the &amp;amp;syslckrc value is &amp;gt;0 it will loop after sleep */
%END;
%mend trylock;

%trylock(member=PORT.STATUS, timeout=3600);
/* If you have another dataset and need to run the rest of the query only if the lock is released. */
%trylock(member=PORT.STATUS2, timeout=3600);

LIBNAME PORT "C:\Users\...\Desktop";/**/
DATA PORT.STATUS;/*It gives the LOCK error here*/
SET PORT.STATUS;
Start_Date="Y";
Run;
/*MAIN CODE IS HERE BUT I DO NOT TYPE IT BECAUSE IT CAN TAKE UP A LOT OF SPACE*/
DATA PORT.STATUS;
SET PORT.STATUS;
Complete_Date="&amp;amp;sysdate9."d;
format Complete_Date date9.;
Start_Date="N";
Run;
/* Release lock on all datasets */
LOCK PORT.STATUS CLEAR;
LOCK PORT.STATUS2 CLEAR;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;SYSLCKRC Automatic macro variable value will be 0 if the last lock statement is successful. &amp;gt;0 if the lock statement is not successful and &amp;lt;0 if warnings or note was written to log.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3550 Sec~1hr because I want to try for checking the lock for 1 hr ie. every 10 min until 1 hr. This is not a fixed number it's up to you how long you want to check for lock on the dataset depending on the previous job run time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note: If running in windows then remove 1000 in sleep sunction&lt;/P&gt;&lt;P&gt;i.e&amp;nbsp;&amp;nbsp;%if &amp;amp;syslckrc ne 0 %then %let rc=%sysfunc(sleep(&lt;STRONG&gt;60*10&lt;/STRONG&gt;/*Minutes*/));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can run the %trylock on any number of datasets you wish. It's a macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%trylock(member=PORT.STATUS, timeout=3600);&lt;/P&gt;&lt;P&gt;%trylock(member=PORT.STATUS2, timeout=3600);&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%trylock(member=PORT.STATUS3, timeout=3600); ........................so on&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 15:55:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/BATCH-Job-Gives-a-Lock-ERROR-While-Enterprise-Guide-Gives-No/m-p/441600#M110496</guid>
      <dc:creator>SuryaKiran</dc:creator>
      <dc:date>2018-03-02T15:55:49Z</dc:date>
    </item>
  </channel>
</rss>

