<?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: adding &amp;quot;return code&amp;quot; to a macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477889#M123166</link>
    <description>&lt;P&gt;Thank you! I don't have much experience working with infile (usually, I work with existing SAS datasets)&lt;/P&gt;&lt;P&gt;&amp;nbsp;and I appreciate you pointing out my omission of LRECL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Jul 2018 14:52:08 GMT</pubDate>
    <dc:creator>desertsp</dc:creator>
    <dc:date>2018-07-13T14:52:08Z</dc:date>
    <item>
      <title>adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477645#M123065</link>
      <description>&lt;P&gt;I have a macro which, when executed, sets a global variable (named &amp;amp;SearchResult)&amp;nbsp;to a 1 or 0. I would instead prefer&amp;nbsp;the macro itself evaluate to 1 or 0, without having to set a global variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BTW - this will be used for program control. The macro is checking whether a log file contains the word SUCCESS, and if so, the main program is allowed to continue processing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have now:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CheckFileForString(FilenameAndPath=,
                          SearchString=);&lt;BR /&gt;
   %global SearchResult; 
   %let SearchResult = 0; /* default unless the string is found */&lt;BR /&gt;&lt;BR /&gt;      /* scan FilenameAndPath for SearchString */
    filename finp ("&amp;amp;FilenameAndPath");
	data result;
	  infile finp filename = _filepath  truncover;
	  input a_line $1000.; /* read first 1000 bytes of each line */
	  if find(a_line,"&amp;amp;SearchString")  then do;&amp;nbsp; 	    
            call symput('SearchResult',1);  /* set the return code  */
		                                end;
	run;	
%mend;

%macro main_program;
   /* search a log file for the string SUCCESS */
 %CheckFileForString(FilenameAndPath=C:\TEMP\LOGFILE.LOG,
                     SearchString= SUCCESS);&lt;BR /&gt;
 %IF &amp;amp;SearchResult = 1 %THEN &lt;BR /&gt;
   %PUT NOTE: Processing can continue;&lt;BR /&gt;
 %ELSE 
   %PUT ERROR: The log file does not contain the word SUCCESS. Processing must halt;
%mend;

  %main_program;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is how I would prefer to setup %main_program. %CheckFileForString needs to evaluate to a 1 or 0, but I'm not sure how to&amp;nbsp;accomplish that.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro main_program;&lt;BR /&gt;
   /* check if a log file contains the word SUCCESS */
 %IF %CheckFileForString(FilenameAndPath=C:\TEMP\LCM_PYLOG.LOG,
                         SearchString= SUCCESS) = 1 %THEN &lt;BR /&gt;
   %PUT NOTE: Processing can continue;

 %ELSE 
   %PUT ERROR: The log file does not contain the word SUCCESS. Processing must halt;
%mend;

  %main_program;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 18:02:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477645#M123065</guid>
      <dc:creator>desertsp</dc:creator>
      <dc:date>2018-07-12T18:02:27Z</dc:date>
    </item>
    <item>
      <title>Re: adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477655#M123069</link>
      <description>&lt;P&gt;You don't want to use SYSCC?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can add this in the beginning of your code&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%Let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; syscc=0; &lt;/FONT&gt;&lt;FONT color="#008000" face="Courier New" size="3"&gt;** Operating Environment Condition Code **;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000" face="Courier New" size="3"&gt;And after each macro, you can add this into your code:&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#008000" face="Courier New" size="3"&gt;if &amp;amp;syscc&amp;gt;0 then your log message.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From SAS notes (Usage Note&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;I&gt;35553)&lt;/I&gt;&lt;/P&gt;&lt;P&gt;:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;SYSCC is a read/write automatic macro variable that enables you to reset the job condition code and to recover from conditions that prevent subsequent steps from running. The values for SYSCC are:&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;0 is no errors no warnings&lt;/LI&gt;&lt;LI&gt;4 is warnings&lt;/LI&gt;&lt;LI&gt;&amp;gt; 4 is an error occurred&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Thu, 12 Jul 2018 18:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477655#M123069</guid>
      <dc:creator>EEng</dc:creator>
      <dc:date>2018-07-12T18:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477667#M123072</link>
      <description>&lt;P&gt;I was not aware of SYSCC - it sounds very useful for trapping unexpected errors. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you don't mind, could you please demonstrate how I would incorporate this logic? If I'm not mistaken, my %CheckFileForString macro does not generate an "error" per se, even if the string isn't found.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or are you simply suggesting that I replace my own &amp;amp;SearchResult with &amp;amp;SYSCC?&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 19:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477667#M123072</guid>
      <dc:creator>desertsp</dc:creator>
      <dc:date>2018-07-12T19:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477707#M123088</link>
      <description>&lt;P&gt;Here is one way to expose a single value without a global macro variable:&lt;/P&gt;
&lt;PRE&gt;%macro dummy (value);
   %if &amp;amp;value=10 %then %let result=1;
   %else %let result=0;
   &amp;amp;result
%mend;

%macro main_program;
   /* search a log file for the string SUCCESS */
 
 %IF %dummy(10) = 1 %THEN 
   %PUT NOTE: Processing can continue;
 %ELSE 
   %PUT ERROR: The value is not 10. Processing must halt;
%mend;

%main_program;&lt;/PRE&gt;
&lt;P&gt;The local macro variable result exposed as a single statement without any ; to end the statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test other values with something like:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#ff0080" face="SAS Monospace" size="2"&gt;%put&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;&amp;nbsp;%dummy(5);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;You might want to include a LRECL parameter on our INFILE statement.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 22:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477707#M123088</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-07-12T22:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477765#M123112</link>
      <description>&lt;P&gt;Macros returning values can't use data-steps or procs, only macro-code is allowed. In the following example sashelp.class is not printed, but the code is returned to the calling macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dummy;
   %local z;
   %let z = 100;
  
   proc print data=sashelp.class;
   run;

   &amp;amp;z.

%mend;

%macro testbox;
   %local result;
   %let result = %dummy;
   %put -----------;
   %put &amp;amp;result.;
   %put -----------;
%mend;

%testbox;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Jul 2018 04:20:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477765#M123112</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-07-13T04:20:01Z</dc:date>
    </item>
    <item>
      <title>Re: adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477805#M123129</link>
      <description>&lt;P&gt;So, you may want to combine what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;have suggested:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Avoid SAS code in macro CheckFileForString.&lt;/LI&gt;
&lt;LI&gt;Let the&amp;nbsp;&lt;SPAN&gt;macro code end with a (local) macro variable reference.&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;SPAN&gt;The result, a (preliminary) function-style version of&amp;nbsp;CheckFileForString, could look something like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro CheckFileForString(FilenameAndPath=,
                          SearchString=);
%local SearchResult filrf rc fid c;
%let SearchResult = 0; /* default unless the string is found */
%let filrf=srcf;
%let rc=%sysfunc(filename(filrf, &amp;amp;FilenameAndPath));
%let fid=%sysfunc(fopen(&amp;amp;filrf));
%if &amp;amp;fid &amp;gt; 0 %then %do;
  %do %while(%sysfunc(fread(&amp;amp;fid))=0);
    %let rc=%sysfunc(fget(&amp;amp;fid, c, 1000)); /* read first 1000 bytes of each line */
    %if %index(%superq(c), &amp;amp;SearchString) %then %let SearchResult=1; /* set the return code */
  %end;
  %let rc=%sysfunc(fclose(&amp;amp;fid));
%end;
%let rc=%sysfunc(filename(filrf));
&amp;amp;SearchResult
%mend CheckFileForString;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Jul 2018 08:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477805#M123129</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-07-13T08:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477884#M123163</link>
      <description>&lt;P&gt;Thank you - I see you also improved how the macro works with the file, which I appreciate!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 14:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477884#M123163</guid>
      <dc:creator>desertsp</dc:creator>
      <dc:date>2018-07-13T14:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477889#M123166</link>
      <description>&lt;P&gt;Thank you! I don't have much experience working with infile (usually, I work with existing SAS datasets)&lt;/P&gt;&lt;P&gt;&amp;nbsp;and I appreciate you pointing out my omission of LRECL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 14:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477889#M123166</guid>
      <dc:creator>desertsp</dc:creator>
      <dc:date>2018-07-13T14:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: adding "return code" to a macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477894#M123169</link>
      <description>&lt;P&gt;I definitely need to learn more about when macros resolve to "text" and when they resolve to "executetable code". Your example helps with that understanding. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jul 2018 14:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-quot-return-code-quot-to-a-macro/m-p/477894#M123169</guid>
      <dc:creator>desertsp</dc:creator>
      <dc:date>2018-07-13T14:58:41Z</dc:date>
    </item>
  </channel>
</rss>

