Tuesday, 17 May 2011

NOTE: Inadequate Mends


%macro article;
In the last couple of weeks I’ve been confronted with a lot of macro code. I love SAS Macro language (is there a cure for this?) but it drives me nuts when the name of the macro is not appended to the respective %MEND statement.

If the code contains two macro definitions, one after the other, it is very easy to accidentally overlook the end of the first and the beginning of the second, and thereby conclude that the first macro ends with the %MEND statement of the second macro. I know, I just did it!

And if the programmer has embedded macro definitions within macro definitions it can be even easier to get confused over what macro definition you’re looking at. In fact, I don’t support the idea of embedding definitions within definitions.

Every %MEND statement should have the name of the macro appended to it; what reason is there to not do this? Put this in your Coding Standards and enforce it rigidly in your Peer Reviews!

And while I'm on my soapbox, I will add that indentation is meant to show the structure of your code, i.e. blocks of lines that belong to a higher-order element. That being the case, lines of code within a macro definition should be indented from the %macro and %mend statements.
%mend article;