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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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