## Macro do loop - skip certain conditions

Solved
Super Contributor
Posts: 506

# 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: 268

## 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;``````

All Replies
Super User
Posts: 24,010

## 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: 268

## 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: 268

## 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: 10,574

## 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
How to convert datasets to data steps
How to post code
Contributor
Posts: 54

## 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: 10,574

## 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
How to convert datasets to data steps
How to post code
Contributor
Posts: 54

## Re: Macro do loop - skip certain conditions

Thanks a lot Sir,
Super Contributor
Posts: 268

## 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: 506

## 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
• 1600 views
• 10 likes
• 5 in conversation