BookmarkSubscribeRSS Feed
Neeta
Fluorite | Level 6

Hi all,

I'm new to SAS. I'm creating a macro where I want to find position of any given character in string using index function.

I'm writing following code but macro stops executing.

%macro check(indata=, char=, var=);

data test;

set &indata;

position=index(&var,"%bquote(&char)");

run;

%mend;

 

%check(indata=try, char=(, var=value);

 

it's throwing errors. Not able to figure out whats going wrong

 

3 REPLIES 3
Astounding
PROC Star

The problem is occuring when invoking the macro, since the unbalanced parentheses throw things off.  Try quoting the character when you invoke the macro:

 

%check (indata=try, char=%str(%(), var=value)

 

In that case, you shouldn't need %BQUOTE any more.  Not all characters require quoting, but parentheses do.

Neeta
Fluorite | Level 6
Thank you so much! It's working now!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Firstly, if your new to SAS, I would strongly advise to learn Base SAS which is the programming language.  Macro is a tool for generation of text, i.e. it is only there to generate Base SAS code, so if you don't understand Base SAS, you will struggle with Macro.  Also, there are specific uses for macro language, using them randomly for purposes creates unweildy hard to mange and QC code which obfuscated.  Take your given example below, if you knew Base SAS, you would know that there are plenty of effecient procedures already built into Base SAS which will both be more efficient, and be known to anyone who uses SAS.  Your code will be inefficient and obfuscated for other users.  Knowing when to use macro is more important than how to use it.  The function you want to use here is index:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212242.htm

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1418 views
  • 0 likes
  • 3 in conversation