Hello,
I have a huge piece of code which contains multiple data steps, macros, proc sqls, loops etc. such that this entire piece of code needs to run only if a certain condition is met.
data _null_;
now=today();
d=day(now);
put d;
if d in (30,31,1,2,3,4) then call symput ('run','N');
else call symput ('run','Y');
run;
%put &run.;
%macro selection;
[entire code: data steps, macros, proc sqls, loops etc. ]
%mend;
data _null_;
%if &run.='Y' %then %do;
%selection;
end;
This is the error that I get:
975 data _null_;
976 %if &run.='Y' %then %do;
ERROR: Nesting of %IF statements in open code is not supported. %IF ignored.
ERROR: Skipping to next %END statement.
977 %selection;
MLOGIC(SELECTION): Beginning execution.
Would there be a way to conditionally execute a huge piece of code?
Appreciate the help!
You don't need macro. Put your required code in a separate SAS program file then use %INCLUDE:
data _null_;
if <condition is true> then do;
call execute('%include "MyLargeProgram.sas";');
end;
run;
Separate your steps. Put all the code that needs to be run in a separate program.
Create your condition and only execute it if the condition was met. You can use %INCLUDE to run a program.
data _null_;
now=today();
d=day(now);
put d;
if d in (30,31,1,2,3,4) then call execute ('%include "path to sas program.sas" / lrecl=500');
run;
The program with the data set becomes your control program that executes the remaining code if necessary.
You can make this more convoluted if you'd like, look at macro logic:
UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...
@AJ_Brien wrote:
Hello,
I have a huge piece of code which contains multiple data steps, macros, proc sqls, loops etc. such that this entire piece of code needs to run only if a certain condition is met.
data _null_; now=today(); d=day(now); put d; if d in (30,31,1,2,3,4) then call symput ('run','N'); else call symput ('run','Y'); run; %put &run.;
%macro selection;
[entire code: data steps, macros, proc sqls, loops etc. ]
%mend;
data _null_;
%if &run.='Y' %then %do;
%selection;
end;This is the error that I get:
975 data _null_;
976 %if &run.='Y' %then %do;
ERROR: Nesting of %IF statements in open code is not supported. %IF ignored.
ERROR: Skipping to next %END statement.
977 %selection;
MLOGIC(SELECTION): Beginning execution.
Would there be a way to conditionally execute a huge piece of code?
Appreciate the help!
You don't need macro. Put your required code in a separate SAS program file then use %INCLUDE:
data _null_;
if <condition is true> then do;
call execute('%include "MyLargeProgram.sas";');
end;
run;
976 %if &run.='Y' %then %do;
Typically, the value of macro variables are not enclosed in quotes, and the way you are creating the macro variable &RUN via CALL SYMPUT, the value created for macro variable &RUN is Y (or N), which is not enclosed in quotes. Thus you are testing to see if the one-letter text string Y (the value of &RUN) is equal to the hard coded text string 'Y', and these are never equal.
Thus, you want
%if &run.=Y %then %do;
Also:
ERROR: Nesting of %IF statements in open code is not supported. %IF ignored.
You need to do this inside a macro or via CALL EXECUTE.
Thank you, moving to a seperate location worked 🙂
@AJ_Brien wrote:
Thank you, moving to a seperate location worked 🙂
I'm not sure anyone recommended "moving to a seperate location", so it's not clear what you are saying.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: