Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro escape string

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.

like image 773
Floris Hoogenboom Avatar asked Aug 02 '26 04:08

Floris Hoogenboom


2 Answers

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
like image 106
Allan Bowe Avatar answered Aug 05 '26 17:08

Allan Bowe


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.

like image 40
Joe Avatar answered Aug 05 '26 16:08

Joe