SASUnit Examples  Version 1.5.0
assertrowexpression.sas
Go to the documentation of this file.
1 
29 %MACRO assertRowExpression(i_libref =
30  ,i_memname =
31  ,i_where =
32  ,i_desc = Check if all observations meet the where expression
33  ,o_maxReportObs = MAX
34  ,o_listVars = _NONE_
35  );
36 
37  /*-- verify correct sequence of calls-----------------------------------------*/
38  %GLOBAL g_inTestcase;
39  %IF &g_inTestcase EQ 1 %THEN %DO;
40  %endTestcall;
41  %END;
42  %ELSE %IF &g_inTestcase NE 2 %THEN %DO;
43  %PUT &g_error.(SASUNIT): assert must be called after initTestcase;
44  %RETURN;
45  %END;
46 
47  %LOCAL l_dsname l_result l_actual l_errmsg l_nobs l_casid l_tstid l_path;
48  %LET l_dsname =%sysfunc(catx(., &i_libref., &i_memname.));
49  %LET l_result = 2;
50  %LET l_actual = 0;
51 
52  %*************************************************************;
53  %*** Check preconditions ***;
54  %*************************************************************;
55  %*** check for valid libref and existence of data set***;
56  %IF (%sysfunc (libref (&i_libref.)) NE 0) %THEN %DO;
57  %LET l_actual =-1;
58  %LET l_errmsg =Libref &i_libref. is not valid!;
59  %GOTO Update;
60  %END;
61  %IF (%sysfunc(exist(&l_dsname.)) EQ 0) %THEN %DO;
62  %LET l_actual =-1;
63  %LET l_errmsg =Dataset &l_dsname. does not exist!;
64  %GOTO Update;
65  %END;
66  %IF (%upcase (&o_maxReportObs.) ne MAX) %THEN %DO;
67  data _null_;
68  num=input (symget ("o_maxReportObs"),??8.);
69  if (num <= 0) then do;
70  PUT "&G_WARNING.(SASUNIT): o_maxReportObs contains an invalid value and is set to MAX";
71  call symputx ("o_maxReportObs", "MAX", 'L');
72  end;
73  run;
74  %END;
75 
76  %*************************************************************;
77  %*** testing the assert condition ***;
78  %*************************************************************;
79  %*** Retrieve number of observations in dataset ***;
80  %LET l_nobs=%_nobs(&l_dsname.);
81  %IF (&l_nobs.=0) %THEN %DO;
82  %LET l_result = 0;
83  %GOTO Update;
84  %END;
85 
86  /*-- get current ids for test case and test --------- ------------------------*/
87  %_getScenarioTestId (i_scnid=&g_scnid, r_casid=l_casid, r_tstid=l_tstid);
88 
89  %*** create subfolder ***;
90  %_createTestSubfolder (i_assertType =assertRowExpression
91  ,i_scnid =&g_scnid.
92  ,i_casid =&l_casid.
93  ,i_tstid =&l_tstid.
94  ,r_path =l_path
95  );
96 
97  libname _areLib "&l_path.";
98 
99  %*** Count matching observations ***;
100  PROC SQL NOPRINT;
101  select count (*) into :l_actual
102  from &l_dsname.
103  where &i_where.;
104  QUIT;
105  %IF (&syserr. ne 0) %THEN %DO;
106  %LET l_actual=-1;
107  %LET l_errmsg=Where expression is not valid!;
108  data _areLib.ViolatingObservations;
109  set &l_dsname;
110  stop;
111  run;
112  libname _areLib clear;
113  %GOTO Update;
114  %END;
115 
116  options obs=&o_maxReportObs.;
117  data _areLib.ViolatingObservations;
118  set &l_dsname (where=(not (&i_where.)));
119  %IF (&o_listVars. ne _NONE_) %THEN %DO;
120  keep &o_listVars.;
121  %END;
122  run;
123  %LET l_rc=&syserr.;
124  options obs=MAX;
125  libname _areLib clear;
126  %IF (&l_rc. ne 0) %THEN %DO;
127  %LET l_actual=-1;
128  %LET l_errmsg=Error writing table with condition violating observations!;
129  %GOTO Update;
130  %END;
131 
132  %*** Determine results***;
133  %if (&l_actual. eq &l_nobs.) %then %do;
134  %let l_result = 0;
135  %end;
136  %else %do;
137  %LET l_errmsg =Some observations are violating the condition!;
138  %end;
139 
140 %Update:
141  *** update result in test database ***;
142  %_asserts(i_type = assertRowExpression
143  ,i_expected = &l_nobs.
144  ,i_actual = &l_actual.
145  ,i_desc = &i_desc.
146  ,i_result = &l_result.
147  ,i_errmsg = &l_errmsg.
148  )
149 %MEND assertRowExpression;