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;
%end;
% IF &I=5 %AND J=3 %THEN "MOVE TO NEXT".
%end;
%mend;
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;
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;
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;
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;
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 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.
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!
Really thank you all very much!
HHC
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.