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  @param in_str the unquoted, spaced delimited string to transform
12  @param dlm= the delimeter to be applied to the output (default comma)
13  @param indlm= the delimeter used for the input (default is space)
14  @param quote= the quote mark to apply (S=Single, D=Double). If any other value
15  than uppercase S or D is supplied, then that value will be used as the
16  quoting character.
17  @return output returns a string with the newly quoted / delimited output.
18 
19  @version 9.2
20  @author Allan Bowe
21 **/
22 
23 
24 %macro mf_getquotedstr(IN_STR,DLM=%str(,),QUOTE=S,indlm=%str( )
25 )/*/STORE SOURCE*/;
26  %if &quote=S %then %let quote=%str(%');
27  %else %if &quote=D %then %let quote=%str(%");
28  %else %let quote=%str();
29  %local i item buffer;
30  %let i=1;
31  %do %while (%qscan(&IN_STR,&i,%str(&indlm)) ne %str() ) ;
32  %let item=%qscan(&IN_STR,&i,%str(&indlm));
33  %if %bquote(&QUOTE) ne %then %let item=&QUOTE%qtrim(&item)&QUOTE;
34  %else %let item=%qtrim(&item);
35 
36  %if (&i = 1) %then %let buffer =%qtrim(&item);
37  %else %let buffer =&buffer&DLM%qtrim(&item);
38 
39  %let i = %eval(&i+1);
40  %end;
41 
42  %let buffer=%sysfunc(coalescec(%qtrim(&buffer),&QUOTE&QUOTE));
43 
44  &buffer
45 
46 %mend;