DATA Step, Macro, Functions and more

Reading "(" in SAS macro

Reply
Occasional Contributor
Posts: 14

Reading "(" in SAS macro

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

 

Super User
Posts: 5,076

Re: Reading "(" in SAS macro

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.

Occasional Contributor
Posts: 14

Re: Reading "(" in SAS macro

Thank you so much! It's working now!
Super User
Super User
Posts: 7,392

Re: Reading "(" in SAS macro

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

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