BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AJ_Brien
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

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;

View solution in original post

5 REPLIES 5
Reeza
Super User

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!


 

SASKiwi
PROC Star

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;
PaigeMiller
Diamond | Level 26

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.

 

 

 

 

 

 

--
Paige Miller
AJ_Brien
Quartz | Level 8

Thank you, moving to a seperate location worked 🙂

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 2605 views
  • 4 likes
  • 4 in conversation