1 min read

To Exit or Not Exit, the SAS Ways

The tragedy of life is not that it ends so soon, but that we wait so long to begin it. –by W. M. Lewis

So, which is the exit style you prefer, in the following 3 macros (which are all valid SAS codes):

/#1 if branch/
%macro tragedy_of_life1(ds);
    %if %sysfunc(exist(&ds)) %then %do;
        proc print data=&ds noobs;
        run;
    %end;
    %else %do;
        %put ERROR: Dataset &ds not exist.;
    %end;
%mend;

/#2 abort/
%macro tragedy_of_life2(ds);
    %if %sysfunc(exist(&ds)) ^= 1 %then %do;
        %put ERROR: Dataset &ds not exist.;
        %abort cancel;   
    %end;

    proc print data=&ds noobs;
    run;
%mend;

/#3 goto/
%macro tragedy_of_life3(ds);
    %if %sysfunc(exist(&ds)) ^= 1 %then %do;
        %put ERROR: Dataset &ds not exist.;
        %goto iExit;   
    %end;

    proc print data=&ds noobs;
    run;

    %iExit:
%mend;

I personally use #2, %abort cancel. For goto in #3, it might not a good idea to use goto statement in any cases, so I avoid it as long as I can.

#1 is good, but you can imagine that the branches will grow dramatically if lots of exceptions are under checking. %abort cancel in #2 will best serve the purpose to isolate the blocks.