SASUnit Examples  Version 1.5.0
_assertlibrary.sas
Go to the documentation of this file.
1 
34 %macro _assertlibrary (i_actual =_NONE_
35  ,i_expected =_NONE_
36  ,i_ExcludeList =_NONE_
37  ,i_CompareCheck=_NONE_
38  ,i_LibraryCheck=_NONE_
39  ,i_id =_NONE_
40  ,i_fuzz =_NONE_
41  ,i_scnid =
42  ,i_casid =
43  ,i_tstid =
44  ,o_result =
45  );
46 
47  %LOCAL l_casid l_tstid AnzCompares l_subfoldername l_path;
48 
49  %*** get table names from the two libraries ***;
50  proc sql noprint;
51  create table WORK._assertLibraryActual as
52  select libname as BaseLibname,
53  memname as BaseMemName,
54  nlobs as BaseObs format=commax18.,
55  nvar as BaseNVar format=commax18.
56  from dictionary.tables
57  where libname = "%upcase(&i_actual)"
58  %if (&i_ExcludeList. ne _NONE_) %then %do;
59  AND memname not in (
60  %let counter=1;
61  %let l_id_col = %scan (&i_ExcludeList.,&counter., %str ( ));
62  %do %while (&l_id_col. ne );
63  "&l_id_col."
64  %let counter=%eval (&counter.+1);
65  %let l_id_col = %scan (&i_ExcludeList.,&counter., %str ( ));
66  %end;
67  )
68  %end;
69  order by memname;
70  create table WORK._assertLibraryExpected as
71  select libname as CmpLibname,
72  memname as CmpMemName,
73  nlobs as CmpObs format=commax18.,
74  nvar as CmpNVar format=commax18.
75  from dictionary.tables
76  where libname = "%upcase(&i_expected)"
77  %if (&i_ExcludeList. ne _NONE_) %then %do;
78  AND memname not in (
79  %let counter=1;
80  %let l_id_col = %scan (&i_ExcludeList.,&counter., %str ( ));
81  %do %while (&l_id_col. ne );
82  "&l_id_col."
83  %let counter=%eval (&counter.+1);
84  %let l_id_col = %scan (&i_ExcludeList.,&counter., %str ( ));
85  %end;
86  )
87  %end;
88  order by memname;
89  quit;
90 
91  %*** flag tables according to check results ***;
92  data work._ergebnis;
93  length DoCompare CompareFailed l_rc 8;
94  merge WORK._assertLibraryActual (in=InAct rename=(BaseMemname=memname))
95  WORK._assertLibraryExpected (in=InExp rename=(CmpMemname=memname));
96  by memname;
97  l_rc = .;
98  InActual=InAct;
99  InExpected=InExp;
100  if (InAct AND inExp) then do;
101  DoCompare=1;
102  CompareFailed=2;
103  if (BaseObs ne CmpObs) then do;
104  if ("&i_CompareCheck." = "STRICT" OR "&i_CompareCheck." = "MORECOLUMNS") then do;
105  DoCompare=0;
106  end;
107  end;
108  if (BaseNVar ne CmpNVar) then do;
109  if ("&i_CompareCheck." = "STRICT" OR "&i_CompareCheck." = "MOREOBS") then do;
110  DoCompare=0;
111  end;
112  end;
113  end;
114  else if (InAct AND not inExp AND "&i_LibraryCheck." ne "STRICT") then do;
115  DoCompare=0;
116  CompareFailed=0;
117  end;
118  else do;
119  DoCompare=0;
120  CompareFailed=2;
121  end;
122  run;
123 
124  %*** determine number of compared tables ***;
125  proc sql noprint;
126  select sum (DoCompare) into :AnzCompares
127  from work._ergebnis;
128  quit;
129 
130  %if (&AnzCompares. > 0) %then %do;
131  %do i=1 %to &AnzCompares.;
132  %local Memname&i.;
133  %end;
134 
135  proc sql noprint;
136  select memname into :Memname1-:Memname%trim(&AnzCompares.)
137  from work._ergebnis
138  where DoCompare=1;
139  quit;
140 
141  %*** upcase for id columns ***;
142  %if (&i_id. ne _NONE_) %then %do;
143  %let i_id=%upcase (&i_id.);
144  %end;
145 
146  %*** Check for open ODS DESTINATIONS ***;
147  %local OpenODSDestinations;
148  %let OpenODSDestinations=0;
149 
150  %*** SASHELP.VDEST is only available in 9.2 or later ***;
151  %if (&sysver. NE 9.1) %then %do;
152  proc sql noprint;
153  select count (*) into :OpenODSDestinations from sashelp.vdest;
154  quit;
155  %end;
156 
157  %if (&OpenODSDestinations. = 0) %then %do;
158  ods listing;
159  %end;
160 
161  %*** Compare each pair of tables ***;
162  %do i=1 %to &AnzCompares.;
163  %if (&i_id. ne _NONE_) %then %do;
164  %let l_col_names=;
165  %let l_id=;
166  proc sql noprint;
167  select distinct upcase (name) into :l_col_names separated by ' '
168  from dictionary.columns
169  where libname = "%upcase (&i_actual.)" AND upcase (memname) = "%upcase(&&memname&i.)";
170  quit;
171 
172  %let counter=1;
173  %let l_id_col = %scan (&i_id.,&counter., %str ( ));
174  %do %while (&l_id_col. ne );
175  %let l_found=%sysfunc (indexw (&l_col_names, &l_id_col.));
176  %if (&l_found. > 0) %then %do;
177  %let l_id = &l_id. &l_id_col;
178  %end;
179  %let counter=%eval (&counter.+1);
180  %let l_id_col = %scan (&i_id.,&counter., %str ( ));
181  %end;
182  %end;
183 
184  proc compare
185  base=&i_expected..&&memname&i.
186  compare=&i_actual..&&memname&i.
187  %if (&i_fuzz. ne _NONE_) %then %do;
188  CRITERION=&i_fuzz.
189  %end;
190  noprint;
191  %if (&i_id. ne _NONE_) %then %do;
192  id &l_id.;
193  %end;
194  run;
195  %let _sysinfo=&sysinfo.;
196  proc sql noprint;
197  update work._ergebnis
198  set l_rc=&_sysinfo.
199  where upcase (memname)="%upcase (&&memname&i.)";
200  quit;
201  %end;
202  %end;
203 
204  %if (&OpenODSDestinations. = 0) %then %do;
205  ods listing close;
206  %end;
207 
208  %*** set test result ***;
209  data work._ergebnis;
210  set work._ergebnis;
211  select (l_rc);
212  when (0)
213  Comparefailed=0;
214  when (128) do; /* COMPOBS - Comparison data set has observation not in base */
215  if ("&i_CompareCheck." = "MOREOBS" OR "&i_CompareCheck." = "MORECOLSNOBS") then do;
216  Comparefailed=0;
217  end;
218  end;
219  when (2048) do; /* COMPVAR - Comparison data set has variable not in base */
220  if ("&i_CompareCheck." = "MORECOLUMNS" OR "&i_CompareCheck." = "MORECOLSNOBS") then do;
221  Comparefailed=0;
222  end;
223  end;
224  when (2176) do; /* COMPOBS & COMPVAR */
225  if ("&i_CompareCheck." = "MORECOLSNOBS") then do;
226  Comparefailed=0;
227  end;
228  end;
229  otherwise;
230  end;
231  run;
232 
233  %*** collect results for report ***;
234  data work._ergebnis;
235  length i_LibraryCheck i_CompareCheck $15 i_id i_ExcludeList $80;
236  set work._ergebnis;
237  i_LibraryCheck="&i_LibraryCheck.";
238  i_CompareCheck="&i_CompareCheck.";
239  i_id="&i_id.";
240  i_ExcludeList="&i_ExcludeList.";
241  run;
242 
243  %*** determine return value for test ***;
244  proc sql noprint;
245  select max (CompareFailed) into :&o_result.
246  from work._ergebnis;
247  quit;
248 
249  %_getScenarioTestId (i_scnid=&g_scnid, r_casid=l_casid, r_tstid=l_tstid);
250 
251  %*** create subfolder ***;
252  %_createTestSubfolder (i_assertType =assertlibrary
253  ,i_scnid =&g_scnid.
254  ,i_casid =&l_casid.
255  ,i_tstid =&l_tstid.
256  ,r_path =l_path
257  );
258 
259  %*** create library listing ***;
260  *** Capture tables instead of ODS DOCUMENT ***;
261  libname _alLib "&l_path.";
262  ods document name=_alLib._Library_act(WRITE);
263  proc print data=WORK._assertLibraryActual label noobs;
264  run;
265  ods document close;
266  ods document name=_alLib._Library_exp(WRITE);
267  proc print data=WORK._assertLibraryExpected label noobs;
268  run;
269  ods document close;
270  data _alLib._Library_rep;
271  set work._ergebnis;
272  run;
273  libname _alLib clear;
274 
275  proc datasets lib=work nolist memtype=(data view);
276  delete _ergebnis _assertLibraryActual _assertLibraryExpected;
277  quit;
278 %mend _assertlibrary;