Example of Using Autocall Macros to Log Messages

The macro program first retrieves the number of variables in a SAS data set and then appends each variable name to a macro variable. Logging facility debug log events send progress messages to a file that is referenced by the REV1 fileref. You can see from the SAS log output that the messages are written in the SAS log as well. By writing debug messages to a separate file, the logging facility acts as a filter where only the messages that you want to see are written to the file. The shaded code lines that follow are the statements that create logging messages.
filename rev1 ("c:\mySAS\Logs\rev1.log");
%log4sas();
%log4sas_appender(dsvar2mvar, "FileRefAppender", 'fileref=rev1');
%log4sas_logger(macroVar, 'level=debug appender-ref=(dsvar2mvar)');

/* Create sample data */

data one;
   input x y z;
datalines;
1 2 3
;

%macro lst(dsn);
   %global x;
   %let x=;
   /* Open the data set */
   %let dsid=%sysfunc(open(&dsn));

   /* Assign the number of variables into the macro variable CNT */
   %let cnt=%sysfunc(attrn(&dsid,nvars));
   %put cnt=&cnt;
   %log4sas_debug(macroVar, 'The number of variables is set in CNT');

   /* Create a macro variable that contains all data set variables */
   %do i = 1 %to &cnt;
      %let x=&x%sysfunc(varname(&dsid,&i));
     %log4sas_debug(macroVar, 'data set variable appended to macro variable');
   %end;

   /* Close the data set */
   %let rc=%sysfunc(close(&dsid));
%mend lst;
%log4sas_debug(macroVar, 'lst Macro complete');

/* Call the macro and pass the name of the data set to be processed */
%log4sas_debug(macroVar, 'calling lst(one)');
%lst(one)
%put macro variable x = &x

%log4sas_debug(macroVar, 'macro lst(one) complete');
The file that is referenced by fileref REV1 contains these lines of text:
Contents of the File Referenced by the REV1 Fileref
lst Macro complete
calling lst(one)
The number of variables is set in CNT
data set variable appended to macro variable
data set variable appended to macro variable
data set variable appended to macro variable
macro lst(one) complete
The following messages were written to the SAS log:
1    %log4sas();
2    %log4sas_appender(dsvar2mvar, "FileRefAppender", 'fileref=rev1');
3    %log4sas_logger(macroVar, 'level=debug appender-ref=dsvar2mvar');
4
5    /* Create sample data */
6
7    data one;
8        input x y;
9    datalines;

NOTE: The data set WORK.ONE has 1 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.03 seconds


11   ;
12
13   %macro lst(dsn);
14       %global x;
15       %let x=;
16     /* Open the data set */
17     %let dsid=%sysfunc(open(&dsn));
18
19     /* Assign the number of variables into the macro variable CNT */
20     %let cnt=%sysfunc(attrn(&dsid,nvars));
21     %put cnt=&cnt;
22     %log4sas_debug(macroVar, 'The number of variables is set in CNT');
23
24     /* Create a macro variable that contains all data set variables */
25     %do i = 1 %to &cnt;
26       %let x=&x%sysfunc(varname(&dsid,&i));
27       %log4sas_debug(macroVar, 'data set variable appended to macro
variable');
28      %end;
29
30     /* Close the data set */
31     %let rc=%sysfunc(close(&dsid));
32   %mend lst;
33   %log4sas_debug(macroVar, 'lst Macro complete');
lst Macro complete
34
35   /* Call the macro and pass the name of the data set to be processed */
36   %log4sas_debug(macroVar, 'calling lst(one)');
calling lst(one)
37   %lst(one)
cnt= 3
The number of variables is set in CNT
data set variable appended to macro variable
data set variable appended to macro variable
data set variable appended to macro variable
38   %put macro variable x = &x
macro variable x = xyz
39
40   %log4sas_debug(macroVar, 'macro lst(one) complete');
macro lst(one) complete