Your SAS programs, embedded in web apps and elsewhere


Posts: 0


This is probably the wrong category. But I'm running into SAS trouble with macros. I have text with an '&' that I want as text and not as a macro symbol. What should I do?

For example:
%let x="a&b";

where & is not referring to &b.

Any suggestions?
Posts: 8,864

Re: Macro

Posted in reply to deleted_user
Essentially, what you have to do is HIDE, PROTECT, or otherwise remove the normal meaning from the & -- the normal meaning of the & to SAS is as a macro trigger -- which sends your code with the & to the macro processor to resolve what SAS thinks is a symbolic reference to a macro variable. So when you submit:
10 %let x = "a&b";
WARNING: Apparent symbolic reference B not resolved.
11 %put =====> x= &x;
WARNING: Apparent symbolic reference B not resolved.
=====> x= "a&b"
you get the log as shown above. The macro facility went to look up &b, didn't find it when handling the %let, so you get a warning message. Then, when you go to use &b again (as in the %PUT), you will get the warning again, because every reference causes an unsuccessful lookup to the memory table where macro variables are stored (Global Symbol Table).

What you do, depends on whether you need the quotes to be double quotes or not. One method of hiding the normal/macro meaning of an & is to enclose it in single quotes (SAS Log shown below):
13 %let xx = 'a&b';
14 %put =====> xx= &xx;
=====> xx= 'a&b'

The other way of hiding or removing the normal meaning from & is to use the
%nrstr function -- which also will prevent it from being resolved:
16 %let xxx = %nrstr("a&b");
17 %put =====> xxx= &xxx;
=====> xxx= "a&b"

For more help with macro functions and usage, you should consult the SAS Macro facility documentation or contact SAS Technical Support for help with your SAS macro variable usage.

Good luck,
Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation