Sample 42583: How to stop processing your code if a certain condition is met
This sample suggests that the code be placed within a macro. By doing this, %RETURN can be used to halt the execution of the code if a certain condition is met. %RETURN causes normal termination of the currently executing macro. %RETURN was first available in SAS9.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
In this simple example, four data steps have been placed within a macro. If one of the data steps generates an error, then we do not want the subsequent steps to execute. This example uses %IF logic to evaluate the SYSERR macro variable to determine whether or not an error has occurred. If an error occurs, %RETURN is used to terminate execution of the macro.
Under the RESULTS tab, please note that the MLOGIC output in the log will show when the condition is true and that the macro has stopped execution.
options mprint symbolgen mlogic;
%macro test;
data one;
x=1;
run;
%if &syserr>4 %then %return;
data two;
xxxx;
run;
%if &syserr>4 %then %return;
data three;
x=3;
run;
%if &syserr>4 %then %return;
data four;
x=4;
run;
%if &syserr>4 %then %return;
%mend;
%test
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Please note the SYMBOLGEN ouput. It shows that the value of SYSERR has
resolved to 3000 which is greater than 4. The MLOGIC output shows how the
%IF condition was evaluated and that the macro has stopped execution.
338
339 options mprint symbolgen mlogic;
340 %macro test;
341
342 data one;
343 x=1;
344 run;
345 %if &syserr>4 %then %return;
346
347
348 data two;
349 xxxx;
350 run;
351 %if &syserr>4 %then %return;
352
353
354 data three;
355 x=3;
356 run;
357 %if &syserr>4 %then %return;
358
359
360 data four;
361 x=4;
362 run;
363 %if &syserr>4 %then %return;
364
365 %mend;
366
367 %test
MLOGIC(TEST): Beginning execution.
MPRINT(TEST): data one;
MPRINT(TEST): x=1;
MPRINT(TEST): run;
NOTE: The data set WORK.ONE has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
SYMBOLGEN: Macro variable SYSERR resolves to 0
MLOGIC(TEST): %IF condition &syserr>4 is FALSE
NOTE: Line generated by the invoked macro "TEST".
367 data two; xxxx; run;
----
180
MPRINT(TEST): data two;
MPRINT(TEST): xxxx;
MPRINT(TEST): run;
ERROR 180-322: Statement is not valid or it is used out of proper order.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.TWO may be incomplete. When this step was stopped there were 0
observations and 0 variables.
WARNING: Data set WORK.TWO was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
SYMBOLGEN: Macro variable SYSERR resolves to 3000
MLOGIC(TEST): %IF condition &syserr>4 is TRUE
MLOGIC(TEST): Ending execution.
This sample suggests that the code be placed within a macro and use %IF logic along with %RETURN to halt the macro's execution.
Date Modified: | 2012-02-02 15:41:36 |
Date Created: | 2011-03-03 12:21:16 |
Operating System and Release Information
SAS System | Base SAS | z/OS | 9.21 | | 9.2 TS2M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows XP 64-bit Edition | 9.21 | | 9.2 TS2M0 | |
Microsoft® Windows® for x64 | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows Server 2003 Datacenter Edition | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows Server 2003 Enterprise Edition | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows Server 2003 Standard Edition | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows Server 2003 for x64 | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows Server 2008 for x64 | 9.21 | | 9.2 TS2M0 | |
Microsoft Windows XP Professional | 9.21 | | 9.2 TS2M0 | |
Windows Vista | 9.21 | | 9.2 TS2M0 | |
Windows Vista for x64 | 9.21 | | 9.2 TS2M0 | |
64-bit Enabled AIX | 9.21 | | 9.2 TS2M0 | |
64-bit Enabled HP-UX | 9.21 | | 9.2 TS2M0 | |
64-bit Enabled Solaris | 9.21 | | 9.2 TS2M0 | |
HP-UX IPF | 9.21 | | 9.2 TS2M0 | |
Linux | 9.21 | | 9.2 TS2M0 | |
Linux for x64 | 9.21 | | 9.2 TS2M0 | |
OpenVMS on HP Integrity | 9.21 | | 9.2 TS2M0 | |
Solaris for x64 | 9.21 | | 9.2 TS2M0 | |