BookmarkSubscribeRSS Feed
gamotte
Rhodochrosite | Level 12

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

5 REPLIES 5
gamotte
Rhodochrosite | Level 12

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

gamotte
Rhodochrosite | Level 12

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.

 

 

Astounding
PROC Star

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;

gamotte
Rhodochrosite | Level 12
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).

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1224 views
  • 0 likes
  • 3 in conversation