Search code examples
sas

Last.variable not returning expected results


I'm trying to create a summary table from a larger table, using just the last row of data. I have the following code:

proc sort data=&data.;
   by date;
run;

data &data._summary;
   set &data.;
   by date;
   if last.date;
run;

My understanding is that this should just output one row (for the last value of date), but instead it's keeping all values. What am I doing wrong here?


Solution

  • From the documentation

    FIRST. and LAST. DATA Step Variables
    ...
    In the DATA step, SAS identifies the beginning and end of each BY group by creating the following two temporary variables for each BY variable:
    • FIRST.variable
    • LAST.variable

    If you want the last observation in the DATA set you can use the END= option to set a flag for the last row.

    data want (label='Last row of have') ;
      set have end=lastrow ;
      if lastrow ;
    run ;