Hi,
I have a macro "isIn" which is supposed to test if an element is in a given (macro) string.
The code is as follows :
%macro rank(string,element,sep=); /* Returns element's index in a list of elements */ %if &sep ne %then %do; %sysfunc(findw(&chaine.,&element.,&sep,E)) %end; %else %do; %sysfunc(find(&chaine.,&element.)) %end; %mend; %macro isIn(string,element); /* Tests whether an element is in a given string */ %eval(%rank(&string.,&element.,sep=' ')>0) %mend;
Somewhere in the program, &mystring. is initialized to "U Z E" (without the quotes) and elsewhere the following test occur :
%if %isIn(&mystring,U) %then ...
The test is expected to return 1 since "U" is in "U Z E".
Several execution of the program give different results for the above test, yet the input is always the same.
It shoudn't be too complicated to modify the code and avoid using the findw function but I am just wondering
what is wrong exactly and why the macro behavior seems random.
Do you see anything wrong with the above code ?
We use SAS 9.4.
Thanks
I have translated the given code from french to english and missed some elements.
The first arguments to findw adn find should be &string. not &chaine.
There is no such problem is the original source code.
Sorry, I am going to have to ask, why? All that macro code, which is nigh on impossible to read, is doing is this:
if index("U Z E","U")>0
Macro language is not there as a replacement for Base SAS.
RW9,
Thanks for your answer.
The program in question is a stored process from a web application. The "U" is in fact the value of a macrovariable that results from the user's choices in a form. We use macro code to take users inputs into account and determine the actions to take. I was unaware of the index function so we could replace the test by :
%if %sysfunc(index(&string.,&element))>0 %then %do; ....
I will try this.
Yet, I am still wondering how this macro can give random results.
One suspect area: switching from FINDW to FIND. If the initial string is WU X E, the FIND function will find U, but the FINDW function will not.
Also note, the executions are almost certainly using FINDW, not FIND. In macro language, this comparison is true:
%if ' ' ne %then %do;
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.