Loading...
Searching...
No Matches
mp_assertcolvals.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Asserts the values in a column
4 @details Useful in the context of writing sasjs tests. The results of the
5 test are _appended_ to the &outds. table.
6
7 Example usage:
8
9 data work.checkds;
10 do checkval='Jane','James','Jill';
11 output;
12 end;
13 run;
14 %mp_assertcolvals(sashelp.class.name,
15 checkvals=work.checkds.checkval,
16 desc=At least one value has a match,
17 test=ANYVAL
18 )
19
20 data work.check;
21 do val='M','F';
22 output;
23 end;
24 run;
25 %mp_assertcolvals(sashelp.class.sex,
26 checkvals=work.check.val,
27 desc=All values have a match,
28 test=ALLVALS
29 )
30
31 <h4> SAS Macros </h4>
32 @li mddl_assert.sas
33 @li mf_existds.sas
34 @li mf_getuniquename.sas
35 @li mf_nobs.sas
36 @li mp_abort.sas
37
38
39 @param [in] indscol The input library.dataset.column to test for values
40 @param [in] checkvals= (0) A library.dataset.column value containing a UNIQUE
41 list of values to be compared against the source (indscol).
42 @param [in] desc= (Testing observations) The user provided test description
43 @param [in] test= (ALLVALS) The test to apply. Valid values are:
44 @li ALLVALS - Test is a PASS if ALL values have a match in checkvals
45 @li ANYVAL - Test is a PASS if at least 1 value has a match in checkvals
46 @li NOVAL - Test is a PASS if there are NO matches in checkvals
47 @param [out] outds= (work.test_results) The output dataset to contain the
48 results. If it does not exist, it will be created, with the following format:
49 |TEST_DESCRIPTION:$256|TEST_RESULT:$5|TEST_COMMENTS:$256|
50 |---|---|---|
51 |User Provided description|PASS|Column &indscol contained ALL target vals|
52
53
54 <h4> Related Macros </h4>
55 @li mp_assertdsobs.sas
56
57 @version 9.2
58 @author Allan Bowe
59
60**/
61
62%macro mp_assertcolvals(indscol,
63 checkvals=0,
64 test=ALLVALS,
65 desc=mp_assertcolvals - no desc provided,
66 outds=work.test_results
67)/*/STORE SOURCE*/;
68
69 %mp_abort(iftrue= (&syscc ne 0)
70 ,mac=&sysmacroname
71 ,msg=%str(syscc=&syscc - on macro entry)
72 )
73
74 %local lib ds col clib cds ccol nobs;
75 %let lib=%scan(&indscol,1,%str(.));
76 %let ds=%scan(&indscol,2,%str(.));
77 %let col=%scan(&indscol,3,%str(.));
78 %mp_abort(iftrue= (%mf_existds(&lib..&ds)=0)
79 ,mac=&sysmacroname
80 ,msg=%str(&lib..&ds not found!)
81 )
82
83 %mp_abort(iftrue= (&checkvals=0)
84 ,mac=&sysmacroname
85 ,msg=%str(Set CHECKVALS to a library.dataset.column containing check vals)
86 )
87 %let clib=%scan(&checkvals,1,%str(.));
88 %let cds=%scan(&checkvals,2,%str(.));
89 %let ccol=%scan(&checkvals,3,%str(.));
90 %mp_abort(iftrue= (%mf_existds(&clib..&cds)=0)
91 ,mac=&sysmacroname
92 ,msg=%str(&clib..&cds not found!)
93 )
94 %let nobs=%mf_nobs(&clib..&cds);
95 %mp_abort(iftrue= (&nobs=0)
96 ,mac=&sysmacroname
97 ,msg=%str(&clib..&cds is empty!)
98 )
99
100 %let test=%upcase(&test);
101
102 %if &test ne ALLVALS and &test ne ANYVAL and &test ne NOVAL %then %do;
103 %mp_abort(
104 mac=&sysmacroname,
105 msg=%str(Invalid test - &test)
106 )
107 %end;
108
109 %local result orig;
110 %let result=-1;
111 %let orig=-1;
112 proc sql noprint;
113 select count(*) into: result trimmed
114 from &lib..&ds
115 where &col not in (
116 select &ccol from &clib..&cds
117 );
118 select count(*) into: orig trimmed from &lib..&ds;
119 quit;
120
121 %local notfound tmp1 tmp2;
122 %let tmp1=%mf_getuniquename();
123 %let tmp2=%mf_getuniquename();
124
125 /* this is a bit convoluted - but using sql outobs=10 throws warnings */
126 proc sql noprint;
127 create view &tmp1 as
128 select distinct &col
129 from &lib..&ds
130 where &col not in (
131 select &ccol from &clib..&cds
132 );
133 data &tmp2;
134 set &tmp1;
135 if _n_>10 then stop;
136 run;
137 proc sql;
138 select distinct &col into: notfound separated by ' ' from &tmp2;
139
140
141 %mp_abort(iftrue= (&syscc ne 0)
142 ,mac=&sysmacroname
143 ,msg=%str(syscc=&syscc after macro query)
144 )
145
146 %local rsds;
147 %let rsds=work.%mf_getuniquename(prefix=mp_assertcolvals);
148 %mddl_assert(&rsds)
149 data &rsds;
150 if 0 then set &rsds;
151 test_description=symget('desc');
152 test_result='FAIL';
153 test_comments="&sysmacroname: &lib..&ds..&col has &result/&orig values "
154 !!"not in &clib..&cds..&ccol.. First 10 vals:"!!symget('notfound');
155 %if &test=ANYVAL %then %do;
156 if &result < &orig then test_result='PASS';
157 %end;
158 %else %if &test=ALLVALS %then %do;
159 if &result=0 then test_result='PASS';
160 %end;
161 %else %if &test=NOVAL %then %do;
162 if &result=&orig then test_result='PASS';
163 %end;
164 %else %do;
165 test_comments="&sysmacroname: Unsatisfied test condition - &test";
166 %end;
167 run;
168
169 proc append base=&outds data=&rsds;
170 run;
171 proc sql;
172 drop table &rsds;
173
174%mend mp_assertcolvals;