Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mf_getquotedstr.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Adds custom quotes / delimiters to a delimited string
4 @details Can be used in open code, eg as follows:
5
6 %put %mf_getquotedstr(blah blah blah);
7
8 which returns:
9> 'blah','blah','blah'
10
11 Alternatively:
12
13 %put %mf_getquotedstr(these words are double quoted,quote=D)
14
15 for:
16> "these","words","are","double","quoted"
17
18 @param [in] in_str The unquoted, spaced delimited string to transform
19 @param [in] dlm= (,) The delimeter to be applied to the output (default comma)
20 @param [in] indlm= ( ) The delimeter used for the input (default is space)
21 @param [in] quote= (S) The quote mark to apply (S=Single, D=Double, N=None).
22 If any other value than uppercase S or D is supplied, then that value will
23 be used as the quoting character.
24 @return output returns a string with the newly quoted / delimited output.
25
26 @version 9.2
27 @author Allan Bowe
28**/
29
30
31%macro mf_getquotedstr(IN_STR
32 ,DLM=%str(,)
33 ,QUOTE=S
34 ,indlm=%str( )
35)/*/STORE SOURCE*/;
36 /* credit Rowland Hale - byte34 is double quote, 39 is single quote */
37 %if &quote=S %then %let quote=%qsysfunc(byte(39));
38 %else %if &quote=D %then %let quote=%qsysfunc(byte(34));
39 %else %if &quote=N %then %let quote=;
40 %local i item buffer;
41 %let i=1;
42 %do %while (%qscan(&IN_STR,&i,%str(&indlm)) ne %str() ) ;
43 %let item=%qscan(&IN_STR,&i,%str(&indlm));
44 %if %bquote(&QUOTE) ne %then %let item=&QUOTE%qtrim(&item)&QUOTE;
45 %else %let item=%qtrim(&item);
46
47 %if (&i = 1) %then %let buffer =%qtrim(&item);
48 %else %let buffer =&buffer&DLM%qtrim(&item);
49
50 %let i = %eval(&i+1);
51 %end;
52
53 %let buffer=%sysfunc(coalescec(%qtrim(&buffer),&QUOTE&QUOTE));
54
55 &buffer
56
57%mend mf_getquotedstr;