BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jaheuk
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Haikuo
Onyx | Level 15

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

Jaheuk
Obsidian | Level 7

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

Tom
Super User Tom
Super User

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.

Astounding
PROC Star

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.

SASJedi
SAS Super FREQ

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;
Check out my Jedi SAS Tricks for SAS Users
FriedEgg
SAS Employee

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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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