Production Ready Macros for SAS Application Developers
mf_getquotedstr.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Adds custom quotes / delimiters to a space 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 quote the quote mark to apply (S=Single, D=Double). If any other value
14  than uppercase S or D is supplied, then that value will be used as the
15  quoting character.
16  @return output returns a string with the newly quoted / delimited output.
17 
18  @version 9.2
19  @author Allan Bowe
20 **/
21 
22 
23 %macro mf_getquotedstr(IN_STR,DLM=%str(,),QUOTE=S
24 )/*/STORE SOURCE*/;
25  %if &quote=S %then %let quote=%str(%');
26  %else %if &quote=D %then %let quote=%str(%");
27  %else %let quote=%str();
28  %local i item buffer;
29  %let i=1;
30  %do %while (%qscan(&IN_STR,&i,%str( )) ne %str() ) ;
31  %let item=%qscan(&IN_STR,&i,%str( ));
32  %if %bquote(&QUOTE) ne %then %let item=&QUOTE%qtrim(&item)&QUOTE;
33  %else %let item=%qtrim(&item);
34 
35  %if (&i = 1) %then %let buffer =%qtrim(&item);
36  %else %let buffer =&buffer&DLM%qtrim(&item);
37 
38  %let i = %eval(&i+1);
39  %end;
40 
41  %let buffer=%sysfunc(coalescec(%qtrim(&buffer),&QUOTE&QUOTE));
42 
43  &buffer
44 
45 %mend;