How To Select First and Last Rows (Observations) In SAS

Selection of the first and last observations from the dataset could be a little tricky. You can use the first. and last. variable but it only works with the grouping of the data. It doesn’t work on the entire dataset.

But the following options are available in SAS that helps you identify and extract last and first observations from a data set.

_N_ Automatic Variable To Select The First Row

The value for _N_ is initially set to 1. Each time the DATA step loops past the DATA statement, the variable _N_ increments by 1. The value of _N_ represents the number of times the DATA step has iterated.

END=last_obs Option To Select The Last Row

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.

Summary:

  • END=last_obs Option: Last observation can be selected using end= option
  • _N_ Automatic Variable: First observation can be selected using _N_

The following sample dataset will be used to demonstrate how to select first and last rows (observations) from the SAS dataset.

				/* create a dataset */
data my_dataset;
	input ID 4. Name $ Dept $;
	datalines;
1011 Jan Sales
1016 Gill Sales
1020 Tomy Finance
1025 Harry Admin
1030 Mary Finance
1031 Peter Admin
1038 Kim Finance
1041 Jay Admin
1041 Magnus Sales
;
run;

/* view dataset */
proc print data=my_dataset;
run;
			
Sample dataset to show how to select first and last observations in SAS

Example 1: Select The First Observation In SAS

You can use _N_ automatic variable to identify and extract the first observation from the sas dataset. You get the first row when you filter the data based on condition _N_=1 in the data step query.

The following code shows how to select the first observation (row) from the sas dataset.

				/* select first observation in SAS */
data select_first_obs;
	set my_dataset;
	if _N_=1 then output;
run;

/* view dataset */
proc print data=select_first_obs;
run;
			
Select first observation in SAS

Example 2: Select The Last Observation In SAS

You can use end=last_obs temporary numeric variable to identify and extract the last observation from the sas dataset. The last_obs temp numeric variable gets flagged with value 1 for the last observation and 0 for the rest of the observations.

Hence, you get the last row when you filter the data based on condition last_obs=1 in the data step query.

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

/* view dataset */
proc print data=select_last_obs;
run;
			
Select the last observation in SAS

Example 3: Select The First and Last Observations In SAS

You can use _N_ automatic variable and end=last_obs temporary numeric variable to identify and extract the first and last observations from the sas dataset.

The first observation can be extracted using the condition _N_=1 whereas the last observation can be extracted using condition last_obs=1.


NOTE: The values for _N_ will be generated sequentially whereas last_obs has numeric flag values either 1 or 0. Value 1 is for the last observation and 0 for the rest of the observations.

The following code demonstrates how to extract first and last observations from the sas dataset.

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

/* view dataset */
proc print data=select_first_last_obs;
run;
			
Select first and last rows in SAS

FAQ

How to extract the first and last observations in SAS?

You can automatic variable _N_ , firstobs=option, and end=last_obs options to identify and extract first and last observations in SAS.

How does SAS identify the first and last observations in a group?

SAS uses the values of the FIRST.variable and LAST.variable temporary variables to identify the first and last observations in a group, and therefore the group itself.