Published on :
Macro CREATION_INTERNE

Word Index Search Macro in a List

This code is also available in: Deutsch Español Français English
This macro iterates through a character string (`full`) considered as a list of words separated by a delimiter (`dlm`, space by default). It compares each word with the searched value (`sub`) and returns its index (1 for the first word, etc.). It offers options for case sensitivity (`case`) and for returning the last found occurrence (`lastone`) instead of the first. It depends on another macro named `%AHGcount` to determine the word count.
Data Analysis

Type : CREATION_INTERNE


This is a character string processing macro that does not consume data tables, but acts on the parameters provided during the call.

1 Code Block
MACRO
Explanation :
Definition of the `AHGindex` macro. It initializes local variables, handles uppercase conversion if the search is case-insensitive, loops through the words in the string using `%scan` and the external macro `%AHGcount`, updates the index if a match is found, and returns the final index value.
Copied!
1%macro AHGindex(full,sub,dlm=%str( ),case=0,lastone=0);
2 %local index i;
3 %IF not &case %THEN
4 %DO;
5 %let full=%upcase(&full);
6 %let sub=%upcase(&sub);
7 %END;
8 %let index=0;
9 %DO i=1 %to %AHGcount(&full,dlm=&dlm);
10 %IF %scan(&full,&i,&dlm)=&sub %THEN
11 %DO;
12 %let index=&i;
13 %IF not &lastone %THEN %goto indexExit;
14 %END;
15 %END;
16 %indexExit:
17 &index
18%mend;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.