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