SASUnit Examples  Version 1.5.0
assertcolumns.sas
Go to the documentation of this file.
1 
63 %MACRO assertColumns (i_expected =
64  ,i_actual =
65  ,i_desc = Compare datasets
66  ,i_fuzz =
67  ,i_allow = DSLABEL LABEL COMPVAR
68  ,i_id =
69  ,o_maxReportObs = max
70  ,i_maxReportObs = /* obsolete */
71  ,i_include =
72  ,i_exclude =
73  );
74 
75  %LOCAL l_allowSymbols l_i l_j l_symboli l_symbolj l_potenz l_mask l_casid l_tstid l_path l_errMsg;
76  %LET l_errMsg=;
77 
78  /*-- possible values for i_allow ---------------------------------------------*/
79  %LET l_allowSymbols=
80  DSLABEL
81  DSTYPE
82  INFORMAT
83  FORMAT
84  LENGTH
85  LABEL
86  BASEOBS
87  COMPOBS
88  BASEBY
89  COMPBY
90  BASEVAR
91  COMPVAR
92  VALUE
93  TYPE
94  BYVAR
95  ERROR
96  ;
97 
98  /*-- check parameter i_allow ------------------------------------------------*/
99  %LET l_mask=0;
100  %LET l_i=0;
101  %DO %WHILE(%length(%scan(&i_allow,%eval(&l_i+1),%str( ))));
102  %LET l_i = %eval (&l_i + 1);
103  %LET l_symboli = %upcase(%scan(&i_allow, &l_i, %str( )));
104  %LET l_j=0;
105  %LET l_potenz=1;
106  %DO %WHILE(%length(%scan(&l_allowSymbols,%eval(&l_j+1),%str( ))));
107  %LET l_j = %eval (&l_j+1);
108  %LET l_symbolj = %scan(&l_allowSymbols, &l_j, %str( ));
109  %IF &l_symboli = &l_symbolj %THEN %goto label1;
110  %LET l_potenz = &l_potenz*2;
111  %END;
112  %PUT &g_error.(SASUNIT): assertColumns: invalid symbol &l_symboli in parameter i_allow;
113  %RETURN;
114  %label1:
115  %LET l_mask = %sysfunc(bor(&l_mask, &l_potenz));
116  %END;
117 
118  /*-- support obsolete parameter i_maxReportObs -------------------------------*/
119  %IF %length(&i_maxReportObs) %then %LET o_maxReportObs = &i_maxReportObs;
120 
121  /*-- input from parameter i_include should override the input from i_exclude--*/
122  %IF (%length(&i_include) > 0 AND %length(&i_exclude) > 0) %THEN %DO;
123  %PUT &g_warning.(SASUNIT): Both parameters i_include and i_exclude have been set.;
124  %PUT &g_warning.(SASUNIT): I_exclude parameter will be dropped;
125  %LET i_exclude =;
126  %END;
127 
128  /*-- verify correct sequence of calls-----------------------------------------*/
129  %GLOBAL g_inTestcase;
130  %IF &g_inTestcase EQ 1 %THEN %DO;
131  %endTestcall;
132  %END;
133  %ELSE %IF &g_inTestcase NE 2 %THEN %DO;
134  %PUT &g_error.(SASUNIT): assert must be called after initTestcase;
135  %RETURN;
136  %END;
137 
138  /*-- get current ids for test case and test --------- ------------------------*/
139  %_getScenarioTestId (i_scnid=&g_scnid, r_casid=l_casid, r_tstid=l_tstid);
140 
141  %*** create subfolder ***;
142  %_createTestSubfolder (i_assertType =assertcolumns
143  ,i_scnid =&g_scnid.
144  ,i_casid =&l_casid.
145  ,i_tstid =&l_tstid.
146  ,r_path =l_path
147  );
148 
149  libname _acLib "&l_path.";
150 
151  /*-- check if actual dataset exists ------------------------------------------*/
152  %LOCAL l_rc l_actual;
153  %IF NOT %sysfunc(exist(&i_actual,DATA)) AND NOT %sysfunc(exist(&i_actual,VIEW)) %THEN %DO;
154  %LET l_rc=2;
155  %LET l_actual=ERROR: actual table not found.;
156  %LET l_errMsg=Actual table (&i_actual.) could not be found!;
157  %END;
158 
159  /*-- check if expected dataset exists ----------------------------------------*/
160  %ELSE %IF NOT %sysfunc(exist(&i_expected,DATA)) AND NOT %sysfunc(exist(&i_expected,VIEW))%THEN %DO;
161  %LET l_rc=2;
162  %LET l_actual=&l_actual ERROR: expected table not found.;
163  %LET l_errMsg=Expected table (&i_expected.) could not be found!;
164  %END;
165 
166  /*-- compare tables ----------------------------------------------------------*/
167  %ELSE %DO;
168 
169  %LOCAL l_formchar l_orientation l_compResult;
170  %LET l_formchar=%sysfunc(getoption(formchar));
171  %LET l_orientation=%sysfunc(getoption(orientation));
172 
173  OPTIONS FORMCHAR="|----|+|---+=|-/\<>*";
174  OPTIONS ORIENTATION=portrait;
175 
176  ODS DOCUMENT NAME=_acLib._columns_rep(WRITE);
177  TITLE;
178  FOOTNOTE;
179  PROC COMPARE
180  BASE=&i_expected %IF %quote(&i_exclude) NE %THEN %str(%(DROP= &i_exclude%));
181  COMPARE=&i_actual %IF %quote(&i_exclude) NE %THEN %str(%(DROP= &i_exclude%));
182 
183  %IF %quote(&i_fuzz) NE %THEN CRITERION=&i_fuzz METHOD=ABSOLUTE;
184  ;
185  %IF %quote(&i_id) NE %THEN %str(ID &i_id;);
186  %IF %quote(&i_include) NE %THEN %str(VAR &i_include;);
187  RUN;
188  %PUT &g_note.(SASUNIT): sysinfo = &sysinfo;
189  %LET l_compResult = &sysinfo;
190 
191  ODS DOCUMENT CLOSE;
192  OPTIONS FORMCHAR="&l_formchar.";
193  OPTIONS ORIENTATION=&l_orientation.;
194 
195  /*-- check proc compare result -----------------------------------------------*/
196  %LET l_rc=%eval((%sysfunc(bxor(%sysfunc(bor(&l_mask,&l_compResult)),&l_mask)) NE 0)*2);
197 
198  /*-- format compare result ---------------------------------------------------*/
199  %LET l_j=0;
200  %LET l_potenz=1;
201  %DO %WHILE(%length(%scan(&l_allowSymbols,%eval(&l_j+1),%str( ))));
202  %LET l_j = %eval (&l_j+1);
203  %LET l_symbolj = %scan(&l_allowSymbols, &l_j, %str( ));
204  %IF %sysfunc(band(&l_compResult, &l_potenz)) %THEN %DO;
205  %LET l_actual = &l_actual &l_symbolj;
206  %END;
207  %LET l_potenz = &l_potenz*2;
208  %END;
209 
210  %IF (&l_rc eq 2) %THEN %DO;
211  %LET l_errMsg=%str(Allowed return codes are %upcase(&i_allow), comparing &i_expected. with &i_actual. resulted in these return codes &l_actual.);
212  %END;
213  %END; /* i_expected and i_actual exist */
214 
215  /*-- update comparison result in test database -------------------------------*/
216  %_asserts(i_type = assertColumns
217  ,i_expected = %upcase(&i_allow)
218  ,i_actual = &l_actual.
219  ,i_desc = &i_desc.
220  ,i_result = &l_rc.
221  ,i_errMsg = &l_errMsg.
222  );
223 
224  /*-- write dataset set the target area ---------------------------------------*/
225  %IF &o_maxreportobs NE 0 %THEN %DO;
226  %IF %sysfunc(exist(&i_expected,DATA)) OR %sysfunc(exist(&i_expected,VIEW)) %THEN %DO;
227  DATA _acLib._columns_exp;
228  SET &i_expected (obs=&o_maxReportObs.);
229  RUN;
230  %END;
231 
232  %IF %sysfunc(exist(&i_actual,DATA)) OR %sysfunc(exist(&i_actual,VIEW)) %THEN %DO;
233  DATA _acLib._columns_act;
234  SET &i_actual (obs=&o_maxReportObs.);
235  RUN;
236  %END;
237  %END;
238 
239  libname _acLib;
240 
241 %MEND assertColumns;