Published on :
Macro INTERNAL_CREATION

AHGscanReplace Macro - String Segment Replacement

This code is also available in: Deutsch Español Français
The macro iterates through an input string segmented by a delimiter (space by default). It reconstructs the string by replacing the element at index 'n' with the value provided in the 'to' parameter. The code relies on an external macro named '%AHGcount' (presumably for counting words) which is not included in this snippet.
Data Analysis

Type : INTERNAL_CREATION


The processing is purely logical on character strings passed as macro parameters. No external data table is read.

1 Code Block
MACRO
Explanation :
Macro definition. It initializes a loop based on the number of words (calculated by %AHGcount), builds a new string 'outstr' by replacing the word at index 'n', and returns the final result without the initial delimiter.
Copied!
1%macro AHGscanReplace(str,n,to,dlm=%str( ));
2 %local i outstr;
3 %DO i=1 %to %AHGcount(&str,dlm=&dlm);
4 %IF &i ne &n %THEN %let outstr=&outstr&dlm%scan(&str,&i,&dlm);
5 %ELSE %let outstr=&outstr&dlm&to;
6 %END;
7 %substr(&outstr,2)
8%mend;
9/*%put %ahgscanReplace(ok @ ok @ ok, 2,no,dlm= @);*/
10/*%put %ahgscanReplace(ok ok ok, 1,no );*/
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.