Published on :
Macro CREATION_INTERNE

Remove option modifiers from a dataset name

This code is also available in: Deutsch Español Français
This macro, `dropmodifmac`, takes as input a character string containing one or more SAS© dataset names, potentially followed by modifiers in parentheses (such as `(where=...)` or `(drop=...)`). Its objective is to return the character string with only the dataset names, without the modifiers. It is designed to correctly handle parentheses present inside quoted strings (single or double quotes) within the modifiers, to avoid accidentally removing them. The macro uses regular expressions (via the `prxchange` function) and a loop to iteratively remove parenthesized blocks from the outside in.
Data Analysis

Type : CREATION_INTERNE


The macro does not process any dataset. It operates on a character string provided as a parameter. The 'data' source is therefore internal to the macro call.

1 Code Block
MACRO
Explanation :
This block defines the `dropmodifmac` macro, which accepts a character string parameter `str`. A local macro variable `tempstr` is declared. Processing begins by neutralizing double-quoted and then single-quoted strings by replacing them with a special character, to prevent any parentheses within them from being processed. Then, a `%do %while` loop executes as long as an opening parenthesis is detected in the string. In each iteration, the `prxchange` function is used with a regular expression to find and remove the innermost `()` pair and its content. The loop repeats until all parenthesis pairs have been eliminated. Finally, the macro returns the cleaned `tempstr` string.
Copied!
1%put MACRO CALLED: dropmodifmac v1.0;
2 
3%macro dropmodifmac(str);
4 %local tempstr;
5 %*- non-greedy replace stuff in double quotes with "" -;
6 %let tempstr=%sysfunc(prxchange(s!%str(%"-).*?%str(%"-)!""!,-1,
7 %superq(str)));
8 %*- non-greedy replace stuff in single quotes with '' -;
9 %let tempstr=%sysfunc(prxchange(s!%str(%'-).*?%str(%'-)!''!,-1,
10 %superq(tempstr)));
11 %*- repeat until we have no more left round brackets -;
12 %DO %while( %index(%superq(tempstr),%str(%()) );
13 %*- Non-greedy replace stuff inside "( )" that does -;
14 %*- not include a left round bracket with null. -;
15 %let tempstr=%sysfunc(prxchange(s!\%str(%()[^\%str(%()]*?\%str(%))!!,-1,
16 %superq(tempstr)));
17 %END;
18&tempstr
19%mend dropmodifmac;
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.