BookmarkSubscribeRSS Feed

Macro Do Loop Continue or Leave

Started ‎07-05-2018 by
Modified ‎07-05-2018 by
Views 10,482

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';
put 'done continue: ' I= ;
do J = 1 to 3;
   put J= 'pre-test';
   if  J gt 2 then leave;
   put J= 'post test';
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;
%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;
%put done leave: J=&J. ;


This was originally posted by Ron Fehd on

Version history
Last update:
‎07-05-2018 05:02 PM
Updated by:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags