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;
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?
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.
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?
@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.
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.
Thank you so much for all of the thorough explanation.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.