How To Use SAS Macros With Quotes

How to Use SAS Macros with Quotes

Ever found yourself scratching your head over SAS macros with quotes? You’re not alone. SAS macros can be a bit tricky, especially when quotes come into play.

In this guide, we’ll break down how to use SAS macros with quotes, step-by-step. By the end, you’ll be a pro at handling those pesky quotation marks.

Why Quotes Matter in SAS Macros

Before explaining how to use quotes in SAS macros, let’s first understand their purpose. Quotes are used to define character strings.

However, when used within macros, they can cause errors or unexpected results. This is because SAS macros treat quotes differently, depending on whether they are single or double quotes.

Single Quotes vs Double Quotes

In SAS, single quotes (' ') and double quotes (" ") are not the same. Here’s a quick rundown:

  • Single Quotes: Preserve the text within them. Macro variables are not resolved.
  • Double Quotes: Allow macro variables to resolve.

For example,

%let name = John;
%put 'Hello &name'; /* Output: Hello &name */
%put "Hello &name"; /* Output: Hello John */

The %Str and %NRStR Functions

When working with macro variables that contain special characters, using %STR and %NRSTR functions can be helpful.

  • %STR: Masks a character string during compilation.
  • %NRSR: Masks a character string during compilation and execution.
%let greeting = %str(Hello, "World");
%put &greeting;  /* Output: Hello, "World" */

When should you use %Str and %NRStr?

The %STR Function

The %STR function allows you to mask special characters during the compilation phase.

If the argument passed to %STR or %NRSTR contains unmatched single or double quotation marks or parentheses, they should be escaped by preceding each of these characters with a % sign.

Notation Description Example Quoted Value Stored
%’ unmatched single quotation mark %let myvar=%str(a%'); a'
%” unmatched double quotation mark %let myvar=%str(title %"first); title "first
%( unmatched left parenthesis %let myvar=%str (log%(12); log(12
%) unmatched right parenthesis %let myvar=%str (345%)); 345)
%macro print_quote_smiley(message, word_to_quote);
  %let complete_message = %str(&message "&word_to_quote");
  %put &complete_message;
%mend print_quote_smiley;

%print_quote_smiley(message=%str(Hello,have a), word_to_quote=nice day)
 Hello,have a "nice day"

The %NRSTR Function in Action

The %NRStr function is similar to %Str, but it also masks the string during the execution phase, not just the compilation phase.

%put "%NRSTR(&SYSDATE9)";
%put "&SYSDATE9";

Resolving macro variables within single quotes

To resolve a macro variable inside single quotes in SAS, you can use the %UNQUOTE function along with the %STR function, %nrbquote or the %tslit function. Here is a simple example:

%let myvar = hello;
%put %unquote(%str(%'&myvar%'));
%put %unquote(%nrbquote('&myvar'));
%put %unquote(%nrbquote('&myvar'));
 69         %put %unquote(%str(%'&myvar%'));
 70         %put %unquote(%nrbquote('&myvar'));
 71         %put %unquote(%nrbquote('&myvar'));

Every week we'll send you SAS tips and in-depth tutorials



Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.