BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

5 REPLIES 5
Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

Tom
Super User Tom
Super User

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. 

ybz12003
Rhodochrosite | Level 12

Thank you so much for all of the thorough explanation. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 20191 views
  • 11 likes
  • 4 in conversation