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

Hi All,

()

Can anybody explain the use and difference between macro variable (&mvar) and macro variable in quotation ("&mvar").

Regards

Anand

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Macro just generates text that SAS can then interpret as command, the same as if you had typed them into the source code.

So it depends on how the value of the macro variable is being used.

For example if you are generating an assignment statement inside of a data step you might have something like:

x = &myvar ;

In that case MYVAR should be a valid expression such as a variable name or even a function call or an equation.

But if you want to store the value of the macro variable MYVAR into a character variable then you would need to put it in quotes the same as you would an other literal string.

char = "&myvar" ;

If you are using the macro variable value in pure macro logic code then normally you would NOT want the quotes as to macro the quotes are just more characters (as compared to in normal SAS syntax where quotes are used to represent literal values instead of variable references).   So in macro logic if you want to test if MYVAR held the value FRED you would write

%IF (&myvar = FRED) %THEN ....

But in a SAS IF or WHERE statement you would need quotes.

IF ("&myvar" = "FRED") %THEN

Also when calling functions in macro code do not include the quotes, again because you do NOT need them to distinguish the syntax, in macro everything is just a stream of characters.

%let first_word = %scan(&myvar,1);

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Macro just generates text that SAS can then interpret as command, the same as if you had typed them into the source code.

So it depends on how the value of the macro variable is being used.

For example if you are generating an assignment statement inside of a data step you might have something like:

x = &myvar ;

In that case MYVAR should be a valid expression such as a variable name or even a function call or an equation.

But if you want to store the value of the macro variable MYVAR into a character variable then you would need to put it in quotes the same as you would an other literal string.

char = "&myvar" ;

If you are using the macro variable value in pure macro logic code then normally you would NOT want the quotes as to macro the quotes are just more characters (as compared to in normal SAS syntax where quotes are used to represent literal values instead of variable references).   So in macro logic if you want to test if MYVAR held the value FRED you would write

%IF (&myvar = FRED) %THEN ....

But in a SAS IF or WHERE statement you would need quotes.

IF ("&myvar" = "FRED") %THEN

Also when calling functions in macro code do not include the quotes, again because you do NOT need them to distinguish the syntax, in macro everything is just a stream of characters.

%let first_word = %scan(&myvar,1);

AnandSahu
Calcite | Level 5

Thanks Tom for providing this valuable explanation.


Regards

Anand

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
  • 2 replies
  • 456 views
  • 1 like
  • 2 in conversation