Previous Page | Next Page

Function Reference

Logging Example Using Functions

The example program determines the number of years, months, and days between two SAS date values. It uses logging facility functions to write progress messages to an external file. The program is structured so that the appender and the logger are created, and variables are initialized during the first iteration of the DATA step in order to ensure efficiency of the program.

data a;
   input @1 dob mmddyy10.;
   format dob tod mmddyy10.;

   /* In the first iteration of the DATA step, create an appender          */
   /* and a logger, and initialize variables tod and bdays. Then, determine */
   /* the number of days in the month prior to the current month.          */

   if _n_ = 1 then 
      do;
       rc=log4sas_appender("functionAppender", "FileRefAppender", "fileref=myfiles");
       if rc ne 0 then do;
           msg = sysmsg();
           put msg;
           ABORT;
           end;

       rc=log4sas_logger("functionLogger", "appender-ref=(functionAppender) 
                                        level=info");
       if rc ne 0 then do;
          msg = sysmsg();
          put msg;
          ABORT;
          end;

       /* Get the current date from the operating system */
      tod=today();
      retain tod;

      rc=log4sas_logevent("functionLogger", "info", "Obtained today's date.");
      if rc ne 0 then do;
         msg = sysmsg();
         put msg;
         ABORT;
        end;

      /* Determine the number of days in the month prior to current month */
      bdays=day(intnx('month',tod,0)-1);
      retain bdays;

      rc=log4sas_logevent("functionLogger", "info",
                                          "Determined the number of business days.");
      if rc ne 0 then do;
         msg = sysmsg();
         put msg;
         ABORT;
         end;

   end; /* end the processing for first iteration */

 
   /* Find the difference in days, months, and years between */
   /* start and end dates                                */              
   dd=day(tod)-day(dob);
   mm=month(tod)-month(dob);
   yy=year(tod)-year(dob);

   rc=log4sas_logevent("functionLogger", "info", "Found date differences.");
   if rc ne 0 then do;
      msg = sysmsg();
      put msg;
      ABORT;
      end;
 
   /* If the difference in days is a negative value, add the number */   
   /* of days in the previous month and reduce the number of months */
   /* by 1.                                                         */
   if dd < 0 then do;
      dd=bdays+dd;
      mm=mm-1;

      rc=log4sas_logevent("functionLogger", "info", "Made adjustments in days.");
      if rc ne 0 then do;
         msg = sysmsg();
         put msg;
         ABORT;
         end;
   end;
   
   /* If the difference in months is a negative number add 12 */   
   /* to the month count and reduce the year count by 1.          */
   if mm < 0 then do;
      mm=mm+12;
      yy=yy-1;
    
      rc=log4sas_logevent("functionLogger", "info", "Made adjustments in months.");
      if rc ne 0 then do;
         msg = sysmsg();
         put msg;
         ABORT;
         end;
   end;
  
datalines;
01/01/1986
02/28/1990
12/03/2006
02/28/2000
02/29/2000
03/01/2000
05/10/1974
05/11/1974
05/12/1974
;

proc print label;
   label dob='Date of Birth'
            tod="Today's Date" 
            dd='Difference in Days'
            mm= 'Difference in Months'
            yy='Difference in Years';
   var dob tod yy mm dd;
run;

The file that is represented by the MYFILES fileref contains the following logging facility messages:

Obtained today's date.
Determined the number of business days.
Found date differences.
Found date differences.
Made adjustments in days.
Found date differences.
Made adjustments in months.
Found date differences.
Made adjustments in days.
Found date differences.
Made adjustments in days.
Found date differences.
Found date differences.
Made adjustments in months.
Found date differences.
Made adjustments in months.
Found date differences.
Made adjustments in months.

Here is the program output:

Program Output for Determining Date Differences

[Program Output for Determining Date Differences]

Previous Page | Next Page | Top of Page