Macro Assertions for

From sasCommunity
Jump to: navigation, search

Note: this page acts as index entries:

  • Assertions for Macros
  • Macros, Assertions for

Author: Ronald_J._Fehd

This page contains common code used in macros which have a parameter data=.

This code tests these assertions:

  • if not exist(&Data) then exit
  • if exist(&Data)
    • get N-Rows, N-Columns
    • if not (N-Rows and N-Columns) then exit

Programs

This extract is common code for both macros:

Code in Macro

%MACRO callmacr
       (data=
       ,testing=0
       )
/ des = 'description: purpose:'
 /**** ** store source /* */
;/*RJF2 2012-May-01 */
%local _C_Col _C_Dsid _C_Nobs _C_Nvars   _C_Rc ;
 
%**  description: assertions;
%**  purpose    : if fail then exit;
%if  not(%sysfunc(exist(&Data.))) %then %do;
     %put Err%str()or: &SysMacroname. exiting: not exist(&Data.);
     %return;
     %end;
%let _C_Dsid  = %sysfunc(open (&Data.         ));
%let _C_Nobs  = %sysfunc(attrn(&_C_Dsid.,Nobs ));
%let _C_Nvars = %sysfunc(attrn(&_C_Dsid.,Nvars));
%if  not &_C_Nobs. or not &_C_Nvars. %then %do;
  %put Err%str()or: &SysMacroName. &Data. obs=&_C_Nobs. vars=&_C_Nvars.;
     %goto CloseExit;
     %end;
%else 
 %put note: &SysMacroname. reading &Data. obs=&_C_Nobs. vars=&_C_Nvars.;     
 
%*... Big Snip Here ...;
 
%CloseExit: %let _C_Rc = %sysfunc(close(&_C_Dsid.));
%mend calltext;

Unit Test

 /*    name: <UNC>\SAS-site\macros-tests\callmacr-test-errors.sas
description: unit tests
             not exist(data)
             not Nobs(data)
             not Nvars(data)
    purpose: test suite for callmacr and calltext
*******/
options mprint source2;*testing;
 
*callmacr-error-not-exist-data;
%CallMacr(Data = sashelp.classes)
 
data Work.No_Obs;
if 0 then set sashelp.class;
stop;         
run;
%CallMacr(Data = Work.No_Obs)
 
data Work.No_Vars;
stop;         
run;
%CallMacr(Data = Work.No_Vars)

Unit Test log

10         *callmacr-error-not-exist-data;
11         %CallMacr(Data = sashelp.classes)
Error: CALLMACR exiting: not exist(sashelp.classes)

12         
13         data Work.No_Obs;
14         if 0 then set sashelp.class;
15         stop;
16         run;

NOTE: The data set WORK.NO_OBS has 0 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds

17         %CallMacr(Data = Work.No_Obs)
Error: CALLMACR Work.No_Obs obs=0 vars=5
note: CALLMACR used real time  0:00:00.000
18         
19         data Work.No_Vars;
20         stop;
21         run;

NOTE: The data set WORK.NO_VARS has 0 observations and 0 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds

22         %CallMacr(Data = Work.No_Vars)
Error: CALLMACR Work.No_Vars obs=0 vars=0
note: CALLMACR used real time  0:00:00.000

References

-- created by User:Rjf2 12 May 2012 (EDT)

--Ronald_J._Fehd macro.maven == the radical programmer