DATA Step, Macro, Functions and more

SAS loop

Reply
Contributor
Posts: 70

SAS loop

Hi There,

 

I have a requirement , where  I want to  execute the loop unless until the lock file is removed.

To elaborate 

 I will have a lock file system.lck in my path (Linux) and when I will start my job, it will keep on  checking  the lck file in each 1 minute interval unless until it is removed from that path.

Once the lck file removed , my job should proceed further.

 

I am trying to use Do while and do until loop in sas , but somehow I am not able catch the bug.

Here is my sample code.

 

%let testfile = '/export/home/xxxxx/sysout2.lck';

data _null_;
a=%sysfunc(fileexist(&testfile));
do while (a>0);
put " a= " a , please wait file is being used ;
x "sleep 30";
end;
x  "touch /export/home/xxxxx/sysout2.lck" ;  /* in each job, before creating the sysout2 file , i will create sysout2.lck*/
filename in "/export/home/xxxxx/test.txt";
data add;
infile in dsd;
length city $10;
input name $ age city $;
file sysout2;
put _infile_;
run;
x  "rm -rf /export/home/xxxxx/sysout2.lck" ;   /*Removing sysout2.lck , when my sysout2 file is created*/

 

Please help.

 

Thanks,

Suchi

Contributor
Posts: 54

Re: SAS loop

Posted in reply to suchismita

For your first data step, where are you getting the value "a" from?  Your "Do while" loop assumes that "a" is a variable in a dataset on the Program Data Vector (PDV). 

Super User
Posts: 10,574

Re: SAS loop

Posted in reply to suchismita

This is your mistake:

a=%sysfunc(fileexist(&testfile));

The macro function %sysfunc is evaluated before the data step runs, therefore a will never change. Just do

a = fileexist("&testfile");

instead. And put the statement into the loop, or a will also never change. And put a run; statement before the x that creates the file (to create a step boundary), or the x might execute too early.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 70

Re: SAS loop

Posted in reply to KurtBremser
Thanks,

I changed that to a = fileexist("&testfile");
but where should I put the 1 minute wait time ( x "sleep 60").
Can you please share me the sample the code that you are saying.
Super User
Posts: 10,574

Re: SAS loop

Posted in reply to suchismita

Instead of using the x statement, use

rc = sleep(30,1);

in the loop.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 70

Re: SAS loop

[ Edited ]
Posted in reply to KurtBremser

Hi KurtBremser,

Are you saying my code should look like below:

 

%let testfile = '/export/home/xxxxx/sysout2.lck';

data _null_;
a=fileexist(&testfile);
do while (a>0);
put " a= " a , please wait file is being used ;
rc = sleep(60,1);
end;
x "touch /export/home/xxxxx/sysout2.lck" ; /* in each job, before creating the sysout2 file , i will create sysout2.lck*/
filename in "/export/home/xxxxx/test.txt";
data add;
infile in dsd;
length city $10;
input name $ age city $;
file sysout2;
put _infile_;
run;
x "rm -rf /export/home/xxxxx/sysout2.lck" ; /*Removing sysout2.lck , when my sysout2 file is created*/

 

With this method, will my loop execute in each 60 secs interval and will keep on checking for /export/home/xxxxx/sysout2.lck file until it is removed?

 

Please suggest or modify my code as required.

Super User
Posts: 10,574

Re: SAS loop

Posted in reply to suchismita

Just take a look at your data step:

data _null_;
a=fileexist(&testfile);
do while (a>0);
  put " a= " a , please wait file is being used ;
  rc = sleep(60,1);
end;

Do you think that the loop will ever terminate, if the file exists before the step starts?

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 70

Re: SAS loop

[ Edited ]
Posted in reply to KurtBremser

Yes that is what happening. The loop is running and running in each 60 sec  even though I remove the file during middle of execution of the loop.

What I want is , the loop should continuously check existence of the  testfile and terminate on its own when file does not exist.

Like I want to repeat the loop iff the file exists otherwise  it should just print "file does not exist ,  create your own lock file"

 

Something like below, but it has some bug and not running as I want it to run. Can you please tell me what is wrong in below code.

%let testfile = '/export/home/xxxxx/sysout2.lck';

data _null_;
a=fileexist(&testfile);
if a>0 then goto loop;
loop:
put "please wait, as &testfile is being used" ;
rc = sleep(10,1);
else
put "File &testfile does not exist ,  create your own lock file";
end;

Regular Contributor
Posts: 226

Re: SAS loop

Posted in reply to suchismita
You need call fileexist in the loop, too.
Contributor
Posts: 70

Re: SAS loop

Posted in reply to error_prone

I did that earlier but it did not work. Can you please share me the code.

Contributor
Posts: 70

Re: SAS loop

Posted in reply to suchismita

Thanks all for valuable reply. I was able to find the solution for it. 

Ask a Question
Discussion stats
  • 10 replies
  • 224 views
  • 2 likes
  • 4 in conversation