3

I'm trying to print a line to the console which should print one macro variable and one piece of text with an ampersand in it. Let me illustrate this with a small example:

%LET THIS_IS_A_MACRO_VAR = test; 
%PUT &THIS_IS_A_MACRO_VAR..&THIS_SHOULD_BE_TEXT;

When running this I would like the output to be:

test.&THIS_SHOULD_BE_TEXT

Coming from languages like PHP I expected there was something like an escape character. Unfortunately I could find anything similar.

I tried using different placements of ' and " but that led to both variable being interpreted as macro variable or both variables not. Also a try using %NRQUOTE was without succes. Lastly, I tried concatenating two strings: "&THIS_IS_A_MACRO_VAR..&" || "THIS_SHOULD_BE_TEXT".

I hope someone knows how to do this.

Joe
  • 62,789
  • 6
  • 49
  • 67

2 Answers2

2

A quick way is to simply separate the ampersand from the text, to prevent it being resolved as a macro variable, as follows:

%LET THIS_IS_A_MACRO_VAR = test; 
%PUT &THIS_IS_A_MACRO_VAR..%str(&)THIS_SHOULD_BE_TEXT;

this gives:

test.&THIS_SHOULD_BE_TEXT
Allan Bowe
  • 12,306
  • 19
  • 75
  • 124
1

I'd suggest the usual way to do this would be to escape the entire bit of text with non-resolution escaping. SAS certainly has very powerful (but very confusing) escaping options.

%nrstr(&this_should_be_text) 

That would be the simplest, if it's only attempted to be resolved once. You might need to use bquote or similar if it needs to stay escaped for several resolution passes. See Secrets of Macro Quoting Functions by Susan O'Connor for more details.

Joe
  • 62,789
  • 6
  • 49
  • 67