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

Hi Everyone,

 

I run a macro like the below and I dont want to run for ,say i=5 and j=3. I dont know the command to skip this and move on to the next itterartion.

Could anyone help me with it?

 

Thanks so much.

Hhc

 

%macro comp;
%local i j;
%do i=1 %to 100;
  %do j=1 %to 100;

% IF &I=5 %AND J=3 %THEN "MOVE TO NEXT".

%end;
%end;

%mend;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
LaurieF
Barite | Level 11

First off, you want and not %and!

 

There's no equivalent of the continue and leave commands within macro code. But you can simulate it by:

%macro comp;
%local i j;
%do i=1 %to 100;
  %do j=1 %to 100;
     %IF &I=5 AND J=3 %THEN 
         %goto continue;
     %lotsOfOtherStuff;
     %continue:
     %end;
%end;

%mend comp;

%comp;

View solution in original post

9 REPLIES 9
Reeza
Super User

Use an else and skip it otherwise? Make sure you have enough %end, formatting can help with seeing that clearly. 

 

%macro comp;
    %local i j;

    %do i=1 %to 100;

        %do j=1 %to 100;

            %IF &I=5 %AND J=3 %THEN
                %do;
                %end;
            %else
                %do;
                    *rest of statements;
                %end;
        %end;
    %end;
%mend;
LaurieF
Barite | Level 11

First off, you want and not %and!

 

There's no equivalent of the continue and leave commands within macro code. But you can simulate it by:

%macro comp;
%local i j;
%do i=1 %to 100;
  %do j=1 %to 100;
     %IF &I=5 AND J=3 %THEN 
         %goto continue;
     %lotsOfOtherStuff;
     %continue:
     %end;
%end;

%mend comp;

%comp;
LaurieF
Barite | Level 11

Additionally, if you want to simulate the data step leave:

%macro comp;
%local i j;
%do I=1 %to 100;
  %do j=1 %to 100;
     %IF &I=5 AND J=3 %THEN 
         %goto leave;
     %lotsOfOtherStuff;
     %continue:
     %end;
%leave:
%Lotsmorestuff;
%end;

%mend comp;

%comp;
Kurt_Bremser
Super User

Reverse the condition:

%macro comp;
%local i j;
%do i=1 %to 100;
  %do j=1 %to 100;
    %if &i. ne 5 or &j. ne 3 %then %do;
      /* your code */
    %end;
  %end;
%end;
%mend;
Yavuz
Quartz | Level 8
@Kurt_Bremser Sorry but as i understand in your code, if then logic have to be "and" instead of "or"
Kurt_Bremser
Super User

@Yavuz wrote:
@Kurt_Bremser Sorry but as i understand in your code, if then logic have to be "and" instead of "or"

The OP wants that his %do loop body does not execute when i = 5 and j = 3,

Boolean math: not (x and y) = (not x) or (not y)

So I execute the loop body when i is not 5 or j is not 3.

Yavuz
Quartz | Level 8
Thanks a lot Sir,
LaurieF
Barite | Level 11

De Morgan's laws:

The negation of a conjunction is the disjunction of the negations; and the negation of a disjunction is the conjunction of the negations

 

or:

 

The nots of the ands is the ors of the nots; the nots of the ors is the ands of the nots.

 

I found that in an old SAS manual!

hhchenfx
Barite | Level 11

Really thank you all very much!

HHC

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 9 replies
  • 8154 views
  • 10 likes
  • 5 in conversation