Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_filtercheck.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_filtercheck macro
4
5 <h4> SAS Macros </h4>
6 @li mp_filtercheck.sas
7 @li mp_assertdsobs.sas
8 @li mp_assert.sas
9 @li mp_assertscope.sas
10
11**/
12
13/* set up test data */
14data work.class ;
15length name $8 sex $1 age height weight 8;
16infile cards dsd;
17input Name:$char. Sex :$char. Age Height Weight;
18datalines4;
19Alfred,M,14,69,112.5
20Alice,F,13,56.5,84
21Barbara,F,13,65.3,98
22Carol,F,14,62.8,102.5
23Henry,M,14,63.5,102.5
24James,M,12,57.3,83
25Jane,F,12,59.8,84.5
26Janet,F,15,62.5,112.5
27Jeffrey,M,13,62.5,84
28John,M,12,59,99.5
29Joyce,F,11,51.3,50.5
30Judy,F,14,64.3,90
31Louise,F,12,56.3,77
32Mary,F,15,66.5,112
33Philip,M,16,72,150
34Robert,M,12,64.8,128
35Ronald,M,15,67,133
36Thomas,M,11,57.5,85
37William,M,15,66.5,112
38;;;;
39run;
40
41/* VALID filter conditions */
42data work.inds;
43 infile datalines4 dsd;
44 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
45 OPERATOR_NM:$10. RAW_VALUE:$4000.;
46datalines4;
47AND,AND,1,AGE,=,12
48AND,AND,1,SEX,<=,"'M'"
49AND,OR,2,Name,NOT IN,"('Jane','Alfred')"
50AND,OR,2,Weight,>=,77.7
51AND,OR,2,Weight,NE,77.7
52AND,AND,1,age,=,.A
53AND,AND,1,height,<,.B
54AND,AND,1,age,IN,"(.a,.b,.)"
55AND,AND,1,age,IN,"(.A)"
56AND,AND,1,AGE,=,AGE
57AND,AND,1,AGE,<,Weight
58AND,AND,1,AGE,BETWEEN,"HEIGHT AND WEIGHT"
59AND,OR,2,Name,=,name
60;;;;
61run;
62
63%mp_assertscope(SNAPSHOT)
64%mp_filtercheck(work.inds,
65 targetds=work.class,
66 outds=work.badrecords,
67 abort=NO
68)
69%mp_assertscope(COMPARE)
70
71%let syscc=0;
72%mp_assertdsobs(work.badrecords,
73 desc=Valid filter query,
74 test=EMPTY,
75 outds=work.test_results
76)
77
78/* invalid column */
79data work.inds;
80 infile datalines4 dsd;
81 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
82 OPERATOR_NM:$10. RAW_VALUE:$4000.;
83datalines4;
84AND,AND,1,invalid,=,12
85AND,AND,1,SEX,<=,"'M'"
86AND,OR,2,Name,NOT IN,"('Jane','Alfred')"
87AND,OR,2,Weight,>=,7
88;;;;
89run;
90%mp_filtercheck(work.inds,
91 targetds=work.class,
92 outds=work.badrecords,
93 abort=NO
94)
95%let syscc=0;
96%mp_assertdsobs(work.badrecords,
97 desc=Invalid column name,
98 test=HASOBS,
99 outds=work.test_results
100)
101
102/* invalid raw value */
103data work.inds;
104 infile datalines4 dsd;
105 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
106 OPERATOR_NM:$10. RAW_VALUE:$4000.;
107datalines4;
108AND,OR,2,Name,NOT IN,"(''''Jane','Alfred')"
109;;;;
110run;
111
112%mp_filtercheck(work.inds,
113 targetds=work.class,
114 outds=work.badrecords,
115 abort=NO
116)
117%let syscc=0;
118%mp_assertdsobs(work.badrecords,
119 desc=Invalid raw value,
120 test=HASOBS,
121 outds=work.test_results
122)
123
124/* invalid IN value */
125data work.inds;
126 infile datalines4 dsd;
127 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
128 OPERATOR_NM:$10. RAW_VALUE:$4000.;
129datalines4;
130AND,OR,2,age,IN,"(.,.a,X)"
131;;;;
132run;
133
134%mp_filtercheck(work.inds,
135 targetds=work.class,
136 outds=work.badrecords,
137 abort=NO
138)
139%let syscc=0;
140%mp_assertdsobs(work.badrecords,
141 desc=Invalid IN value,
142 test=HASOBS,
143 outds=work.test_results
144)
145
146/* Code injection - column name */
147data work.inds;
148 infile datalines4 dsd;
149 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
150 OPERATOR_NM:$10. RAW_VALUE:$4000.;
151datalines4;
152AND,AND,1,%abort,=,12
153AND,OR,2,Weight,>=,7
154;;;;
155run;
156
157%mp_filtercheck(work.inds,
158 targetds=work.class,
159 outds=work.badrecords,
160 abort=NO
161)
162%let syscc=0;
163%mp_assertdsobs(work.badrecords,
164 desc=Code injection - column name,
165 test=HASOBS,
166 outds=work.test_results
167)
168
169/* Code injection - raw values*/
170data work.inds;
171 infile datalines4 dsd;
172 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
173 OPERATOR_NM:$10. RAW_VALUE:$4000.;
174datalines4;
175AND,AND,1,age,=,;;%abort
176;;;;
177run;
178%mp_filtercheck(work.inds,
179 targetds=work.class,
180 outds=work.badrecords,
181 abort=NO
182)
183%let syscc=0;
184%mp_assertdsobs(work.badrecords,
185 desc=Code injection - raw value abort,
186 test=HASOBS,
187 outds=work.test_results
188)
189
190/* Supply variables with incorrect types */
191data work.inds;
192 infile datalines4 dsd;
193 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
194 OPERATOR_NM:$10. RAW_VALUE:8.;
195datalines4;
196AND,AND,1,age,=,0
197;;;;
198run;
199%let syscc=0;
200%mp_filtercheck(work.inds,
201 targetds=work.class,
202 outds=work.badrecords,
203 abort=NO
204)
205%mp_assert(iftrue=(&syscc=42),
206 desc=Throw error if RAW_VALUE is incorrect,
207 outds=work.test_results
208)
209%let syscc=0;
210
211
212/* invalid IN value (cannot use var names) */
213data work.inds;
214 infile datalines4 dsd;
215 input GROUP_LOGIC:$3. SUBGROUP_LOGIC:$3. SUBGROUP_ID:8. VARIABLE_NM:$32.
216 OPERATOR_NM:$10. RAW_VALUE:$4000.;
217datalines4;
218AND,AND,1,AGE,NOT IN,"(height, age)"
219;;;;
220run;
221
222%mp_filtercheck(work.inds,
223 targetds=work.class,
224 outds=work.badrecords,
225 abort=NO
226)
227%let syscc=0;
228%mp_assertdsobs(work.badrecords,
229 desc=Invalid IN syntax,
230 test=HASOBS,
231 outds=work.test_results
232)