Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_assertcols.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Asserts the existence (or not) of columns
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 %mp_assertcols(sashelp.class,
10 cols=name age sex,
11 test=ALL,
12 desc=check all columns exist
13 )
14
15 %mp_assertcols(sashelp.class,
16 cols=a b c,
17 test=NONE
18 )
19
20 %mp_assertcols(sashelp.class,
21 cols=age depth,
22 test=ANY
23 )
24
25 <h4> SAS Macros </h4>
26 @li mf_existds.sas
27 @li mf_existvarlist.sas
28 @li mf_getvarlist.sas
29 @li mf_wordsinstr1butnotstr2.sas
30 @li mp_abort.sas
31
32
33 @param [in] inds The input library.dataset to test for values
34 @param [in] cols= (0) The list of columns to check for
35 @param [in] desc= (0) The user provided test description
36 @param [in] test= (ALL) The test to apply. Valid values are:
37 @li ALL - Test is a PASS if ALL columns exist in &inds
38 @li ANY - Test is a PASS if ANY of the columns exist in &inds - including
39 when all of them do
40 @li NONE - Test is a PASS if NONE of the columns exist in &inds
41 @param [out] outds= (work.test_results) The output dataset to contain the
42 results. If it does not exist, it will be created, with the following format:
43 |TEST_DESCRIPTION:$256|TEST_RESULT:$5|TEST_COMMENTS:$256|
44 |---|---|---|
45 |User Provided description|PASS|Dataset &inds contained ALL columns|
46
47
48 <h4> Related Macros </h4>
49 @li mp_assertdsobs.sas
50 @li mp_assertcolvals.sas
51 @li mp_assertdsobs.sas
52
53 @version 9.2
54 @author Allan Bowe
55
56**/
57
58%macro mp_assertcols(inds,
59 cols=0,
60 test=ALL,
61 desc=0,
62 outds=work.test_results
63)/*/STORE SOURCE*/;
64
65 %mp_abort(iftrue= (&syscc ne 0)
66 ,mac=&sysmacroname
67 ,msg=%str(syscc=&syscc - on macro entry)
68 )
69
70 %local lib ds ;
71 %let lib=%scan(&inds,1,%str(.));
72 %let ds=%scan(&inds,2,%str(.));
73 %let cols=%upcase(&cols);
74
75 %mp_abort(iftrue= (%mf_existds(&lib..&ds)=0)
76 ,mac=&sysmacroname
77 ,msg=%str(&lib..&ds not found!)
78 )
79
80 %mp_abort(iftrue= (&cols=0)
81 ,mac=&sysmacroname
82 ,msg=%str(No cols provided)
83 )
84
85
86 %let test=%upcase(&test);
87
88 %if &test ne ANY and &test ne ALL and &test ne NONE %then %do;
89 %mp_abort(
90 mac=&sysmacroname,
91 msg=%str(Invalid test - &test)
92 )
93 %end;
94
95 /**
96 * now do the actual test!
97 */
98 %local result;
99 %if %mf_existVarList(&inds,&cols)=1 %then %let result=ALL;
100 %else %do;
101 %local targetcols compare;
102 %let targetcols=%upcase(%mf_getvarlist(&inds));
103 %let compare=%mf_wordsinstr1butnotstr2(
104 Str1=&cols,
105 Str2=&targetcols
106 );
107 %if %cmpres(&compare)=%cmpres(&cols) %then %let result=NONE;
108 %else %let result=SOME;
109 %end;
110
111 data;
112 length test_result $5 test_description $256 test_comments $256;
113 test_description=symget('desc');
114 if test_description='0'
115 then test_description="Testing &inds for existence of &test of: &cols";
116
117 test_result='FAIL';
118 test_comments="&sysmacroname: &inds has &result columns ";
119 %if &test=ALL %then %do;
120 %if &result=ALL %then %do;
121 test_result='PASS';
122 %end;
123 %end;
124 %else %if &test=ANY %then %do;
125 /* SOME or ALL - if every column exists then any of them does too */
126 %if &result ne NONE %then %do;
127 test_result='PASS';
128 %end;
129 %end;
130 %else %if &test=NONE %then %do;
131 %if &result=NONE %then %do;
132 test_result='PASS';
133 %end;
134 %end;
135 %else %do;
136 test_comments="&sysmacroname: Unsatisfied test condition - &test";
137 %end;
138 run;
139
140 %local ds;
141 %let ds=&syslast;
142 proc append base=&outds data=&ds;
143 run;
144 proc sql;
145 drop table &ds;
146
147%mend mp_assertcols;