Published on :

String Formatting with Delimiters and Quoting

This code is also available in: Deutsch Español Français
The `%mf_getquotedstr` macro takes an input string (`IN_STR`) and transforms it into a new string where each element is enclosed in quotes (single, double, or none) and separated by a specified delimiter. Parameters include:
  • `IN_STR`: The unquoted input string, delimited by spaces (default).
  • `DLM`: The delimiter to apply to the output string (comma by default).
  • `QUOTE`: The type of quotes to apply (S for single, D for double, N for none). Any other value will be used as the quoting character.
  • `indlm`: The delimiter used in the input string (space by default).
The process involves iterating through each 'word' of the input string, applying the specified quotes, removing superfluous spaces around each word (`%qtrim`), and then assembling the new string with the output delimiter. If the input string is empty, it returns two empty quotes (depending on the specified type).
Data Analysis

Type : NONE


The macro operates exclusively on character strings provided as parameters. It neither accesses nor creates SAS datasets, nor does it depend on external data (files, tables).

1 Code Block
MACRO
Explanation :
This block defines the `%mf_getquotedstr` macro which processes an input string. It starts by determining the quoting character to use, converting 'S' to a single quote (`byte(39)`) and 'D' to a double quote (`byte(34)`) via `%qsysfunc`. It initializes a local variable `i` to iterate through the input string `IN_STR` using a `%do %while` loop and the `%qscan` function to extract each element according to the input delimiter `indlm`. Each element is then quoted and cleaned of excess spaces with `%qtrim`. The first element initializes the `buffer`, subsequent ones are added with the specified `DLM`. Finally, `%sysfunc(coalescec())` is used to handle the case where the input string is empty, returning two quotes. The final result is the `&buffer` string.
Copied!
1%macro mf_getquotedstr(IN_STR
2 ,DLM=%str(,)
3 ,QUOTE=S
4 ,indlm=%str( )
5)/*/STORE SOURCE*/;
6 /* credit Rowland Hale - byte34 is double quote, 39 is single quote */
7 %IF "e=S %THEN %let quote=%qsysfunc(byte(39));
8 %ELSE %IF "e=D %THEN %let quote=%qsysfunc(byte(34));
9 %ELSE %IF "e=N %THEN %let quote=;
10 %local i item buffer;
11 %let i=1;
12 %DO %while (%qscan(&IN_STR,&i,%str(&indlm)) ne %str() ) ;
13 %let item=%qscan(&IN_STR,&i,%str(&indlm));
14 %IF %bquote("E) ne %THEN %let item="E%qtrim(&item)"E;
15 %ELSE %let item=%qtrim(&item);
16 
17 %IF (&i = 1) %THEN %let buffer =%qtrim(&item);
18 %ELSE %let buffer =&buffer&DLM%qtrim(&item);
19 
20 %let i = %eval(&i+1);
21 %END;
22 
23 %let buffer=%sysfunc(coalescec(%qtrim(&buffer),"E"E));
24 
25 &buffer
26 
27%mend mf_getquotedstr;
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 : Author: Allan Bowe