Return to Archive

SAS Tip of the Month
March 2004

When a SAS dataset is sorted and is later being processed, SAS creates a FIRST and LAST flag for each BY variable with a value of 1 if true and 0 if false. SAS sets the FIRST.variable when it is processing the first observation in a BY group, and sets the LAST.variable when it is processing the last observation in a BY group. This allows for different actions, based on whether processing is starting for a new BY group or ending for a BY group. The following example will output all records for a customer (CUSTNUM) from data master dataset (MNTHTRNS) to a dataset (RPTTRANS) if they have two or more transactions in the one day (TRANDATE):

   DATA rpttrans;
      SET mnthtrns;
      BY custnum trandate;
      IF ^(FIRST.trandate=LAST.trandate=1) THEN OUTPUT rpttrans;
Updated March 9, 2004