Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
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_str the unquoted, spaced delimited string to transform
19 @param dlm= the delimeter to be applied to the output (default comma)
20 @param indlm= the delimeter used for the input (default is space)
21 @param quote= the quote mark to apply (S=Single, D=Double). If any other value
22 than uppercase S or D is supplied, then that value will be used as the
23 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,DLM=%str(,),QUOTE=S,indlm=%str( )
32)/*/STORE SOURCE*/;
33 %if &quote=S %then %let quote=%str(%');
34 %else %if &quote=D %then %let quote=%str(%");
35 %else %let quote=%str();
36 %local i item buffer;
37 %let i=1;
38 %do %while (%qscan(&IN_STR,&i,%str(&indlm)) ne %str() ) ;
39 %let item=%qscan(&IN_STR,&i,%str(&indlm));
40 %if %bquote(&QUOTE) ne %then %let item=&QUOTE%qtrim(&item)&QUOTE;
41 %else %let item=%qtrim(&item);
42
43 %if (&i = 1) %then %let buffer =%qtrim(&item);
44 %else %let buffer =&buffer&DLM%qtrim(&item);
45
46 %let i = %eval(&i+1);
47 %end;
48
49 %let buffer=%sysfunc(coalescec(%qtrim(&buffer),&QUOTE&QUOTE));
50
51 &buffer
52
53%mend mf_getquotedstr;