Loading...
Searching...
No Matches
mp_assertdsobs.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Asserts the number of observations in a dataset
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_assertdsobs(sashelp.class) %* tests if any observations are present;
10
11 %mp_assertdsobs(sashelp.class,test=ATLEAST 10) %* pass if >9 obs present;
12
13 %mp_assertdsobs(sashelp.class,test=ATMOST 20) %* pass if <21 obs present;
14
15
16 @param [in] inds input dataset to test for presence of observations
17 @param [in] desc= (Testing observations) The user provided test description
18 @param [in] test= (HASOBS) The test to apply. Valid values are:
19 @li HASOBS - Test is a PASS if the input dataset has any observations
20 @li EMPTY - Test is a PASS if input dataset is empty
21 @li EQUALS [integer] - Test passes if row count matches the provided integer
22 @li ATLEAST [integer] - Test passes if row count is more than or equal to
23 the provided integer
24 @li ATMOST [integer] - Test passes if row count is less than or equal to
25 the provided integer
26 @param [out] outds= (work.test_results) The output dataset to contain the
27 results. If it does not exist, it will be created, with the following format:
28 |TEST_DESCRIPTION:$256|TEST_RESULT:$5|TEST_COMMENTS:$256|
29 |---|---|---|
30 |User Provided description|PASS|Dataset &inds has XX obs|
31
32 <h4> SAS Macros </h4>
33 @li mddl_assert.sas
34 @li mf_getuniquename.sas
35 @li mf_nobs.sas
36 @li mp_abort.sas
37
38 <h4> Related Macros </h4>
39 @li mp_assertcolvals.sas
40 @li mp_assert.sas
41 @li mp_assertcols.sas
42
43 @version 9.2
44 @author Allan Bowe
45
46**/
47
48%macro mp_assertdsobs(inds,
49 test=HASOBS,
50 desc=Testing observations,
51 outds=work.test_results
52)/*/STORE SOURCE*/;
53
54 %local nobs ds;
55 %let nobs=%mf_nobs(&inds);
56 %let test=%upcase(&test);
57 %let ds=work.%mf_getuniquename(prefix=mp_assertdsobs);
58
59 %if %substr(&test.xxxxx,1,6)=EQUALS %then %do;
60 %let val=%scan(&test,2,%str( ));
61 %mp_abort(iftrue= (%DATATYP(&val)=CHAR)
62 ,mac=&sysmacroname
63 ,msg=%str(Invalid test - &test, expected EQUALS [integer])
64 )
65 %let test=EQUALS;
66 %end;
67 %else %if %substr(&test.xxxxxxx,1,7)=ATLEAST %then %do;
68 %let val=%scan(&test,2,%str( ));
69 %mp_abort(iftrue= (%DATATYP(&val)=CHAR)
70 ,mac=&sysmacroname
71 ,msg=%str(Invalid test - &test, expected ATLEAST [integer])
72 )
73 %let test=ATLEAST;
74 %end;
75 %else %if %substr(&test.xxxxxxx,1,7)=ATMOST %then %do;
76 %let val=%scan(&test,2,%str( ));
77 %mp_abort(iftrue= (%DATATYP(&val)=CHAR)
78 ,mac=&sysmacroname
79 ,msg=%str(Invalid test - &test, expected ATMOST [integer])
80 )
81 %let test=ATMOST;
82 %end;
83 %else %if &test ne HASOBS and &test ne EMPTY %then %do;
84 %mp_abort(
85 mac=&sysmacroname,
86 msg=%str(Invalid test - &test)
87 )
88 %end;
89
90 %mddl_assert(&ds)
91 data &ds;
92 if 0 then set &ds;
93 test_description=symget('desc');
94 test_result='FAIL';
95 test_comments="&sysmacroname: Dataset &inds has &nobs observations.";
96 test_comments=test_comments!!" Test was "!!symget('test');
97 %if &test=HASOBS %then %do;
98 if &nobs>0 then test_result='PASS';
99 %end;
100 %else %if &test=EMPTY %then %do;
101 if &nobs=0 then test_result='PASS';
102 %end;
103 %else %if &test=EQUALS %then %do;
104 if &nobs=&val then test_result='PASS';
105 %end;
106 %else %if &test=ATLEAST %then %do;
107 if &nobs ge &val then test_result='PASS';
108 %end;
109 %else %if &test=ATMOST %then %do;
110 if &nobs le &val then test_result='PASS';
111 %end;
112 %else %do;
113 test_comments="&sysmacroname: Unsatisfied test condition - &test";
114 %end;
115 run;
116
117 proc append base=&outds data=&ds;
118 run;
119
120 proc sql;
121 drop table &ds;
122
123%mend mp_assertdsobs;