macro variable in double quotes

Reply
Occasional Contributor
Posts: 11

macro variable in double quotes

[ Edited ]

Hi all,

 

I have a question about my codes:

 

%macro varinput(data=, selectname= , namelike=);
proc contents data=&data. out=contents(keep=name) noprint;
run;

%global &selectname.;

proc sql noprint;
select name into: &selectname. separated by ' '
from contents
where name like "%nrstr(&namelike.)";
quit;

%mend varinput;
%varinput(data=outcome.cvd, selectname=chfad , namelike=v%chfad);

 

what I would like to present in the where clause is: where name like "v%chfad". However, the warning says "WARNING: Apparent invocation of macro CHFAD not resolved." Is there any way to solve this? Thank you.

 

 

SAS Super FREQ
Posts: 304

Re: macro variable in double quotes

Does this give you the desired result?

 

%macro varinput(data=, selectname=, namelike=);

%global &SELECTNAME;

proc contents data=&DATA out=contents(keep=name) noprint; run; quit;

proc sql noprint feedback;
  select name into: &SELECTNAME separated by ' '
  from contents
  where name like "%nrbquote(&NAMELIKE)";
quit;

%mend varinput;

data test;
set sashelp.class;
v1chfad=name;
v2chfad=sex;
v3chfad=age;
v4chfad=height;
v5chfad=weight;
run;

%VARINPUT(data=test, selectname=varlist, namelike=%nrstr(v%%chfad))

%put &=VARLIST;

 

Vince DelGobbo

SAS R&D

 

Occasional Contributor
Posts: 11

Re: macro variable in double quotes

Posted in reply to Vince_SAS

I think the easiest way to achieve my desired result it just add a %nrstr in namelike and the where statement stays as: where name like "&namelike.". But thank you all the same!

%VARINPUT(data=test, selectname=varlist, namelike=%nrstr(v%chfad))

 

Super User
Super User
Posts: 7,050

Re: macro variable in double quotes

[ Edited ]

If the macro is for yourself then just add single quotes in the macro CALL and eliminate the quotes in the macro code.

%macro varinput...
   ... like &namelike
   ...
%mend;

%VARINPUT(data=test, selectname=varlist, namelike='v%chfad')

Or if you want to get a little fancier you can have the macro use DEQUOTE() function to remove and existing quote characters around the values and use the QUOTE() function to add them back.

%macro varinput...;
%let namelike=%sysfunc(quote(%qsysfunc(dequote(&namelike)),%str(%')));

   ... like &namelike
   ...
%mend;

Then you could all it with or without quotes around it the pattern.

 

%varinput(... namelike='A%B')
%varinput(... namelike=ID)
%varinput(... namelike="VAR%")

Personally what I do is tell the uses to use * as the wildcard in the macro call and then translate the * to % when using it in a LIKE clause.  This eliminates the problem of needing either quotes or macro quoting in the macro call.

%macro varinput...;
%let namelike=%sysfunc(quote(%qsysfunc(translate(&namelike,'%','*')),%str(%')));

   ... like &namelike
   ...
%mend;

%varinput(... namelike=A*B)
%varinput(... namelike=ID)
%varinput(... namelike=VAR*)

 

Ask a Question
Discussion stats
  • 3 replies
  • 195 views
  • 0 likes
  • 3 in conversation