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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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