DATA Step, Macro, Functions and more

Macro do loop - skip certain conditions

Accepted Solution Solved
Reply
Super Contributor
Posts: 371
Accepted Solution

Macro do loop - skip certain conditions

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;

 

 


Accepted Solutions
Solution
‎02-25-2017 11:05 PM
Super Contributor
Posts: 251

Re: Macro do loop - skip certain conditions

[ Edited ]

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


All Replies
Super User
Posts: 17,836

Re: Macro do loop - skip certain conditions

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;
Solution
‎02-25-2017 11:05 PM
Super Contributor
Posts: 251

Re: Macro do loop - skip certain conditions

[ Edited ]

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;
Super Contributor
Posts: 251

Re: Macro do loop - skip certain conditions

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;
Super User
Posts: 6,939

Re: Macro do loop - skip certain conditions

[ Edited ]

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 43

Re: Macro do loop - skip certain conditions

@KurtBremser Sorry but as i understand in your code, if then logic have to be "and" instead of "or"
Super User
Posts: 6,939

Re: Macro do loop - skip certain conditions


Yavuz wrote:
@KurtBremser 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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 43

Re: Macro do loop - skip certain conditions

Thanks a lot Sir,
Super Contributor
Posts: 251

Re: Macro do loop - skip certain conditions

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!

Super Contributor
Posts: 371

Re: Macro do loop - skip certain conditions

Really thank you all very much!

HHC

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 315 views
  • 9 likes
  • 5 in conversation