Now that we've spent some time identifying the first and last observations among a group of observations, now we'll turn our attention to identifying the last observation *in a data set*. You might find yourself in situations in which you need to determine when the last observation in an input data set has been read, so that you can then tell SAS to take some action.

In this section, we'll investigate how to use the SET statement's END= option to tell SAS to create a temporary numeric value whose value is used to detect the last observation. We'll start first by taking a look at a program in which we would benefit from knowing the last observation.

##
Example 13.15
Section* *

The following program uses an accumulator variable called `TotalSales` to determine the overall total `Sales` amount in the *sales* data set:

```
LIBNAME stat481 'C:\Simon\Stat481WC\sp09\01sasdata\sasndata';
DATA storesales;
set stat481.sales;
TotalSales + Sales;
format Sales TotalSales Dollar13.2;
RUN;
PROC PRINT data = storesales NOOBS;
title;
RUN;
```

If you haven't already done so, download the sales data set, and save it to a convenient location on your computer. Launch the SAS program and edit the LIBNAME statement to reflect the location in which you saved the data set. Then, run * * the program, and review the output to convince yourself that the value of the `TotalSales` variable for each observation is the sum of the `Sales` values for the observation and all of the observations that precede it.

In this case, we ended up with a data set called `storesales` whose `TotalSales` variable contains a running total of the `Sales` values. Now, suppose instead we wanted a data set called `storesales` which contains only the last observation with the overall total of all `Sales`. The SET statement's END= option can help us select the last observation.

##
Example 13.16
Section* *

The following program uses the SET statement's END= option and a subsetting IF statement to tell SAS to write only the last observation in the input data set (*stat481.sales*) to the output data set (`storesales`):

```
LIBNAME stat481 'C:\Simon\Stat481WC\sp09\01sasdata\sasndata';
DATA storesales;
set stat481.sales end=last;
TotalSales + Sales;
format Sales TotalSales Dollar13.2;
drop Store Dept Sales Quarter;
if last;
RUN;
PROC PRINT data = storesales NOOBS;
title;
RUN;
```

The END=`last` option tells SAS to create a temporary numeric variable called `last`, which is initialized to 0 and set to 1 only when the SET statement reads the last observation in the input data set. Although we used the variable name `last` here, we could have used any valid SAS variable name. The variable is temporary in that it is placed in the program data vector but not written to the output data set.

The subsetting IF statement:

`if last;`

is the statement that tells SAS to include in the output data set only the last observation in the input data set. The DROP statement tells SAS to drop the then meaningless `Store`, `Dept`, `Quarter`, and `Sales` variables from the output data set.

If you haven't already done so, download the sales data set, and save it to a convenient location on your computer. Launch the SAS program and edit the LIBNAME statement to reflect the location in which you saved the data set. Then, run * * the program, and review the output to convince yourself that the `storesales` data set contains just one observation and one variable containing the overall total sales (`TotalSales`) of all of the stores.