DATA Step, Macro, Functions and more

To semicolon or not to semicolon

Reply
Occasional Contributor
Posts: 12

To semicolon or not to semicolon

We are having a bit of a debate here ... I am an old time SAS programmer who was told that it is better to semicolon after a macro call ... %macroname; There is another thought that one must never semicolon ... %macroname.

My thinking is that if there were unbalanced semicolons at least this one would make sure that the macro would run. However, there is a note in a course note publication that states the opposite. I know there are times when if a macro call was in the middle of other code it may not be appropriate. I am just wondering about the call itself as stand alone code.

Your thoughts?
Thanks!
Super Contributor
Posts: 359

Re: To semicolon or not to semicolon

After 25 years, I almost always put in the semicolon. I have known programmers who put a semicolon in empty lines as spacers. In most cases it will not hurt, and helps locate things in debugging.

I also insist on hard returns and RUN; statements which I often see left out of code.
Valued Guide
Posts: 2,175

Re: To semicolon or not to semicolon

another old $0.02
sometimes the statement needs to be completed with a semicolon after the macro is called.
The macros which generate steps, usually, finish by generating a semicolon on a "run;" Those which generate only statements usually deliver statements completed by a semicolon. These macro invocations should not need a trailing semicolon.
I use a lot of "sub-statement" macros. They are really useful when a string or value needs to be retrieved. These "sub-statement macros" generate no semicolon so that they can be used, for example, in a calculation. It is normally important that these sub-statement macros are not immediately followed by a semicolon.
For example:
%put the message at %now() needs action ;

The semicolon ends the statement, not the macro invocation.

PeterC
Super Contributor
Posts: 359

Re: To semicolon or not to semicolon

Agreed. One really needs to know the context in which a macro is to be used. In the case of a sub-statement type of macr. I make it clear in the documentation if no semi-colon is to be used.
PROC Star
Posts: 1,567

Re: To semicolon or not to semicolon

The semicolon after the macro call will never be used, will it? (actually if it is part of the logic of the macro-generated code, it should be inside the macro itself).

The only reasonable case when a semicolon will be used after the macro call is when it closes a non-macro statement, as in
title %write_title(big, red, bold);
but then the semicolon is there to close the title statement, not as part of the macro call.

>I have known programmers who put a semicolon in empty lines as spacers.

Unless you are one of these and like extra and unused semicolons, a semicolon after a macro call makes no sense.

My 2 cents, to each their own and all that. Having an extra ; is not a big sin (though it sometimes hides bad understanding of what macros are).
Super Contributor
Posts: 474

Re: To semicolon or not to semicolon

Yep, agree with Chris. No need for any semicolon after the macro call.

But in my case, sometimes the habit of terminating any line of code with a semicolon takes over this understanding. Indeed, not a big sin.

Now, a situation that I found to be particularly confusing to understand for some sas coders is the need of a semicolon after the %if %then macro statement, say something like this:

%macro x(TAB,KEEP,VARS);

data _null_;
set &TAB %if &KEEP %then (keep = &VARS);;
run;

/* OR */

data _null_;
set &TAB %if &KEEP %then (keep = &VARS); end = _EOF;
run;

%mend x;

Cheers from Portugal.

Daniel Santos at www.cgd.pt.
Ask a Question
Discussion stats
  • 5 replies
  • 294 views
  • 1 like
  • 5 in conversation