BookmarkSubscribeRSS Feed
hein68
Obsidian | Level 7

Hello.  I'm trying to run a macro with a wildcard in it.  This the my macro code:

 

%macro oth(n,c,l);

 data &c&n;

set &nopilot;

where othmedc&n like "%&c%" and med13&l.pn=1;

 keep study_id othmedc&n med13&l.pn;

run;

 %mend oth;

 

%oth(1,neuropathy,v)

 

Why do I get this error message:

 

WARNING: Apparent invocation of macro NEUROPATHY not resolved.

 

What is wrong with my code?

 

Thanks!

4 REPLIES 4
novinosrin
Tourmaline | Level 20

I think you need to quote the % before &c because when &c resolves to neuropathy, the resolved text looks like %neuropathy macro call. This % should not be considered a macro trigger rather should be considered as text by the macro processor. Therefore quoting the % is required and the following may help

 

where othmedc&n like %str("%%&c%%") and med13&l.pn=1;

yabwon
Onyx | Level 15

Hi,

 

the error/warning is because condition

 

where othmedc&n like "%&c%" and med13&l.pn=1;

 

 

 

is resolved to:

 

where othmedc1like "%neuropathy%" and med13vpn=1;

and when  SAS finds text >>%neuropathy<< text in double quotation marks (") so it tries to find macro named >>neuropathy<< and since can't it gives warning.

 

I think that simple workaround can be done in following way:

 

%macro oth(n,c,l);

 data &c&n;

set &nopilot;

where othmedc&n like cats('%',"&c.",'%') and med13&l.pn=1;

 keep study_id othmedc&n med13&l.pn;

run;

 %mend oth;

All the best

Bart

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Patrick
Opal | Level 21

@hein68

Using %% will mask the second % from the macro processor so it won't get interpreted as a token for a macro that doesn't exist.

options mprint;
data have;
  length study_id $1 othmedc1 $20 med13vpn 8;
  call missing(of _all_);
  med13vpn=1;
  othmedc1='abcd Neuropathy xyz';
  output;
  othmedc1='abcd Xeuropathy xyz';
  output;
  stop;
run;

%let nopilot=have;
%macro oth(n,c,l);
  data &c&n;
    set &nopilot;
    where upcase(othmedc&n) like %upcase("%%&c%%") and med13&l.pn=1;
    keep study_id othmedc&n med13&l.pn;
  run;

%mend oth;

%oth(1,neuropathy,v)
Astounding
PROC Star

As a last resort, if nothing else works you can always build your string in a separate DATA step:

 

data _null_;

call symputx('string', "'%" || "&c" || "%'");

run;

 

Then use the macro variable for comparing:

 

where ..... like &string ......;

 

Don't add quotes around &STRING since the quotes are built into its value.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 2409 views
  • 2 likes
  • 5 in conversation