SAS Documentation
SASĀ® Solution for Stress Testing
Reference manual - version 08.2021
Loading...
Searching...
No Matches
irm_tslit_convert.sas
1/*----------------------------------------------------------------------
2 * NAME: irm_tslit_convert.sas
3 *
4 * PURPOSE: Converts a string with double quoted items to single quotes.
5 * This is similar to the %tslit macro but it operates on a
6 * data set and will convert a string at once.
7 *
8 * NOTES:
9 *
10 * MACRO OPTIONS:
11 * in_table - Input table to process
12 * out_table - OPTIONAL: Output table (overwrites input if not specified)
13 * variable - Variable to process
14 * debug - OPTIONAL: Y/N flag for use in debuging
15 *
16 *----------------------------------------------------------------------*/
17
18%macro irm_tslit_convert(
19 in_table=,
20 out_table=,
21 out_filter_variable_table=work.filter_variables,
22 variable=,
23 debug=no)
24 / minoperator;
25
26 /*--------------------------------------
27 * Default output table to input table
28 *--------------------------------------*/
29 %if %quote(&out_table) eq %then
30 %let out_table = &in_table;
31
32 data &out_table %if %eval(%qupcase(&debug) in YES Y) eq 0 %then
33 (
34 drop=&variable
35 _new_literal
36 _quote_type
37 _quote_start
38 _quote_end
39 rename=(_tslit_filter_value=&variable)
40 );;
41 set &in_table;
42 length _tslit_filter_value _new_literal $32767 _quote_type $1;
43 /*-----------------------------------------------------
44 * Only run through algorithm if double quotes exist
45 *-----------------------------------------------------*/
46 do while(find(&variable,'"'));
47 /*-------------------------------------------------
48 * Find matching quotes
49 * - Do this for both single and double quotes
50 * in case a quote is inside of a string
51 *-------------------------------------------------*/
52 _quote_start = findc(&variable,'"''');
53 _quote_type = substr(&variable,_quote_start,1);
54 _quote_end = find(&variable,_quote_type,'',_quote_start+1);
55 /*-------------------------------------------------
56 * If single quotes are found do not adjust quotes
57 *-------------------------------------------------*/
58 if _quote_type eq "'" then
59 do;
60 _tslit_filter_value = cat(strip(_tslit_filter_value),
61 substrn(&variable,1,_quote_end));
62 end;
63 /*-------------------------------------------------
64 * If double quotes are found convert string to
65 * single quotes using QUOTE function
66 *-------------------------------------------------*/
67 else
68 do;
69 _new_literal = quote(substrn(&variable,_quote_start+1,_quote_end-_quote_start-1),"'");
70 _tslit_filter_value = cat(strip(_tslit_filter_value),
71 substrn(&variable,1,_quote_start-1),
72 trim(_new_literal));
73 end;
74 /*---------------------------------------------
75 * Remove processed portion from filter value
76 *---------------------------------------------*/
77 &variable = substrn(&variable,_quote_end+1);
78
79 /*--------------------------------------------------
80 * If debugging is requested, output each iteration
81 *--------------------------------------------------*/
82 %if %qupcase(&debug) in Y YES %then
83 output;;
84 end;
85
86 /*--------------------------------------------------
87 * Add any remaining filter to the converted filter
88 *--------------------------------------------------*/
89 _tslit_filter_value = catt(_tslit_filter_value,
90 &variable);
91 run;
92
93 /*------------------------------------------
94 * Get rough listing of filter variables
95 * NOTE: this will include function names
96 *------------------------------------------*/
97 data &out_filter_variable_table(keep=_filter_variable);
98 set &out_table;
99 &variable = upcase(&variable);
100 &variable = tranwrd(&variable,"'''",'');
101 &variable = tranwrd(&variable,' AND ',' ');
102 &variable = tranwrd(&variable,' BETWEEN ',' ');
103 &variable = tranwrd(&variable,' CONTAINS ',' ');
104 &variable = tranwrd(&variable,' EQ ',' ');
105 &variable = tranwrd(&variable,' GE ',' ');
106 &variable = tranwrd(&variable,' GT ',' ');
107 &variable = tranwrd(&variable,' IN ',' ');
108 &variable = tranwrd(&variable,' IS ',' ');
109 &variable = tranwrd(&variable,' LE ',' ');
110 &variable = tranwrd(&variable,' LIKE ',' ');
111 &variable = tranwrd(&variable,' LT ',' ');
112 &variable = tranwrd(&variable,' NE ',' ');
113 &variable = tranwrd(&variable,' NOT ',' ');
114 &variable = tranwrd(&variable,' NOTIN ',' ');
115 &variable = tranwrd(&variable,' OR ',' ');
116 do while(find(&variable,"'"));
117 _quote_start = findc(&variable,"'");
118 _quote_end = findc(&variable,"'",_quote_start+1);
119 substr(&variable,_quote_start,_quote_end-_quote_start+1) = '';
120 end;
121 i = 1;
122 do while(scan(&variable,i,' !$%&()*+,-./;<=>?^|~','o') ne '');
123 _filter_variable = scan(&variable,i,' !$%&()*+,-./;<=>?^|~','o');
124 if nvalid(_filter_variable,'v7') then
125 output;
126 i+1;
127 end;
128 run;
129
130 proc sort data=&out_filter_variable_table(rename=_filter_variable=filter_variable)
131 nodupkey;
132 by filter_variable;
133 run;
134
135%mend irm_tslit_convert;