Published on :
Macro CREATION_INTERNE

commaparms - Add commas to macro parameters

This code is also available in: Deutsch Español Français
This macro processes a character string containing macro parameters (e.g., 'param1=val1 param2=val2') where separating commas have been omitted. The goal is to avoid having to protect the entire string with %str() or %nrstr() during the call. The macro uses a regular expression with the PRXCHANGE function to detect parameter assignments and insert a comma before each of them. The result is a correctly formatted parameter string (e.g., ',param1=val1,param2=val2'), then the first character (the initial comma) is removed. The result is returned unquoted so that equals signs are not masked.
Data Analysis

Type : CREATION_INTERNE


This macro does not manipulate datasets. It operates on a character string passed as a parameter to transform it.

1 Code Block
MACRO
Explanation :
This block defines the `%commaparms` macro. It takes a `str` argument. The `%qsysfunc(prxchange(...))` function is used to perform a regular expression-based substitution. The expression `s·\s*(\w+)\s*=\s*·,\1=·` searches for 'word=value' patterns (with optional spaces around the '=' sign) and replaces them with ',word=value'. The `%qsubstr(..., 2)` function removes the superfluous first comma. Finally, `%unquote` ensures that the final string is returned without its special characters (like '=') being masked, making it directly usable in a macro call.
Copied!
1/*
2/ Program : commaparms.sas
3/ Version : 1.0
4/ Author : Roland Rashleigh-Berry
5/ Date : 10-Jun-2014
6/ Purpose : Function-style macro to add back commas between macro parameters
7/ where these have been deliberately omitted in a string.
8/ SubMacros : none
9/ Notes : The result will be returned macro UNQUOTED so that equals signs
10/ are not macro quoted.
11/
12/ Where a controlling macro allows the user to run other macros and
13/ supply parameters values then it is common practice to not use
14/ commas to separate the parameter values and to add these
15/ programatically when the called macro is invoked. This is done to
16/ avoid having to macro quote the whole string due to the presence
17/ of commas. This macro adds back the commas to the parameter list
18/ that are needed when the macro is called.
19/
20/ The macro removes spaces either side of the equals sign and
21/ precedes the parameter name with a comma (removing the very first
22/ comma).
23/
24/ Enclose the string for conversion inside %nrbquote() when calling
25/ this macro otherwise the first parameter name in the string will
26/ be interpreted as a macro parameter of the %commaparms macro.
27/
28/ Usage : %let params=%commaparms(%nrbquote(&str));
29/===============================================================================
30/ PARAMETERS:
31/-------name------- -------------------------description------------------------
32/ str (pos) String
33/===============================================================================
34/ AMENDMENT HISTORY:
35/ init --date-- mod-id ----------------------description------------------------
36/ rrb 25Mar13 New (v1.0)
37/ rrb 10Jun14 Header tidy (v1.0)
38/===============================================================================
39/ This is public domain software. No guarantee as to suitability or accuracy is
40/ given or implied. User uses this code entirely at their own risk.
41/=============================================================================*/
42 
43%put MACRO CALLED: commaparms v1.0;
44 
45%macro commaparms(str);
46%unquote(%qsubstr(%qsysfunc(prxchange(%nrbquote(s·\s*(\w+)\s*=\s*·,\1=·),-1,
47%nrbquote(&str))),2))
48%mend commaparms;
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.