When is proper time to add dot . after macro variables?

Accepted Solution Solved
Reply
Super Contributor
Posts: 394
Accepted Solution

When is proper time to add dot . after macro variables?

Hello:

I was told a dot should be placed after macro variable.  I found sometime is needed, sometime isn't.   Please advice when should add .?  Is there any rule for that?  Thanks.

 

%let State=CA;

%let Year=2009;

 

For example, no matter I added dot . after '&state' or '&year'.  It looks the codes is working.  However, if the dot is missing after 'poverty&year..csv', the program stopped running due to the reference wasn't recognized.   So I am confused that when I should add the dot properly.  Thanks.

 

PROC IMPORT OUT=Poverty&state.&year

DATAFILE= "\\abc.org\project\Share\\ACS\&state.\&year.\Poverty&year..csv"

DBMS=CSV REPLACE;

GETNAMES=YES;

DATAROW=3;

RUN;

 

PROC IMPORT OUT=Poverty&state&year

DATAFILE= "\\abc.org\project\Share\ACS\&state\&year\Poverty&year..csv"

DBMS=CSV REPLACE;

GETNAMES=YES;

DATAROW=3;

RUN;

 

Below is Error message shown if the dot is missing in 'Poverty&year'

 

ERROR: Import unsuccessful. See SAS Log for details.

PROC IMPORT OUT=Poverty&state&year

DATAFILE= "\\abc.org\project\Share\ACS\&state\&year\Poverty&year.csv"

DBMS=CSV REPLACE;

GETNAMES=YES;

DATAROW=3;

RUN;


Accepted Solutions
Solution
‎10-16-2017 02:36 PM
Super User
Super User
Posts: 9,441

Re: When is proper time to add dot . after macro variables?

Right nice easy one this one.  A macro variable is ended by space, semicolon, dot or other special character per the tokenizer.  Therefore:

"Something &mymacro will work"

Because the space terminates the macro variable.  However you have to remember what the full expanded code would look like, so in a filename:

"&mymacro.xls"

Will resolve is mymacro is abc:

"abcxls"

As the dot terminates the macro variable and therefore is not in the final code, which would be wrong, hence you need two dots there:

"&mymacro..xls"

One to terminate, the other as part of the string.  So - and this is my opinion - in technical terms the dot might not always be needed, however as best programming practice I would always want to see the dot present, both for completeness, understanding of the code, and it also makes the macro variable highlight in a different color (well most cases) so its easy to see.  Like anything else codewise, you could put all your code on one line or type all in upcase, but is it good practice?

View solution in original post


All Replies
Super User
Posts: 9,924

Re: When is proper time to add dot . after macro variables?

[ Edited ]

It's never wrong to have the dot, but it can cause problems to not use it. Therefore you should make it your practice to always use it.

In your example with

%let year=2009;

 

Poverty&year..csv

will resolve to

Poverty2009.csv

while

Poverty&year.csv

will resolve to

Poverty2009csv

as the dot is consumed by the macro variable resolution.

 

As long as you keep asking yourself "should I use the dot", use it always. Only a programmer who is firm with macro programming can get away with using the dot selectively, but it is not considered good practice.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎10-16-2017 02:36 PM
Super User
Super User
Posts: 9,441

Re: When is proper time to add dot . after macro variables?

Right nice easy one this one.  A macro variable is ended by space, semicolon, dot or other special character per the tokenizer.  Therefore:

"Something &mymacro will work"

Because the space terminates the macro variable.  However you have to remember what the full expanded code would look like, so in a filename:

"&mymacro.xls"

Will resolve is mymacro is abc:

"abcxls"

As the dot terminates the macro variable and therefore is not in the final code, which would be wrong, hence you need two dots there:

"&mymacro..xls"

One to terminate, the other as part of the string.  So - and this is my opinion - in technical terms the dot might not always be needed, however as best programming practice I would always want to see the dot present, both for completeness, understanding of the code, and it also makes the macro variable highlight in a different color (well most cases) so its easy to see.  Like anything else codewise, you could put all your code on one line or type all in upcase, but is it good practice?

Super User
Posts: 9,924

Re: When is proper time to add dot . after macro variables?


RW9 wrote:
Like anything else codewise, you could put all your code on one line or type all in upcase, but is it good practice?

Like that.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 7,939

Re: When is proper time to add dot . after macro variables?

The dot is needed when the macro processor would not be able to detect the end of the macro variables name.  So if the macro variable name is followed immediately by a letter or digit that could possible be part of the macro variable name then you need to include the dot. So in this example we need the dot so that SAS knows you mean the macro variable PREFIX and not the macro variable PREFIXYZ.

%let prefix=X;
%put &prefix.YZ;

But also when the dot is there it becomes part of the macro variable reference.  That is what is causing confusion in your example. So if the text that you want to follow immediately after the macro variable's value starts with a dot then there also needs to be a dot to end the macro variable reference. Otherwise the macro processor will consider the dot part of the macro variable reference.

 

It does not hurt to always add the dot. 

Super Contributor
Posts: 394

Re: When is proper time to add dot . after macro variables?

Thank you so much for all of the thorough explanation. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 1191 views
  • 6 likes
  • 4 in conversation