Published on :
Macro CREATION_INTERNE

Word Substring Extraction Macro (AHGscanSubstr)

This code is also available in: Deutsch Español Français
This macro extracts a defined number of words (`num`) from a source string (`words`), starting at a given word index (`from`). It allows specifying a custom delimiter (`dlm`) and includes an option to compress the final result (whitespace removal).
Data Analysis

Type : CREATION_INTERNE


The macro only processes the arguments (character strings) passed to it; it does not depend on any external tables.

1 Code Block
MACRO
Explanation :
Macro definition. It iterates `num` times to extract successive words via `%scan` and concatenates them into the local variable `outstr`, which is then returned.
Copied!
1%macro AHGscanSubstr(words,from,num,dlm1st=0,dlm=%str( ),compress=0/*right*/);
2 %local i outstr;
3 %let outstr=;
4 %DO i=0 %to %eval(&num-1);
5 %IF &i gt &dlm1st %THEN %let outstr=&outstr&dlm;
6 %let outstr=&outstr%scan(&words,%eval(&i+&from),&dlm);
7 %END;
8 %IF &compress %THEN %let outstr=%sysfunc(compress(&outstr));
9 &outstr
10%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.