06-10-2016 09:04 AM
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.
06-10-2016 09:09 AM
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.
06-10-2016 09:24 AM
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.
06-10-2016 09:52 AM
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.
06-10-2016 09:36 AM - edited 06-10-2016 10:04 AM
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;