DATA Step, Macro, Functions and more

call a macro end with semicolon?

Reply
Regular Contributor
Posts: 241

call a macro end with semicolon?

Hello,

 

Could anyone tell me what is the difference of add semicolon or not after call a macro?

 

Thanks!

 

%macro test;

       %put a;

%mend test;

 

%test;

%test

PROC Star
Posts: 7,487

Re: call a macro end with semicolon?

Posted in reply to GeorgeSAS

In your example, it wouldn't make much of a difference .. other than you are typing a character (the semi-colon) that isn't needed. You can find a nice explanation, including cases where it would make a BIG difference, at: https://communities.sas.com/t5/Base-SAS-Programming/To-semicolon-or-not-to-semicolon/td-p/60777

 

Art, CEO, AnalystFinder.com

 

Trusted Advisor
Posts: 1,137

Re: call a macro end with semicolon?

[ Edited ]
Posted in reply to GeorgeSAS

you don't need a semicolon at the end of the above macro because the very last statement (%put a; ) has a semicolon in it, so adding a semicolon to %test; would essentially be the same as typing a;;

Thanks,
Jag
Super User
Posts: 5,516

Re: call a macro end with semicolon?

Posted in reply to GeorgeSAS

The semicolon is not needed.  If you add it, it's like adding a semicolon in the middle of your program.  For example, consider:

 

%macro test;

proc print;

%mend test;

 

%test 

run;

 

%test;

run;

 

The top macro call generates:

 

proc print;

run;

 

The second generates:

 

proc print;;

run;

 

The extra semicolon is the one that was added when calling the macro.  Most of the time, an extra semicolon won't hurt anything.  But here are some cases when it will:

 

  • When the macro call generates just a portion of a SAS statement
  • When a macro is called repeatedly, and generates an ELSE statement 

In short, adding the semicolon when you call a macro adds the semicolon to the resulting program.  Most of the time an extra semicolon is just a null statement and gets ignored.  But there are cases when an extra semicolon causes problems.

PROC Star
Posts: 1,760

Re: call a macro end with semicolon?

[ Edited ]
Posted in reply to GeorgeSAS

Adding a semicolon means you add a semicolon in your code.

 

Even if unneeded (which is common), it generally won't do any damage [but it could be a problem in the wrong place, for example in something like:  where VAR1 = %get_value(param) group by VAR2 ], but often makes no difference.

 

The colour parser in SAS EG and SAS Desktop editors can get confused if the semicolon is missing, and that's reason I usually add it when I can, even if unneeded.

 

I'd rather not, but the benefit of better colour parsing outweighs the cost of an extraneous semicolon imho.

 

Super User
Super User
Posts: 7,977

Re: call a macro end with semicolon?

Posted in reply to GeorgeSAS

Personally I find this comes under the same heading as the dot at the end of macro varaibles, and the run; at the end of procedures.  Are they needed, not all the time, does it make code more readable yes.  Therefore I would always finish a macro variable with a dot:

&macrovariable.

And I would always put the semi-colon in, and always put run; after each procedure.  I don't see any beenfit to not doing this, however there are various downsides, such as coloring in the editor not working, code being less readable, and such like.  It also brings up question with people learning when they have seen:

&macrovariable

And then think they can get away with it anywhere (this is a question we see here fairly often):

file="\\abc\&macrovariable.csv";

Doesn't do what you thing as there is no dot in the resolved text.  

There are instances where not using the semicolo is valid, for instance if I have a macro that resolves to a result then:

x=%domymacro(num=5)-20;

Is fine as the macro resolves say to 100 in this case then 100-20; is fine, but 100;-20; is not.

PROC Star
Posts: 7,487

Re: call a macro end with semicolon?

@RW9: you said: And I would always put the semi-colon in

 

Didn't you really mean to say: And I would always NOT put the semi-colon in

 

Art, CEO, AnalystFinder.com

 

Super User
Super User
Posts: 7,977

Re: call a macro end with semicolon?

Sorry, I don't follow.  I meant that I would always have the code as:

%macro test;

       %put a;

%mend test;

 

%test;

 

With the semicolon after the macro invocation, except such times as when the macro is used to resolve to a value, in the case of:

x=%domymacro(num=5)-20;

This is the only even where I wouldn't have a semicolon as it results in invalid SAS code.

PROC Star
Posts: 7,487

Re: call a macro end with semicolon?

With the disclaimer you added .. I agree!

 

Art, CEO, AnalystFinder.com

 

Ask a Question
Discussion stats
  • 8 replies
  • 214 views
  • 1 like
  • 6 in conversation