&mvar Vs "&mvar" in sas

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 81
Accepted Solution

&mvar Vs "&mvar" in sas

Hi All,

()

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

Regards

Anand


Accepted Solutions
Solution
‎08-23-2013 02:26 PM
Super User
Super User
Posts: 6,502

Re: &mvar Vs "&mvar" in sas

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


All Replies
Solution
‎08-23-2013 02:26 PM
Super User
Super User
Posts: 6,502

Re: &mvar Vs "&mvar" in sas

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);

Frequent Contributor
Posts: 81

Re: &mvar Vs "&mvar" in sas

Thanks Tom for providing this valuable explanation.


Regards

Anand

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 205 views
  • 1 like
  • 2 in conversation