Hi All,
()
Can anybody explain the use and difference between macro variable (&mvar) and macro variable in quotation ("&mvar").
Regards
Anand
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);
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);
Thanks Tom for providing this valuable explanation.
Regards
Anand
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.
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.