While referencing a SAS macro variable, when do I need to enclose it in double quotes and when should I not? i.e When to use "&var_name" and when to use &var_name
2 Answers
Quote marks are not part of the macro language. The job of the macro language (most often) is to generate SAS code. Quote marks are part of the SAS code language. Therefore, you should use double quotes in the macro language wherever you would like to generate SAS code that has double quotes.
For example. Consider SAS DATA step assignment statement:
name="Mary" ;
The SAS language uses quotes to tell the data step compiler that Mary is a string value, not the name of a variable.
If you want to use macro language you could do:
%let name=Mary;
data want;
Name="&name" ;
run;
Or you could do:
%let name="Mary";
data want;
Name=&name;
Run;
In both cases, the quote marks have the same meaning to the data step compiler. They tell it that Mary is a text string. If you did not have quote marks, the compiler would see Mary as referring to a data step variable.
The macro language does not need quote marks to identify text string, because everything in the macro language is a text string. The macro language does not know about data step variables.

- 5,960
- 1
- 13
- 21
-
This is *mostly* correct - and how I think about it. Quotes do matter for [tokenization](http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#p030uw6k2s7wzyn18i9obutg923m.htm), particularly related to how [macro comments are parsed](http://support.sas.com/documentation/cdl/en/mcrolref/62978/HTML/default/viewer.htm#n17rxjs5x93mghn1mdxesvg78drx.htm). But for 99% of things, quote characters are irrelevant to the macro language. – Joe Aug 31 '15 at 20:34
-
1Perhaps you should append a sentence or two about the differences between single and double quotes? I know the question only asked about double-quotes but I feel this should be addressed for completeness. – Robert Penridge Aug 31 '15 at 22:39
It all depends on the value of your macro variable and what you want to do with it. As an example:
%let unQuoted = My string;
%let quoted = "My string";
data _null_;
isEqual = "&unQuoted." = "ed.;
put _all_;
run;
prints isEqual=1
, meaning true. If this does not help you, please be more specific in your question.

- 3,753
- 4
- 20
- 37