BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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?
1 REPLY 1
Cynthia_sas
SAS Super FREQ
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:
[pre]
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"
[/pre]
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):
[pre]
12
13 %let xx = 'a&b';
14 %put =====> xx= &xx;
=====> xx= 'a&b'
[/pre]

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:
[pre]
16 %let xxx = %nrstr("a&b");
17 %put =====> xxx= &xxx;
=====> xxx= "a&b"
[/pre]

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,
cynthia

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1089 views
  • 0 likes
  • 2 in conversation