Published on :
Macro CREATION_INTERNE

Nested String Parsing Macro

This code is also available in: Deutsch Español Français
The `AHGscan2` macro is designed to extract a specific element from a given character string, handling two levels of delimiters. It uses SAS©'s `%SCAN` function in a nested manner: a first `%SCAN` to isolate a section of the main string using the first delimiter (`dlm`), then a second `%SCAN` on this section to extract a precise element using the second delimiter (`dlm2`). The default delimiters are the backslash (\) and the hash (#). This macro is useful for parsing complex structured strings where information is separated by different delimitation schemes within segments.
Data Analysis

Type : CREATION_INTERNE


The macro operates on a character string passed to it as a parameter (`mac`). It neither creates nor directly uses external or internal SAS data sets (SASHELP). The 'data' processed is the input string provided during the macro call.

1 Code Block
MACRO
Explanation :
This block defines the `AHGscan2` macro. It takes four parameters: `mac` (the source string), `i` (the index of the element to extract at the first scan level), `j` (the index of the element to extract at the second scan level), `dlm` (the first delimiter, defaulting to `\`) and `dlm2` (the second delimiter, defaulting to `#`). The body of the macro performs an external `%SCAN` using `dlm` on the `mac` string at index `i`, then an internal `%SCAN` on the result with `dlm2` at index `j`. The final result is the extracted substring.
Copied!
1%macro AHGscan2(mac,i,j,dlm=\,dlm2=#);
2%scan(%scan(&mac,&i,&dlm),&j,&dlm2)
3%mend;
4 
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.