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

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

1 ACCEPTED SOLUTION

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

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

4 REPLIES 4
Kurt_Bremser
Super User

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 */

 

dastan
Fluorite | Level 6

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

 

 

Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1099 views
  • 1 like
  • 3 in conversation