DATA Step, Macro, Functions and more

Macro variable

Reply
Super Contributor
Posts: 426

Macro variable

May I request someone to tell me when we should put a period(.) while calling a macro variable? In some cases, macro variable will get resolved if we just put ampersand (e.g. &test) and in other cases we need to put a period (e.g.&test. ).

 

Thanks for any advise you offer me!

Super User
Super User
Posts: 7,401

Re: Macro variable

Well, the SAS language will allow you to not put the period after a macro variable where it can clearly still identifiy the end of macro variable, e.g. a space.  Whilst this is possible I would strongly advise against doing it this way.  In my opionion nowadays, code readbility is far more important that saving a character space.  Code layout - e.g. indetations, well formed code - i.e. finisihng macro variable with period, finishing steps with run; or quit;, consistent casing - i.e. all code in lower case unless needed etc. will make your code far more readable.  If you ever have to pick up someone else's code you will know what I mean.  Long gone are the days where text space is limited and saving a tiny fraction of a second is more important than being able to maintain programs and keep transparancy on the code.

 

So TL;DR, in my opinion always use a period.  

Super Contributor
Posts: 426

Re: Macro variable

Thanks for your advise!

 

What would be the best example where I should use period and should not use period while resolving macro variable?

Super User
Super User
Posts: 7,401

Re: Macro variable

Thats really my point, there should never be an instance where you wouldn't put the period in.  Its not special functionality or anything, just laziness and sloppy coding.  Always put the period in.

Super User
Posts: 17,818

Re: Macro variable

[ Edited ]

Some examples of when it are needed, when the text needs a period. Since the period is attached to the macro variable you need two periods:

%let test=Macro Variable;
%put This macro variable has a period &test..;

%put How do I know whats text and whats my &test if it forms part of a file path?;
%put Path = "C:/temp/&testSaved.txt";
%put Path = "C:/temp/&test.Saved.txt";


The documentation also explains this fairly well.

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a001071889.htm

Super Contributor
Posts: 426

Re: Macro variable

Thanks for your example.

 

Since I don't have SAS enviornment to execute your code, may I know why you have placed two periods in following put statement? Won't it get resolved if you place one period?

 

%put This macro variable has a period &test..;
Super User
Super User
Posts: 7,401

Re: Macro variable

%put This macro variable has a period &test..;

The first period after test shows that the macro variable finishes, the second period is just plain text.  So assume test has the value "too", then it resolves to:
%put This macro variable has a period too.;

Super Contributor
Posts: 426

Re: Macro variable

Thanks for your explanation.

 

What if the macro variable test has a value (numeric) "1". Do we still want to put two periods like below while resolving?

 

%put This macro variable has a period &test..;
Super User
Super User
Posts: 7,401

Re: Macro variable

What do you want the ouput to look like?  If you set test to be "1" - remember all macro variables are text, then the put statement resolves to:

%put This macro variable has a period 1.;

 

 

Super User
Posts: 9,676

Re: Macro variable

"May I request someone to tell me when we should put a period(.) while calling a macro variable?"
Always. Put period after macro variable is always right.
Super User
Posts: 17,818

Re: Macro variable

You can download SASUE to learn on.
Ask a Question
Discussion stats
  • 10 replies
  • 259 views
  • 7 likes
  • 4 in conversation