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