3
votes

This should be an easy one, but I can't figure it out: there are situations where I need to create a macro variable from contents of a table, and they sometimes contain ampersands (&) as part of the text. How do I get SAS to ignore the ampersands when I call the macro variable? For example, this code...

data _null_;
test="Amos&Andy";
call symputx("testvar",test);
run;
%put testvar=&testvar;

...writes this to the log:

28   data _null_;
29   test="Amos&Andy";
WARNING: Apparent symbolic reference ANDY not resolved.
30   call symputx("testvar",test);
31   run;

NOTE: DATA statement used (Total process time):
  real time           0.00 seconds
  cpu time            0.00 seconds


WARNING: Apparent symbolic reference ANDY not resolved.
32   %put testvar=&testvar;
testvar=Amos&Andy

How can I get SAS to ignore the ampersand and not write the WARNING to the log? Thanks very much!

1
Try using quoting funtions like nrstr or superq on such variables or put the value of variable in single quotesPuneet Tripathi
That's great. Thank you!Eric

1 Answers

6
votes

The first warning is easy to avoid. Macro variable references don't resolve inside of single quotes, so you can use:

data _null_;
test='Amos&Andy';
call symputx("testvar",test);
run;

When you want to resolve &testvar, you need a way to tell the macro processor not to resolve any macro triggers that are revealed by resolving &testvar. This is the purpose of macro quoting:

%put testvar=%superq(testvar);

%SUPERQ tells the macro processor to resolve the macro variable Testvar and to quote (i.e. mask) any macro triggers that are revealed by resolving Testvar. This prevents the macro processor from looking for a macro variable named Andy.