As the first step in the decommissioning of the site has been converted to read-only mode.

Here are some tips for How to share your SAS knowledge with your professional network.

Macro Assertions for

From sasCommunity
Revision as of 16:52, 27 October 2014 by Ron.Fehd.macro.maven (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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


This extract is common code for both macros:

Code in Macro

%MACRO callmacr
/ 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.);
%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;
 %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\
description: unit tests
             not exist(data)
             not Nobs(data)
             not Nvars(data)
    purpose: test suite for callmacr and calltext
options mprint source2;*testing;
%CallMacr(Data = sashelp.classes)
data Work.No_Obs;
if 0 then set sashelp.class;
%CallMacr(Data = Work.No_Obs)
data Work.No_Vars;
%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)

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
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


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

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