BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ssll
Fluorite | Level 6

Hi there,

 

The following codes has generated an error message "Nesting of %IF statements in open code is not supported. %IF ignored. Skipping to next %END statement."   But if I deleted the red part of the codes (i.e., the 3 lines of codes starting from %else %if  ... %then %do)  , then the codes worked fine and generated the correct answer.

 

I also changed part of the problematic codes to (&current_month_num=9 or &current_month_num=10), and then changed it to (&current_month_num=10). None of them work.

 

Can you please point out my mistake, and suggest a workaround solution?  Any help or any comments is highly appreciated.  Thanks for your help!

 

%let current_month_num = %sysfunc(month(%sysfunc(today())));
%if (&current_month_num=1 or &current_month_num=2 ) %then %do;
%put no report will be generated;
%end;
%else %if (&current_month_num=9 or &current_month_num=8 or &current_month_num=7) %then %do;
%put quarter to date comparison report;
%end;
%else %do;
%put not in Q3 and not in Jan or Feb;
%end;

1 ACCEPTED SOLUTION

Accepted Solutions
ssll
Fluorite | Level 6

Hi PaigeMiller, you are right, I have tested putting those codes inside a macro, and it worked and generated the correct output.  Sorry for my late reply and thank you so much for your help!

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
Nesting of %IF statements in open code is not supported

 

Nested %IF is not supported in open code. The code in red is a nested %IF, in other words an %IF within another %IF. You cannot code this way outside of a macro.

 

Your code seems like it should run inside a macro.

--
Paige Miller
ssll
Fluorite | Level 6

Hi PaigeMiller, you are right, I have tested putting those codes inside a macro, and it worked and generated the correct output.  Sorry for my late reply and thank you so much for your help!

Tom
Super User Tom
Super User

Remove the nesting.  Just use three independent %IF blocks.

%let current_month_num = %sysfunc(month(%sysfunc(today())));

%if (&current_month_num=1 or &current_month_num=2 ) %then %do;
%put no report will be generated;
%end;

%if (&current_month_num=9 or &current_month_num=8 or &current_month_num=7) %then %do;
%put quarter to date comparison report;
%end;

%if (&current_month_num=3
  or &current_month_num=4
  or &current_month_num=5
  or &current_month_num=6
  or &current_month_num=10
  or &current_month_num=11
  or &current_month_num=12
) %do;
%put not in Q3 and not in Jan or Feb;
%end;
ssll
Fluorite | Level 6

Hi Tom, sorry for my late reply and thank you so much for  your reply. Yes I agree with you, using 3 independent %if blocks is a workaround solution cuz I just tested using 3 independent if blocks.  Thanks for your help!

FreelanceReinh
Jade | Level 19

Hi @ssll,

 

You could avoid the %IF-%THEN/%ELSE statements like this:

%let m=NNEEEERRREEE;
%let N=no report will be generated;
%let R=quarter to date comparison report;
%let E=not in Q3 and not in Jan or Feb;

%let c=%substr(&m,&current_month_num,1);
%put &&&c;

 

However, I suspect that you actually want to execute some reporting statements if &c=R. In this case you could at least avoid the nesting:

%let m=NNEEEERRREEE;
%let N=no report will be generated;
%let E=not in Q3 and not in Jan or Feb;

%let c=%substr(&m,&current_month_num,1);
%if &c=R %then %do;
  %inc 'C:\Temp\myQtoDreport.sas' / source;
%end;
%else %do;
  %put &&&c;
%end;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 366 views
  • 0 likes
  • 4 in conversation