DATA Step, Macro, Functions and more

FILEEXIST vs SLEEP

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

FILEEXIST vs SLEEP

DATA

_NULL_

;

fex=

0

;

DO UNTIL(fex=1

) ;

if %sysfunc(fileexist("C:\temp\fix.txt")) then fex=1

;

else x=sleep(5

);

END

;

RUN

;

hi guys,

when file exist at start, it works!

why does this NOT work when the file does not exist and is created during the sleep loop?

it looks to me that fileexist is only executed once??

Regards,

Herman


Accepted Solutions
Solution
‎02-21-2012 12:56 PM
Super User
Posts: 5,518

FILEEXIST vs SLEEP

Herman,

The comments you've received are on the mark.  %SYSFUNC is not part of the DATA step.  It merely determines what statements appear in the DATA step.  So the DATA step sees:

if 0 then fex=1;

else x=sleep(5);

Creating the file will not change these statements, so SAS ought to cut short on the looping.  But removing %SYSFUNC might do the trick.

Good luck.

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: FILEEXIST vs SLEEP

When the file does not exist, your code will enter infinite loop, cause always fex=0. If file exists, yes, fileexist will exexute once, since there is no set statement, data _null_ will only run it once, no implied loop here.

Also, don't understand why you use %sysfunc, it seems to me unnecessary here, unless you need to deal with macro variables.

Regards,

Haikuo

Frequent Contributor
Posts: 89

FILEEXIST vs SLEEP

indeed it will go into a loop UNTIL the file is present in the folder, I'd hoped !!

Super User
Super User
Posts: 7,079

FILEEXIST vs SLEEP

SAS might have optimized your test out of the loop.

Try putting the filename in a variable so that SAS compiler doesn't view it as a constant.

Solution
‎02-21-2012 12:56 PM
Super User
Posts: 5,518

FILEEXIST vs SLEEP

Herman,

The comments you've received are on the mark.  %SYSFUNC is not part of the DATA step.  It merely determines what statements appear in the DATA step.  So the DATA step sees:

if 0 then fex=1;

else x=sleep(5);

Creating the file will not change these statements, so SAS ought to cut short on the looping.  But removing %SYSFUNC might do the trick.

Good luck.

SAS Employee
Posts: 104

FILEEXIST vs SLEEP

As others have said, you need to get the %SYSFUNC out of this code - it is not necessary.  You also do not need the variables FEX or X.  On my Windows system, this sample code worked just fine:

DATA _NULL_;
   DO UNTIL(fileexist("C:\temp\fix.txt"));
      call sleep(5);
   END;
RUN;
Trusted Advisor
Posts: 1,301

FILEEXIST vs SLEEP

On unix you would want to alter this just slightly since the default time setting for the sleep function is different than under windows (millisecond on unix vs. second in windows).

data _null_;

  do until(fileexist("/temp/fix.txt"));

   call sleep(5,1);

  end;

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 1272 views
  • 3 likes
  • 6 in conversation