forked from sasutils/macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlist.sas
61 lines (54 loc) · 2.53 KB
/
qlist.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
%macro qlist
/*----------------------------------------------------------------------
Adds quotes to each word in a list, and optionally separate multiple
words with a comma and adds parentheses to the entire string.
----------------------------------------------------------------------*/
(list /* List of words */
,paren=1 /* Include list in parentheses? 1=Yes,0=No */
,comma=1 /* Use comma delimiter in output list? 1=Yes,0=No */
,delimit= /* Word delimiter for input list */
,dsd= /* Adjacent delimiters indicate blank value? 1=Yes,0=No */
,quote=1 /* Quote character type. 1=Single 2=Double */
);
%local rp i sep ;
%*----------------------------------------------------------------------
Set COMMA to value to use as separator in the output list.
-----------------------------------------------------------------------;
%if "&comma" = "1" %then %let comma = ,;
%else %let comma = %str( );
%*----------------------------------------------------------------------
When delimiter not specified set to a blank and default DSD to 0.
-----------------------------------------------------------------------;
%if ^%length(&delimit) %then %do;
%let delimit = %str( );
%if ^%length(&dsd) %then %let dsd=0;
%end;
%*----------------------------------------------------------------------
Set DSD to value needed for COUNTW() and SCAN() function calls.
-----------------------------------------------------------------------;
%if "&dsd"="0" %then %let dsd=;
%else %let dsd=M;
%*----------------------------------------------------------------------
Set QUOTE to value needed for QUOTE() function calls.
-----------------------------------------------------------------------;
%if ""e"="1" %then %let quote=%str(%');
%else %let quote=%str(%");
%*----------------------------------------------------------------------
Add parentheses when requested.
-----------------------------------------------------------------------;
%if ("&paren" = "1") %then %do;
%let rp = );
(
%end;
%*----------------------------------------------------------------------
Process each word individually using the SCAN() and QUOTE() functions.
-----------------------------------------------------------------------;
%do i=1 %to %sysfunc(max(1,%sysfunc(countw(&list,&delimit,&dsd))));
%*;%unquote(&sep)%sysfunc(quote(%qsysfunc(scan(&list,&i,&delimit,&dsd)),"e))
%let sep=,
%end;
%*----------------------------------------------------------------------
Add parentheses when requested.
-----------------------------------------------------------------------;
%*;&rp
%mend qlist;