DATA Step, Macro, Functions and more

findw erratic behavior

Reply
Regular Contributor
Posts: 234

findw erratic behavior

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

Regular Contributor
Posts: 234

Re: findw erratic behavior

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.

Super User
Super User
Posts: 7,979

Re: findw erratic behavior

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.

Regular Contributor
Posts: 234

Re: findw erratic behavior

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.

 

 

Super User
Posts: 5,516

Re: findw erratic behavior

[ Edited ]

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;

Regular Contributor
Posts: 234

Re: findw erratic behavior

Posted in reply to Astounding
Thanks. Using findw is the goal here, the string always being "U Z E" in the execution context where the problem occur (the U is always isolated).
Ask a Question
Discussion stats
  • 5 replies
  • 395 views
  • 0 likes
  • 3 in conversation