<?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: Error : Nesting of %IF statements in open code is not supported. %IF ignored in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Error-Nesting-of-IF-statements-in-open-code-is-not-supported-IF/m-p/872561#M344725</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;'s solution to put your code into an actual macro is correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if I'm understanding what you're doing, you're basically taking a date and trying to increment or decrement it by some number of months.&amp;nbsp; In your code, you're doing it 'by hand' by treating the year and month as two different integers.&amp;nbsp; But SAS knows how to work with dates, and has a function, INTNX, which will add a month to a date.&amp;nbsp; It's generally easier (and safer) to let SAS do date calculations rather than write your own.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can even use the macro language to call the INTNX function.&amp;nbsp; The code gets a bit ugly.&amp;nbsp; But basically, you use the INPUTN function to convert the string 202212 to a SAS date (22980 = 01Dec2022), then use INTNX to increment that date by 2 months (01Feb2023), then use PUTN to format that date as 202302.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let anomes_execucao = 202212;
%let min_maturidade = 2;

%let anomes_execucao_want= %sysfunc(putn(%sysfunc(intnx(month,%sysfunc(inputn(&amp;amp;anomes_execucao,yymmn6)),&amp;amp;min_maturidade)),yymmn6)) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;returns:&lt;/P&gt;
&lt;PRE&gt;1    %let anomes_execucao = 202212;
2    %let min_maturidade = 2;
3
4    %let anomes_execucao_want=
4  ! %sysfunc(putn(%sysfunc(intnx(month,%sysfunc(inputn(&amp;amp;anomes_execucao,yymmn6)),&amp;amp;min_maturidade)),yy
4  ! mmn6)) ;
5    %put &amp;amp;=anomes_execucao_want ;
ANOMES_EXECUCAO_WANT=202302
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Apr 2023 12:44:02 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2023-04-27T12:44:02Z</dc:date>
    <item>
      <title>Error : Nesting of %IF statements in open code is not supported. %IF ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Nesting-of-IF-statements-in-open-code-is-not-supported-IF/m-p/872531#M344715</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following code:&lt;/P&gt;&lt;PRE&gt;%let anomes_execucao = 202212;
%let min_maturidade = 2;

%put MIN_MATURIDADE=&amp;amp;MIN_MATURIDADE anomes_execucao=&amp;amp;anomes_execucao;

%if &amp;amp;min_maturidade eq . %then %do;
%let anomes_execucao=&amp;amp;anomes_execucao;
%put &amp;amp;=anomes_execucao;
%end;
%else %do;

%let year = %substr(&amp;amp;anomes_execucao,1,4);
%let month = %substr(&amp;amp;anomes_execucao,5,2);

%let new_month = %eval(&amp;amp;month - &amp;amp;min_maturidade);

%if &amp;amp;new_month &amp;gt; 12 %then %do;
%let year = %eval(&amp;amp;year + 1);
%let new_month = %eval(&amp;amp;new_month - 12);
%end;
%else %if &amp;amp;new_month &amp;lt; 1 %then %do;
%let year = %eval(&amp;amp;year - 1);
%let new_month = %eval(&amp;amp;new_month + 12);
%end;
%else %do;
%end;

%let anomes_execucao = %sysfunc(putn(&amp;amp;year*100 + &amp;amp;new_month, best6.));
%put &amp;amp;=anomes_execucao;
%end;&lt;/PRE&gt;&lt;P&gt;but i am getting this errors:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;     %if &amp;amp;min_maturidade eq . %then %do;
32         %let anomes_execucao=&amp;amp;anomes_execucao;
33         %put &amp;amp;=anomes_execucao;
34         %end;
35         %else %do;
36         
37         %let year = %substr(&amp;amp;anomes_execucao,1,4);
38         %let month = %substr(&amp;amp;anomes_execucao,5,2);
39         
40         %let new_month = %eval(&amp;amp;month - &amp;amp;min_maturidade);
41         
42         %if &amp;amp;new_month &amp;gt; 12 %then %do;
ERROR: Nesting of %IF statements in open code is not supported. %IF ignored.
ERROR: Skipping to next %END statement.
43         %let year = %eval(&amp;amp;year + 1);
44         %let new_month = %eval(&amp;amp;new_month - 12);
45         %end;
46         %else %if &amp;amp;new_month &amp;lt; 1 %then %do;
ERROR: The %ELSE statement is not valid in open code.
47         %let year = %eval(&amp;amp;year - 1);
48         %let new_month = %eval(&amp;amp;new_month + 12);
49         %end;
ERROR: The %END statement is not valid in open code.
50         %else %do;
ERROR: The %ELSE statement is not valid in open code.
2                                                          The SAS System                             11:44 Thursday, April 27, 2023

51         %end;
ERROR: The %END statement is not valid in open code.
52         
53         %let anomes_execucao = %sysfunc(putn(&amp;amp;year*100 + &amp;amp;new_month, best6.));
54         %put &amp;amp;=anomes_execucao;
ANOMES_EXECUCAO=202210
55         %end;
ERROR: The %END statement is not valid in open code.
56         &lt;/PRE&gt;&lt;P&gt;Can anyone help me understand what is wrong with my code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 11:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Nesting-of-IF-statements-in-open-code-is-not-supported-IF/m-p/872531#M344715</guid>
      <dc:creator>msf2021</dc:creator>
      <dc:date>2023-04-27T11:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Error : Nesting of %IF statements in open code is not supported. %IF ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Nesting-of-IF-statements-in-open-code-is-not-supported-IF/m-p/872533#M344716</link>
      <description>&lt;P&gt;The rather new open code macro syntax option is really only for basic things.&lt;/P&gt;
&lt;P&gt;Wrap your code into a macro. That's imho also better because it keeps many of your macro variables within local scope and though doesn't "pollute" your session.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need a macro variable outside of the macro then make it explicitly global via %global statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sample(
  anomes_execucao = 
  ,min_maturidade = 
  );
  %put MIN_MATURIDADE=&amp;amp;MIN_MATURIDADE anomes_execucao=&amp;amp;anomes_execucao;
  %if &amp;amp;min_maturidade eq . %then
    %do;
      %let anomes_execucao=&amp;amp;anomes_execucao;
      %put &amp;amp;=anomes_execucao;
    %end;
  %else
    %do;
      %let year = %substr(&amp;amp;anomes_execucao,1,4);
      %let month = %substr(&amp;amp;anomes_execucao,5,2);
      %let new_month = %eval(&amp;amp;month - &amp;amp;min_maturidade);

      %if &amp;amp;new_month &amp;gt; 12 %then
        %do;
          %let year = %eval(&amp;amp;year + 1);
          %let new_month = %eval(&amp;amp;new_month - 12);
        %end;
      %else %if &amp;amp;new_month &amp;lt; 1 %then
        %do;
          %let year = %eval(&amp;amp;year - 1);
          %let new_month = %eval(&amp;amp;new_month + 12);
        %end;
      %else
        %do;
        %end;

      %let anomes_execucao = %sysfunc(putn(&amp;amp;year*100 + &amp;amp;new_month, best6.));
      %put &amp;amp;=anomes_execucao;
    %end;
%mend;
%sample(
  anomes_execucao = 202212
  ,min_maturidade = 2
  );
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 12:35:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Nesting-of-IF-statements-in-open-code-is-not-supported-IF/m-p/872533#M344716</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-04-27T12:35:54Z</dc:date>
    </item>
    <item>
      <title>Re: Error : Nesting of %IF statements in open code is not supported. %IF ignored</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-Nesting-of-IF-statements-in-open-code-is-not-supported-IF/m-p/872561#M344725</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;'s solution to put your code into an actual macro is correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if I'm understanding what you're doing, you're basically taking a date and trying to increment or decrement it by some number of months.&amp;nbsp; In your code, you're doing it 'by hand' by treating the year and month as two different integers.&amp;nbsp; But SAS knows how to work with dates, and has a function, INTNX, which will add a month to a date.&amp;nbsp; It's generally easier (and safer) to let SAS do date calculations rather than write your own.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can even use the macro language to call the INTNX function.&amp;nbsp; The code gets a bit ugly.&amp;nbsp; But basically, you use the INPUTN function to convert the string 202212 to a SAS date (22980 = 01Dec2022), then use INTNX to increment that date by 2 months (01Feb2023), then use PUTN to format that date as 202302.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let anomes_execucao = 202212;
%let min_maturidade = 2;

%let anomes_execucao_want= %sysfunc(putn(%sysfunc(intnx(month,%sysfunc(inputn(&amp;amp;anomes_execucao,yymmn6)),&amp;amp;min_maturidade)),yymmn6)) ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;returns:&lt;/P&gt;
&lt;PRE&gt;1    %let anomes_execucao = 202212;
2    %let min_maturidade = 2;
3
4    %let anomes_execucao_want=
4  ! %sysfunc(putn(%sysfunc(intnx(month,%sysfunc(inputn(&amp;amp;anomes_execucao,yymmn6)),&amp;amp;min_maturidade)),yy
4  ! mmn6)) ;
5    %put &amp;amp;=anomes_execucao_want ;
ANOMES_EXECUCAO_WANT=202302
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Apr 2023 12:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-Nesting-of-IF-statements-in-open-code-is-not-supported-IF/m-p/872561#M344725</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2023-04-27T12:44:02Z</dc:date>
    </item>
  </channel>
</rss>

