I know there exists the LEAVE statement for data step DO loops to terminate the current loop. I cannot find, however, documentation for a corresponding macro command.
I've tried %LEAVE
but that appears not to be defined.
%GOTO
or a DATA _NULL_
?I generally use a %GOTO for breaking out of a macro loop, if a %RETURN statement is not an option. And I sometimes also use a GOTO for leaving a datastep loop, because: the most CPU-efficient way of programming depends on being able to leave not just the current loop, but the loop surrounding that as well.
Until you can specify the level of loop that you want to break out of, there is no way around the occasional GOTO if you want to program with maximal efficiency and clarity. And this is true for SAS, C, C++ and any other language that uses loop constructs. Without GOTO you will have to do silly stuff like repeating code and checking for the same condition more than once.
If you want to break out of a macro completely, you can use %abort
(if you want to trigger an error) or %return
(if you don't). These won't help if you just want to break out of a loop and carry on with the rest of your macro, but you could potentially write a loop as a separate macro and call it inside a larger macro.
Another option is to use %do %while
or %do %until
and check your exit condition at the start of each loop, with extra %if-%then-%do-%end
blocks based on the same condition within the loop if you want to skip the rest of an iteration after a break condition is met halfway through.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With