How To Use END=last Option To Select Last Observation In SAS

The END= option used in the data step is very powerful to identify the last observation from the SAS dataset. The end= option to tell SAS to create a temporary numeric value whose value is used to detect the last observation.

The following sample dataset will be used to show how to use end=last option in SAS to select last observation from a sas dataset.

				/* create a dataset */
data result;
	input student $ marks major $;
	datalines;
2000 87 Math
2010 92 English
2020 92 History
2030 94 Music
2040 96 Robotics
2050 98 AI/ML
2060 91 Physics
2070 90 Chemistry
2080 86 Aeronautics
2090 95 Geology
;
run;

/* view dataset */
proc print data=result;
run;
			
Sample SAS dataset to select last observation

Example 1: How To Assign END=last Temp Variable Value to a New Variable In SAS

Here is the example explaining how this temp numeric variable is being created. Let’s create a new variable and assign the temp variable value to it.

The end=last temp numeric variable is physically not available on the dataset but it can be made available by assigning values to a new variable.

The following code shows how to add a new variable that holds values of this temp numeric variable.

The last_obs has value 0 for all the rows except the last one. The last row would be highlighted with value 1 on last_obs.

				/* assign end=option values to new variable */
data result_New;
	set result end=last_obs;
	new_var = last_obs;
run;

/* view dataset */
proc print data=result_New;
run;
			
The end=last_obs option in SAS

Example 2: Select Last Observation In SAS Using END=Option

With the end=last_obs option you can easily identify and extract last observation as it flags value 1 on last_obs temp numeric variable for the last observation in the dataset.

The following code shows how to select the last observation in SAS using end=option.

				/* select last observation in SAS */
data select_last_obs;
	set result end=last_obs;
	if last_obs=1 then output;
run;

/* view dataset */
proc print data=select_last_obs;
run;
			
The end=last_obs option to select last observation in SAS

Example 3: Select First and Last Observations In SAS Using END=Option

You can extract first and last observations from a SAS dataset using _N_ automatic variable and END=last_obs option.

  • First observation can be selected using _N_
  • Last observation can be selected using end= option

The following code shows how to select first and last observations in SAS.

				/* select first and last observations in SAS */
data select_first_last_obs;
	set result end=last_obs;
	if _N_=1 OR last_obs=1 then output;
run;

/* view dataset */
proc print data=select_first_last_obs;
run;
			
SAS First and Last Observation using end=last_obs option

FAQ – How To Use END=last_obs Option In SAS

What is the END=last option in SAS?

The END=last option is a way to create a temporary variable that indicates whether the current observation is the last one in the input data set. The variable is assigned a value of 1 if it is the last observation, and 0 otherwise.

What are some applications of selecting the last observation in SAS?

Some applications of selecting the last observation in SAS are:

  • Performing a calculation based on the last observation, such as computing a cumulative sum or a running total.
  • Extracting the last observation of a group or a category, such as the most recent sales or the highest score.