Carl
Carl

Reputation: 5779

Dealing with parenthesis in quotes in SAS

I have a SAS program that dynamically generates another SAS program (don't ask why) like this:

data _null_;file="myFile.sas";x = "%nrstr(my sas code)";put x;run;

I have an edge case where my code looks like this:

"%nrstr(%myMacro(something,)" || "&macroVariable);";

The issue is that the first ) is closing %myMacro even though I really want it to close %nrstr so instead of printing %myMacro(something,*valueOfMacroVariable); it prints %myMacro(something,)*valueOfMacroVariable); which won't work.

Problem is that macrovariables don't resolve inside of %nrstr, but if I don't wrap with %nrstr it evaluates when I put rather than printing to the file. What I want is x = "%myMacro(something,&macroVariable);", but like I said this doesn't print to new program, this just runs the macro, which I don't want.

Is there a way for me to escape ( in my %nrstr so that it treats it as an arbitrary character and doesn't try to pair it up with the corresponding ) so that the ) can be used to close the %nrstr instead?

Kind Regards

Upvotes: 1

Views: 1292

Answers (2)

Tom
Tom

Reputation: 51566

You only need to have the %NRTSTR() macro quoting around the string that you want quoted, in this case the macro name. Plus do not use double quotes unless you want to expand macro variables or macro calls. So if you don't want either the macro call or the macro variable reference to execute until later (perhaps you are pushing them onto the stack using CALL EXECUTE) then you could build you string like this.

'%nrstr(%myMacro)' || '(something,' || '%nrstr(&macroVariable)' || ');' 

If you did want the macro reference to run now, but not the macro call then you probably want something like this:

'%nrstr(%myMacro)' || '(something,' || "&macroVariable" || ');' 

Upvotes: 3

Joe
Joe

Reputation: 63424

% is how you escape things inside macro quoting functions.

So this would work:

data _null_;
  x="%nrstr(%myMacro%(something,)" || '&macroVariable);';
  put x=;
run;

Note the % before the parenthesis.

If you're constructing this from data, though (as you hopefully are) there may be better options depending on how you're doing so.

Upvotes: 1

Related Questions