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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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