Search code examples
sassas-macro

When to use double quotes while referencing a SAS macro variable


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


Solution

  • 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.