-
Notifications
You must be signed in to change notification settings - Fork 0
/
cquote.sas
69 lines (56 loc) · 2.08 KB
/
cquote.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/******************************************************************************\
* Name: cquote.sas
*
* Purpose: Converts a list of space-separated strings into a quoted, comma-separated list.
*
* Author: Original code by Tom - StackOverflow: https://stackoverflow.com/a/65863926
* Converted to functions by Stu Sztukowski
*
* MACRO
* Parameters: strlist | Space-separated string list
* quote | Optional. Specify SINGLE or DOUBLE quotes.
* Default: DOUBLE
*
* FUNCTION
* strlist | Space-separated string list. Returns a length of 200 if none is assigned.
*
* Usage: Ideally useful for creating quoted lists of macro variables. Exists in both an FCMP and a macro
* form.
*
* Example:
(1) Convert a space-separated list into a double-quoted, comma-separated list
%let list = a b c;
%put %cquote(&list);
(2) Convert a space-separated list into a single-quoted, comma-separated listed
%let list = a b c;
%put %cquote(&list, single);
(3) Use a space-separted list in an IN operator:
%let list = BMW Mercedes Audi;
data foo;
set sashelp.cars;
where make IN(%cquote(&list));
run;
(4) Convert a DATA Step string into a quoted list
options cmplib=work.funcs;
data bar;
list = 'a b c';
qlist = cquote(list);
run;
*
\******************************************************************************/
%macro cquote(strlist, quote);
%if(%upcase("e) = SINGLE) %then %let q = %str(%');
%else %let q = %str(%");
%unquote(%bquote(&q)%qsysfunc(tranwrd(%qsysfunc(compbl(%superq(strlist))),%bquote( ),%bquote(&q,&q)))%bquote(&q))
%mend;
proc fcmp outlib=work.funcs.str;
/* Double quote version */
function cquote(str$) $200;
return (cats('"', tranwrd(compbl(str),' ','","'), '"'));
endfunc;
/* Single quote version */
function scquote(str$) $200;
return (cats("'", tranwrd(compbl(str),' ',"','"), "'"));
endfunc;
run;