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
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.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.