Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
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= The list of columns to check for
35 @param [in] desc= (Testing observations) 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
39 @li NONE - Test is a PASS if NONE of the columns exist in &inds
40 @param [out] outds= (work.test_results) The output dataset to contain the
41 results. If it does not exist, it will be created, with the following format:
42 |TEST_DESCRIPTION:$256|TEST_RESULT:$4|TEST_COMMENTS:$256|
43 |---|---|---|
44 |User Provided description|PASS|Column &inds contained ALL columns|
45
46
47 <h4> Related Macros </h4>
48 @li mp_assertdsobs.sas
49 @li mp_assertcolvals.sas
50 @li mp_assertdsobs.sas
51
52 @version 9.2
53 @author Allan Bowe
54
55**/
56
57%macro mp_assertcols(inds,
58 cols=0,
59 test=ALL,
60 desc=0,
61 outds=work.test_results
62)/*/STORE SOURCE*/;
63
64 %mp_abort(iftrue= (&syscc ne 0)
65 ,mac=&sysmacroname
66 ,msg=%str(syscc=&syscc - on macro entry)
67 )
68
69 %local lib ds ;
70 %let lib=%scan(&inds,1,%str(.));
71 %let ds=%scan(&inds,2,%str(.));
72 %let cols=%upcase(&cols);
73
74 %mp_abort(iftrue= (%mf_existds(&lib..&ds)=0)
75 ,mac=&sysmacroname
76 ,msg=%str(&lib..&ds not found!)
77 )
78
79 %mp_abort(iftrue= (&cols=0)
80 ,mac=&sysmacroname
81 ,msg=%str(No cols provided)
82 )
83
84
85 %let test=%upcase(&test);
86
87 %if &test ne ANY and &test ne ALL and &test ne NONE %then %do;
88 %mp_abort(
89 mac=&sysmacroname,
90 msg=%str(Invalid test - &test)
91 )
92 %end;
93
94 /**
95 * now do the actual test!
96 */
97 %local result;
98 %if %mf_existVarList(&inds,&cols)=1 %then %let result=ALL;
99 %else %do;
100 %local targetcols compare;
101 %let targetcols=%upcase(%mf_getvarlist(&inds));
102 %let compare=%mf_wordsinstr1butnotstr2(
103 Str1=&cols,
104 Str2=&targetcols
105 );
106 %if %cmpres(&compare)=%cmpres(&cols) %then %let result=NONE;
107 %else %let result=SOME;
108 %end;
109
110 data;
111 length test_description $256 test_result $4 test_comments $256;
112 test_description=symget('desc');
113 if test_description='0'
114 then test_description="Testing &inds for existence of &test of: &cols";
115
116 test_result='FAIL';
117 test_comments="&sysmacroname: &inds has &result columns ";
118 %if &test=ALL %then %do;
119 %if &result=ALL %then %do;
120 test_result='PASS';
121 %end;
122 %end;
123 %else %if &test=ANY %then %do;
124 %if &result=SOME %then %do;
125 test_result='PASS';
126 %end;
127 %end;
128 %else %if &test=NONE %then %do;
129 %if &result=NONE %then %do;
130 test_result='PASS';
131 %end;
132 %end;
133 %else %do;
134 test_comments="&sysmacroname: Unsatisfied test condition - &test";
135 %end;
136 run;
137
138 %local ds;
139 %let ds=&syslast;
140 proc append base=&outds data=&ds;
141 run;
142 proc sql;
143 drop table &ds;
144
145%mend mp_assertcols;