Joshua Schlichting
Joshua Schlichting

Reputation: 3450

SAS E Guide 5.1 Macro Wrapper not working

I'm looking to conditionally exit a SAS program. Every corner of the internet is suggesting that I encapsulate my code in a macro wrapper, but when I do this, the code appears to no longer be 'recognized' by SAS (keywords are no longer blue). I haven't seen it written anywhere thus far, but I feel that maybe this feature isn't compatible with Enterprise Guide 5.1.

%macro wrapper;
PROC SQL;
    All this is just grey text, to include the PROC SQL and QUIT that should be blue
;QUIT;
%mend wrapper;

Ultimately, I want to have something like this in my code

IF date1 ^= date2 THEN ABORT(the entire program if possible)

I tried using %IF %THEN %GOTO, but it doesn't work unless the entire program is within a macro wrapper, which brings me here.

(I've tried the IF condtion to abort within a DATA step, but it just aborts that block of code, and not the program.)

I hope I've articulated this clearly. Can any SAS wizards out there help? Are there any alternative methods I haven't thought of that could apply to my main goal here?

Thanks everyone!

Upvotes: 0

Views: 141

Answers (3)

Robert Penridge
Robert Penridge

Reputation: 8513

This solution assumes you've saved the value of date1 and date2 into macro variables. If you need help doing so, let me know and I'll update this answer.

Using the %abort cancel; statement is my favorite way to conditionally abort the currently running program. Simply perform the test prior to your SQL code, there's no need to wrap all your code in a macro if the execution never makes it that far.

%macro exit_if_dates_dont_match;
  %if &date1 ^= &date2 %then %do;
    %abort cancel;
  %end;
%mend;
%exit_if_dates_dont_match;

Upvotes: 1

Joe
Joe

Reputation: 63424

What you're describing is a well known flaw in the SAS code highlighter. It's true in DM and in Enterprise Guide. Interestingly enough, the EG Autocompleter doesn't have that problem - it knows to autocomplete properly in that context - so I'm not sure why they haven't fixed it.

One way to fix it is to insert a 'dummy' macro inside your macro.

%macro your_macro;
   %macro fix_color; %mend fix_color;

   proc sql;
      your code;
   quit;
%mend your_macro;

Assuming you enter that in order, it will properly highlight everything in it.

As Tom notes, it doesn't mean the code won't work, but it does of course make it harder to read the code.

Now, to answer your larger question: how else could you do this?

In Enterprise Guide, you should be taking advantage of conditional code execution via the process flow.

Let's say you have two programs - one that runs if &date1. = &date2. and one that runs if they're note equal (say, it prints something out to a text file so you know it failed).

You start by going to the process flow, right clicking on one of the programs, and selecting 'Condition' -> 'Add'. Then you can add a condition, like for example:

First Condition Window

So now we tell it what to do if that's true. If true, run Program, and if false, run Program1. Those each have their own code that does whatever you want in that particular case (and hopefully have better names!).

enter image description here

Finally, we see the process flow, which shows the programs being conditionally run. You can of course have another program that runs before these two programs that sets date1 and date2. You see the small flag icons in the upper right of the programs' icons on the flow, indicating they're being conditionally run.

enter image description here

Upvotes: 1

Tom
Tom

Reputation: 51566

The editor is totally independent from the SAS execution. The color coding that you see in the editor is just the editor's attempt to understand the code. It should have no effect on whether or not the code works.

Upvotes: 2

Related Questions