The SAS macro processor scans text before it reaches the SAS compiler and interprets certain characters as macro triggers
Characters such as semicolons, commas, parentheses, and the ampersand and percent signs all have special meaning to the macro processor
When these characters appear inside a macro call or a `%let` statement, SAS may misinterpret them as the end of a parameter, the start of a macro call, or a macro variable reference
Macro quoting functions tell SAS to treat these characters as plain text and not as triggers
The three most commonly used quoting functions are `%str`, `%nrstr`, and `%quote`
%STR - quote a text string at compile time
`%str` is applied at macro compile time, before the macro is called
It prevents the macro processor from interpreting special characters such as semicolons, commas, and parentheses as syntax
`%str` does not mask ampersands or percent signs — those are still resolved normally
The most common use is to include a semicolon or comma inside a `%let` value without ending the statement prematurely
SAS Log
The `%put` should print: `where_clause resolves to: year=2023 and department='Sales'`
Without `%str`, the single quotes around `Sales` would cause a macro error because unmatched quotes confuse the macro tokeniser
The value stored in `where_clause` can now be used safely inside a WHERE statement
SAS Log
SAS resolves `&where_clause` to the stored text, and the WHERE clause is syntactically correct
Confirm that `sales_2023` contains only the 2023 Sales row
Dataset View
Use %STR to pass a comma-separated list as a single parameter
Commas inside macro call arguments are treated as parameter separators by default
Wrapping the value in `%str` prevents the comma from being interpreted as a separator
This allows you to pass a list of values as a single parameter
SAS Log
The log should show the full three-variable list as a single value
If you called `%show_vars(year, amount, department)` without `%str`, SAS would raise an error because the macro only has one parameter
%NRSTR - quote text including ampersands and percent signs
`%nrstr` works like `%str`, but also masks ampersands and percent signs
This means macro variable references and macro calls inside `%nrstr` are not resolved — they are kept as literal text
This is useful when you need to store text that contains an ampersand or a macro call as a plain string rather than resolving it
SAS Log
The `%put` prints the literal text `&report_date` rather than trying to resolve `report_date` as a macro variable
Without `%nrstr`, SAS would attempt to resolve `&report_date` and produce a WARNING if that variable does not exist
SAS Log
The ampersand in `Version 1 & 2` is kept as a literal character rather than being interpreted as the start of a macro variable reference
Use `%nrstr` whenever your stored text must contain a literal ampersand or percent sign
%QUOTE - quote a value at macro execution time
`%quote` is applied at macro execution time, rather than at compile time
It is typically used inside a macro definition to quote a parameter value that was passed in at run time
This is useful when the value passed to a parameter contains special characters that should not be interpreted
`%quote` does resolve ampersands and percent signs — it only masks punctuation characters like semicolons and commas
SAS Log
`%quote(&val)` ensures the resolved value of `val` is treated as a plain string before comparison
This pattern is standard practice for `%if` conditions that compare a macro parameter to a literal value
The log should show `Value is Yes` for the first call, and `Value is not Yes` for the second call
%QUOTE with a parameter containing special characters
When a macro parameter value contains a comma or parenthesis, `%quote` prevents SAS from misinterpreting them
In the next example, a parameter contains a parenthesised range, which would normally confuse the macro parser
SAS Log
The parentheses in `(inclusive)` are safely masked and do not cause a macro syntax error
Note that `%str` was used in the macro call to pass the parentheses as part of the argument, and `%quote` inside the macro protects the resolved value further
Summary - when to use each quoting function
Use `%str` in `%let` statements and macro call arguments when the value contains semicolons, commas, unmatched quotes, or parentheses, but not ampersands or percent signs that should remain as literals
Use `%nrstr` when the value also contains ampersands or percent signs that must be stored as literal characters rather than resolved
Use `%quote` inside a macro definition to protect a parameter value at execution time, particularly in `%if` comparisons
All three functions are transparent at the data level — they affect only how the macro processor tokenises text, not the content of datasets or output
SAS Log
Key points to remember
`%str` quotes special characters at compile time and is the right choice for most `%let` values that contain commas, semicolons, or unmatched quotes
`%nrstr` additionally masks ampersands and percent signs, so they are not resolved — use it when you need a literal `&` or `%` in a stored string
`%quote` quotes at execution time and is commonly used inside `%if` conditions to protect resolved parameter values from being misinterpreted
Macro quoting never changes the actual content of the stored value — it only affects how the macro processor tokenises the text during scanning
When in doubt, test with `%put` to confirm what value is actually stored in a macro variable