Published on :
Macro CREATION_INTERNE

Finding the Second Delimiter in a String

This code is also available in: Deutsch Español Français
The `AHGindex2` macro takes two arguments: `str` (the string to analyze) and `dlm` (the delimiter to search for). It initializes a `result` variable to 0 and iterates through the string character by character. It uses local variables `one` to mark the first occurrence of the delimiter and `result` to store the index of the second occurrence. The macro returns the index of the second occurrence of the delimiter. If the second delimiter is not found or if there is only one occurrence, it returns 0.
Data Analysis

Type : CREATION_INTERNE


Data is provided directly via macro parameters (`str` and `dlm`). No external file or SAS library is read.

1 Code Block
MACRO DEFINITION
Explanation :
This block defines the `AHGindex2` macro. It uses a `%do` loop to iterate over the `str` string. The `%qsubstr` function is used to extract one character at a time. The logic identifies the first occurrence of the delimiter using the `one` variable and, once found, searches for the second occurrence by storing its index in `result`.
Copied!
1%macro AHGindex2(str,dlm);
2%local i one RESULT;
3%let RESULT=0;
4%DO i=1 %to %LENGTH(&str);
5%IF %qsubstr(&str,&i,1) = &dlm and (&RESULT=0) and (&one=1) %THEN %let RESULT=&i;
6%IF %qsubstr(&str,&i,1) eq &dlm %THEN %let one=1;
7%END;
8&RESULT
9%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.