As the first step in the decommissioning of sasCommunity.org the site has been converted to read-only mode.


Here are some tips for How to share your SAS knowledge with your professional network.


Macro Do Loop Continue or Leave

From sasCommunity
Jump to: navigation, search


As part of the decommissioning effort for sasCommunity.org this article/tip has been migrated to communities.sas.com.

The new home for this article/tip is Macro Do Loop Continue or Leave (https://communities.sas.com/t5/SAS-Communities-Library/Macro-Do-Loop-Continue-or-Leave/ta-p/475831)



This example shows data step and macro examples of the loop exit verbs:

  • continue: skip to 'end;' statement, where iteration happens
  • leave: exit loop
DATA _Null_;
do I = 1 to 3;
   put I= 'pre-test';
   if  I le 2 then continue;
   put I= 'post test';
   end;
put 'done continue: ' I= ;
 
do J = 1 to 3;
   put J= 'pre-test';
   if  J gt 2 then leave;
   put J= 'post test';
   end;
put 'done leave: ' J= ;
stop; run;
 
%Macro Do_Tests(i=,j=);
%do I = 1 %to 3;
   %put I=&I. pre-test;
   %if  &I le 2 %then %goto continue;
   %put I=&I. post test;
   %continue:
   %end;
%put done continue: I=&I. ;
 
%do J = 1 %to 3;
   %put J=&J. pre-test;
   %if  &J. gt 2 %then %goto leave;
   %put J=&J. post test;
   %end;
%leave:
%put done leave: J=&J. ;
%Mend;
%Do_Tests

References

--macro maven == the radical programmer 14:39, 13 October 2009 (UTC)