Published on :
Macro CREATION_INTERNE

Substring Extraction After Target Character

This code is also available in: Deutsch Español Français
The `%allafterc` macro is a function-style macro that returns the part of a string that follows the first occurrence of one of the characters specified in a target string. The search is case-sensitive. If none of the target characters are found, a null string is returned. It uses the SAS© `indexc` function to locate the character's position. This macro is designed to be executable on SAS© Viya 4.
Parameters:
* `string`: (positional) The string to search within.
* `target`: (positional) One or more target characters to search for (case-sensitive).
Usage example:
`%let rest=%allafterc(&str,\/);`
Data Analysis

Type : CREATION_INTERNE


The data processed by the macro (the main string and the target string) are provided directly as input parameters to the macro. No data reading from external files, databases, or SASHELP libraries is performed.

1 Code Block
MACRO allafterc
Explanation :
This block defines the `%allafterc` macro. It initializes a local variable `pos`. It uses the SAS `indexc` function via `%sysfunc` to find the position of the first character of `target` in `string`. If a character is found (i.e., if `indexc` does not return 0), the position is stored in `pos`. Then, another `%if` checks if this position is not the end of the string. If not, it extracts and returns the substring of `string` starting one character after the found position, using `%qsubstr` to handle special characters. If no target character is found or if the target character is the last in the string, no value is returned (implicitly an empty string in the context of a functional macro).
Copied!
1 %local pos;
2 %IF %sysfunc(indexc(&string,&target)) %THEN %DO;
3 %let pos=%sysfunc(indexc(&string,&target));
4 %IF &pos LT %LENGTH(&string) %THEN %qsubstr(&string,&pos+1);
5 %END;
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.
Copyright Info : This is public domain software. No guarantee as to suitability or accuracy is given or implied. User uses this code entirely at their own risk.