DATA Step, Macro, Functions and more

Using "%" with a macro variable

Reply
New Contributor
Posts: 3

Using "%" with a macro variable

I have been pulling my hair out with this. I want to create a macro that submits some SQL code. The SQL code contains a WHERE clause using the LIKE operator. I need to include a macro variable in the LIKE string. Here is what it looks like.

%Macro MyMac(name);
Proc SQL;
Select *
From somefile
WHERE somefield LIKE "%&name%";
%Mend;

%MyMac(jeff);

The resulting SQL code should look like

Proc SQL
Select *
From somefile
WHERE somefield LIKE "%jeff%";

Those two percent signs are killing me. Can anyone come up with a series of macro functions that would result in the string Im looking for? Your help is appreciated.

Jeff
Super Contributor
Posts: 260

Re: Using "%" with a macro variable

What you need is a QUOTING function. Quoting is the art of hiding characters, preventing the macro processor to do them wrong. In that case, %NRBQUOTE seems to work out fine.
[pre]
WHERE name LIKE "%NRBQUOTE(%)&name%NRBQUOTE(%)" ;
[/pre]
Regards.
Olivier
New Contributor
Posts: 3

Re: Using "%" with a macro variable

Thanks Olivier. I had a feeling it would be something as simple as that. It worked perfectly.
Ask a Question
Discussion stats
  • 2 replies
  • 112 views
  • 0 likes
  • 2 in conversation