DATA Step, Macro, Functions and more

Referring to a variable already defined

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Referring to a variable already defined

I was going through a SAS code and found the following ways of referencing a variable.

  1. &variable_name .
  2. &variable_name

I know that the first way of referencing is the normal way of how we refer to a defined variable in sas.

But, could you please explain how does the second way(without period after the variable name) is different from the first way


Accepted Solutions
Solution
‎07-03-2017 07:49 AM
Super User
Super User
Posts: 7,988

Re: Referring to a variable already defined

The dot indicates to the processor that it has come to the end of the macro variable name.  Picture is as this:

% - this character starts the macro or macro name

<name> - this is a SAS valid name for the macro variable

. - this finishes the macro name:

%<name>.

 

Now it is possible to omit the . and roll the closing sequence over to a closing item, this could be a semicolon, it could be a space etc. So:

%<name>;

However, that isn't good practice, it is always advisable to specifically state where a macro variable name finishes as this aids in reading the code.  There are instances for example where the dot is absolutely required, so no need to be lazy and not put it in all the time, for instance:

%let fname=abc;

filename test "c:\&fname.xlsx";

filename test2 "c:\&fname..xlsx";

Examine the above in the first filename once this has been through the macro pre-processor will look like:
filename test "c:\abcxlsx";

The second

filename test "c:\abc.xlsx";

 

You can clearly see here that the dot is very necessary to indicate that the macro name ends, and then there is a dot as part of the file extension.  Missing dot is just sloppy coding, like leaving off run; statements at the end of blocks.

 

 

 

View solution in original post


All Replies
Super User
Posts: 7,854

Re: Referring to a variable already defined

[ Edited ]

What you are talking about are not typical SAS variables (as contained in a dataset or used in a datastep), but SAS macro variables used by the macro preprocessor. Since such can be used as parts of words (ie as parts of dataset names or variable names), they are very often not separated by the blank or other separator characters (like a semicolon that ends a SAS statement). The dot then signals the end of the macro variable name to the preprocessor:

%let test=xxx;
%let testxxx=yyy;

%put &testxxx; /* willl render yyy in the log */
%put &test.xxx; /* will render xxxxxx in the log, contents of &test followed by literal xxx */

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 5

Re: Referring to a variable already defined

Posted in reply to KurtBremser

Thanks for your reply Kurt.

In the example you have stated, 

 

%put &testxxx.; 

/*
would also render yyy in the log right ?

so, what is the difference between &testxxx. and &testxxx (or when they should be used)

*/

Sorry, if this seems a silly question. As I'm new to SAS, I find it difficult to understand the application.

Solution
‎07-03-2017 07:49 AM
Super User
Super User
Posts: 7,988

Re: Referring to a variable already defined

The dot indicates to the processor that it has come to the end of the macro variable name.  Picture is as this:

% - this character starts the macro or macro name

<name> - this is a SAS valid name for the macro variable

. - this finishes the macro name:

%<name>.

 

Now it is possible to omit the . and roll the closing sequence over to a closing item, this could be a semicolon, it could be a space etc. So:

%<name>;

However, that isn't good practice, it is always advisable to specifically state where a macro variable name finishes as this aids in reading the code.  There are instances for example where the dot is absolutely required, so no need to be lazy and not put it in all the time, for instance:

%let fname=abc;

filename test "c:\&fname.xlsx";

filename test2 "c:\&fname..xlsx";

Examine the above in the first filename once this has been through the macro pre-processor will look like:
filename test "c:\abcxlsx";

The second

filename test "c:\abc.xlsx";

 

You can clearly see here that the dot is very necessary to indicate that the macro name ends, and then there is a dot as part of the file extension.  Missing dot is just sloppy coding, like leaving off run; statements at the end of blocks.

 

 

 

Super User
Posts: 7,854

Re: Referring to a variable already defined

As such, there would be no difference between &testxxx and &testxxx.

But if you wanted to directly append any text to the contents of testxxx, then you would have to use

&testxxx.other_text

to get

yyyother_text

For clarity, it is good to end all macro variable calls with a dot.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 134 views
  • 1 like
  • 3 in conversation