BookmarkSubscribeRSS Feed
Heraaaa
Fluorite | Level 6

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.

 

 

3 REPLIES 3
Vince_SAS
Rhodochrosite | Level 12

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

 

Heraaaa
Fluorite | Level 6

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))

 

Tom
Super User Tom
Super User

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*)

 

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 4531 views
  • 1 like
  • 3 in conversation