Zephyr
Zephyr

Reputation: 138

How to mask an & in a SAS macro variable call via an indirect reference

I'm using the following macro code to generate a list of clients from a table:

%macro listclient;
    proc sql noprint;
    select unique(client)
      into :cli1 - 
    from books;   quit;

  %put Total Number of Clients: &sqlobs..;   
  %do i=1 %to &sqlobs;
    %put Client &i &&cli&i;   
  %end; 
%mend listclient;

%listclient

My problem is that some of the clients have names such as Smith & Jones, so I need to use some sort of masking function to get a correct resolution. I've tried a few things, my best guess being to use %nrbquote(&&cli&i) but can't seem to null the problem out. I imagine that I am making a syntax error, or that there may be an issue with the indirect macro variable referencing.

The code runs but with a warning every time an & is encountered in the client name.

I would prefer not to go down the route of replacing all of the &s with "and"s and then changing them back again!

Can anybody be of assistance?

Upvotes: 2

Views: 266

Answers (1)

Tom
Tom

Reputation: 51566

A useful macro function for this is %SUPERQ(). You just need to provide it with the name (not the value) of the macro variable to be quoted.

%do i=1 %to &sqlobs;
  %put Client &i %superq(cli&i);   
%end; 

You could also build your macro variable to already be formatted as quoted strings. If you use single quotes then the macro triggers inside will be ignored.

proc sql noprint;
  select distinct cats("'",tranwrd(name,"'","''"),"'")
    into :name1 -
    from sashelp.class
 ;
quit;
%put &=NAME1;
NAME1='Alfred'

Upvotes: 4

Related Questions