Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
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 <h4> SAS Macros </h4>
12 @li mf_nobs.sas
13 @li mp_abort.sas
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:$4|TEST_COMMENTS:$256|
29 |---|---|---|
30 |User Provided description|PASS|Dataset &inds has XX obs|
31
32 <h4> Related Macros </h4>
33 @li mp_assertcolvals.sas
34 @li mp_assert.sas
35 @li mp_assertcols.sas
36
37 @version 9.2
38 @author Allan Bowe
39
40**/
41
42%macro mp_assertdsobs(inds,
43 test=HASOBS,
44 desc=Testing observations,
45 outds=work.test_results
46)/*/STORE SOURCE*/;
47
48 %local nobs;
49 %let nobs=%mf_nobs(&inds);
50 %let test=%upcase(&test);
51
52 %if %substr(&test.xxxxx,1,6)=EQUALS %then %do;
53 %let val=%scan(&test,2,%str( ));
54 %mp_abort(iftrue= (%DATATYP(&val)=CHAR)
55 ,mac=&sysmacroname
56 ,msg=%str(Invalid test - &test, expected EQUALS [integer])
57 )
58 %let test=EQUALS;
59 %end;
60 %else %if %substr(&test.xxxxxxx,1,7)=ATLEAST %then %do;
61 %let val=%scan(&test,2,%str( ));
62 %mp_abort(iftrue= (%DATATYP(&val)=CHAR)
63 ,mac=&sysmacroname
64 ,msg=%str(Invalid test - &test, expected ATLEAST [integer])
65 )
66 %let test=ATLEAST;
67 %end;
68 %else %if %substr(&test.xxxxxxx,1,7)=ATMOST %then %do;
69 %let val=%scan(&test,2,%str( ));
70 %mp_abort(iftrue= (%DATATYP(&val)=CHAR)
71 ,mac=&sysmacroname
72 ,msg=%str(Invalid test - &test, expected ATMOST [integer])
73 )
74 %let test=ATMOST;
75 %end;
76 %else %if &test ne HASOBS and &test ne EMPTY %then %do;
77 %mp_abort(
78 mac=&sysmacroname,
79 msg=%str(Invalid test - &test)
80 )
81 %end;
82
83 data;
84 length test_description $256 test_result $4 test_comments $256;
85 test_description=symget('desc');
86 test_result='FAIL';
87 test_comments="&sysmacroname: Dataset &inds has &nobs observations.";
88 test_comments=test_comments!!" Test was "!!symget('test');
89 %if &test=HASOBS %then %do;
90 if &nobs>0 then test_result='PASS';
91 %end;
92 %else %if &test=EMPTY %then %do;
93 if &nobs=0 then test_result='PASS';
94 %end;
95 %else %if &test=EQUALS %then %do;
96 if &nobs=&val then test_result='PASS';
97 %end;
98 %else %if &test=ATLEAST %then %do;
99 if &nobs ge &val then test_result='PASS';
100 %end;
101 %else %if &test=ATMOST %then %do;
102 if &nobs le &val then test_result='PASS';
103 %end;
104 %else %do;
105 test_comments="&sysmacroname: Unsatisfied test condition - &test";
106 %end;
107 run;
108
109 %local ds;
110 %let ds=&syslast;
111
112 proc append base=&outds data=&ds;
113 run;
114
115 proc sql;
116 drop table &ds;
117
118%mend mp_assertdsobs;