<?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: macro code check, please (scanning for trigger file) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452741#M114285</link>
    <description>&lt;P&gt;I am not a great fan of using global macro variables to return values. In this case it is not necessary; by using %sysfunc(sleep()) you can avoid the data step, and change the code to a function style macro:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro WaitForFile(FilenameAndPath , /* file to check */
                   interval_time=1, /* how long to pause between checks, seconds */
                   timeout=10  /* total time before giving up, seconds */
                   );
          %local i rc;

          /* begin looping */
          %do i = 1 %to %sysevalf(&amp;amp;timeout/&amp;amp;interval_time,ceil);
             %if %sysfunc(fileexist(&amp;amp;FilenameAndPath)) %then  %do;
                %put NOTE: File exists (&amp;amp;FilenameAndPath);
                %*;1
                %return;
                %end;
             /* pause before trying again */
             %let rc=%sysfunc(sleep(&amp;amp;interval_time, 1));
             %end;  /* end of iteration */
%put WARNING: File does not exist (&amp;amp;FilenameAndpath);
0
%mend;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, you can use the macro in code like&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %WaitForFile(x:\test.txt) %then...&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;or&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let FileOK=%WaitForFile(x:\test.txt);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I changed the name of the macro to WaitForFile, as that seems to explain the function better. I dropped the all-caps style as that makes the code easier to read (I once heard that there is a scientific study to prove that!). And I simplified the code: No reason to put the number of intervals in a variable, it is only used once. Do not use %GOTO when you can use %RETURN. And I dropped the named parameter requirement (the "=") for the first parameter, as the meaning seems obvious when you see the macro used in code, and added defaults for the other two parameters.&lt;/P&gt;&lt;P&gt;One finale note: the macro comment "%*;" before the 1 is just a way of avoiding spurious blanks in the return value.&lt;/P&gt;</description>
    <pubDate>Tue, 10 Apr 2018 08:35:36 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-04-10T08:35:36Z</dc:date>
    <item>
      <title>macro code check, please (scanning for trigger file)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452693#M114269</link>
      <description>&lt;P&gt;I've built a simple macro which checks for the existence of a file (any kind), and then populates a global macro variable with a 0 or 1 to indicate the result.&amp;nbsp;It keeps testing for the file's existence at a user-specified interval until a user-specified timeout. Using SAS 9.4 running interactively on Windows.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Being that I'm relatively inexperienced writing macro code, I thought I'd run it by you folks for suggestions to make this more robust.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;By the way, I intend to use this following execution of a Python script called using the "X" command. The Python script sets a trigger file if the process completed successfully, and SAS will be looking for that trigger file. If that seems like a shortsighted approach, please let me know!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the macro code, and a call to check for a file every half second for 10 seconds.&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;%MACRO CHECK_FILE_EXISTANCE (FILENAMEANDPATH= , /* FILE TO CHECK */
                             INTERVAL_TIME= , /* HOW LONG TO PAUSE BETWEEN CHECKS, SECONDS */
                             TIMEOUT= ,  /* TOTAL TIME BEFORE GIVING UP, SECONDS */
                             RESULT_INDC_VAR= );  /* NAME OF MACRO VARIABLE TO POPULATE WITH BOOLEAN RESULT */

            /* DEFINE GLOBAL VAR TO HOLD OUTCOME */
          %GLOBAL &amp;amp;RESULT_INDC_VAR;

            /* DEFINE LOCAL MACRO VARS */
          %LOCAL MAX_INTERVALS;
          %LOCAL I;

            /* DETERMINE NUMBER OF ITERATIONS REQUIRED */
          %LET MAX_INTERVALS = %SYSEVALF(&amp;amp;TIMEOUT/&amp;amp;INTERVAL_TIME,CEIL);

          /* BEGIN LOOPING */
          %DO I = 1 %TO &amp;amp;MAX_INTERVALS;
                       
             %IF %SYSFUNC(FILEEXIST(&amp;amp;FILENAMEANDPATH)) %THEN  %DO;
                %LET &amp;amp;RESULT_INDC_VAR = 1; /* FILE EXISTS */
                %GOTO EXIT; 
                                                              %END;                       
             %ELSE        %DO;
                %LET &amp;amp;RESULT_INDC_VAR = 0; /* FILE WAS NOT FOUND (YET) */

                   /* PAUSE BEFORE TRYING AGAIN */
                 DATA _NULL_;
                   X = SLEEP(&amp;amp;INTERVAL_TIME, 1);
                 RUN;
                           %END;

           %END;  /* END OF ITERATION */


           %EXIT:

           %IF &amp;amp;RESULT_INDC_VAR = 1 %THEN  %DO;
              %PUT NOTE: FILE EXISTS (&amp;amp;FILENAMEANDPATH);
                                           %END;

           %IF &amp;amp;RESULT_INDC_VAR = 0 %THEN  %DO;
              %PUT WARNING: FILE DOES NOT EXIST (&amp;amp;FILENAMEANDPATH);
                                           %END;

%MEND;

%CHECK_FILE_EXISTANCE (FILENAMEANDPATH=C:\TESTFILE.TXT ,
                       INTERVAL_TIME= .5,
                       TIMEOUT= 10,
                       RESULT_INDC_VAR = FILE1_CHECK);

%PUT &amp;amp;FILE1_CHECK;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Apr 2018 23:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452693#M114269</guid>
      <dc:creator>desertsp</dc:creator>
      <dc:date>2018-04-09T23:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: macro code check, please (scanning for trigger file)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452730#M114279</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think it's fine of doing it this way, make sure to use triple ampersands to resolve the value of FILE1_CHECK&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;           %IF &amp;amp;&amp;amp;&amp;amp;RESULT_INDC_VAR = 1 %THEN  %DO;
              %PUT NOTE: FILE EXISTS (&amp;amp;FILENAMEANDPATH);
                                           %END;

           %IF &amp;amp;&amp;amp;&amp;amp;RESULT_INDC_VAR = 0 %THEN  %DO;
              %PUT WARNING: FILE DOES NOT EXIST (&amp;amp;FILENAMEANDPATH);
                                           %END;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is a little interesting paper explaining the situation:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A title="The Ampersand (&amp;amp;) Challenge, Single, Double or more" href="https://www.lexjansen.com/phuse/2015/cc/CC08.pdf" target="_blank"&gt;https://www.lexjansen.com/phuse/2015/cc/CC08.pdf&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Apr 2018 07:59:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452730#M114279</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2018-04-10T07:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: macro code check, please (scanning for trigger file)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452741#M114285</link>
      <description>&lt;P&gt;I am not a great fan of using global macro variables to return values. In this case it is not necessary; by using %sysfunc(sleep()) you can avoid the data step, and change the code to a function style macro:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro WaitForFile(FilenameAndPath , /* file to check */
                   interval_time=1, /* how long to pause between checks, seconds */
                   timeout=10  /* total time before giving up, seconds */
                   );
          %local i rc;

          /* begin looping */
          %do i = 1 %to %sysevalf(&amp;amp;timeout/&amp;amp;interval_time,ceil);
             %if %sysfunc(fileexist(&amp;amp;FilenameAndPath)) %then  %do;
                %put NOTE: File exists (&amp;amp;FilenameAndPath);
                %*;1
                %return;
                %end;
             /* pause before trying again */
             %let rc=%sysfunc(sleep(&amp;amp;interval_time, 1));
             %end;  /* end of iteration */
%put WARNING: File does not exist (&amp;amp;FilenameAndpath);
0
%mend;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, you can use the macro in code like&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if %WaitForFile(x:\test.txt) %then...&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;or&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let FileOK=%WaitForFile(x:\test.txt);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I changed the name of the macro to WaitForFile, as that seems to explain the function better. I dropped the all-caps style as that makes the code easier to read (I once heard that there is a scientific study to prove that!). And I simplified the code: No reason to put the number of intervals in a variable, it is only used once. Do not use %GOTO when you can use %RETURN. And I dropped the named parameter requirement (the "=") for the first parameter, as the meaning seems obvious when you see the macro used in code, and added defaults for the other two parameters.&lt;/P&gt;&lt;P&gt;One finale note: the macro comment "%*;" before the 1 is just a way of avoiding spurious blanks in the return value.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Apr 2018 08:35:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452741#M114285</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-04-10T08:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: macro code check, please (scanning for trigger file)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452743#M114287</link>
      <description>I would use %sysfunc (sleep())  instead of a data step. This way your macro generates no sas code and can be called from anywhere, including inside a data step.</description>
      <pubDate>Tue, 10 Apr 2018 08:43:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452743#M114287</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-04-10T08:43:28Z</dc:date>
    </item>
    <item>
      <title>Re: macro code check, please (scanning for trigger file)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452832#M114320</link>
      <description>&lt;P&gt;Hi, one little more thing, the right spelling is 'existence' with an -e&lt;/P&gt;</description>
      <pubDate>Tue, 10 Apr 2018 14:03:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452832#M114320</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2018-04-10T14:03:41Z</dc:date>
    </item>
    <item>
      <title>Re: macro code check, please (scanning for trigger file)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452841#M114322</link>
      <description>&lt;P&gt;Thank you very much - your solution is much better than mine!&lt;/P&gt;</description>
      <pubDate>Tue, 10 Apr 2018 14:29:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/macro-code-check-please-scanning-for-trigger-file/m-p/452841#M114322</guid>
      <dc:creator>desertsp</dc:creator>
      <dc:date>2018-04-10T14:29:27Z</dc:date>
    </item>
  </channel>
</rss>

