BookmarkSubscribeRSS Feed
GeorgeSAS
Lapis Lazuli | Level 10

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

8 REPLIES 8
art297
Opal | Level 21

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

 

Jagadishkatam
Amethyst | Level 16

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
Astounding
PROC Star

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.

ChrisNZ
Tourmaline | Level 20

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.

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

art297
Opal | Level 21

@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

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

art297
Opal | Level 21

With the disclaimer you added .. I agree!

 

Art, CEO, AnalystFinder.com

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 4650 views
  • 7 likes
  • 6 in conversation