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