FIRST. and LAST. Variables in SAS – How to Select the Variables

Previously, we have seen the SAS variable, today we will be looking at the use of FIRST. and LAST. variable in SAS Programming Language. We will discuss the working of FIRST. and LAST. Variables in SAS and how to select SAS FIRST. Variable. At last, we will study how to select LAST. variables in SAS and calculate the cumulative score by the group.

So, let’s start with the FIRST. and LAST. Variables in SAS.

FIRST. and LAST. Variables in SAS - Data Step Processing in By Groups

What is FIRST. & LAST. Variables in SAS?

Anytime we wish to group our data in SAS Programming, we make use of the BY statement to tell the order of grouping and the SET statement to group data. Whenever both BY and SET statements are used together, SAS automatically creates two variables, FIRST. and LAST. Variables in SAS, that are temporary.

SAS uses the value of the FIRST. and LAST. Variables to identify the first and last observations in a group. SAS places FIRST. and LAST. Variable in SAS Program Data Vector (PDV). Then, they are available for DATA step processing but SAS does not add them to the output data set as they are temporary in nature.

Explore the concept – SAS Macro For Beginners

How FIRST. and LAST. Variables Works

When an observation is the first in a BY group, SAS sets the value of FIRST.variable to 1 for the variable whose value changed, as well as for all of the variables that follow in the BY statement. For all other observations in the BY group, the value of FIRST.variable is 0.

Likewise, if the observation is the last in a BY group, SAS sets the value of LAST.variable to 1 for the variable whose value changes on the next observation, as well as for all the variables that follow in the BY statement. For all other observations in the BY group, the value of LAST.variable is 0. For the last observation in a data set, the value of all LAST.variable variables are set to 1.

The values of both FIRST. and LAST. variables in SAS are either 1 or 0.

  • FIRST.variable = 1, when an observation is the first observation in a BY group.
  • FIRST.variable = 0, when an observation is not the first observation in a BY group.
  • LAST.variable = 1, when an observation is the last observation in a BY group.
  • LAST.variable = 0, when an observation is not the last observation in a BY group.

Example of FIRST. and LAST. Variables in SAS –

data class1;
input ID Name $ Marks;
cards;
1     Rahul     45
1     Ajay      74
2     Ram       45
2     Girish    54
3     Simran    87
3     Priya     92
3     Riya      87
4     Tina      23
5     Dave      87
5     Ken       87
6     Albert    63
8     Alex      72
;
run;
PROC SORT DATA = class1;
BY ID;
RUN;
DATA class2;
SET class1;
BY ID;
First_ID= First.ID;
Last_ID= Last.ID;
RUN;

Example of FIRST. and LAST. Variables in SAS

We used PROC SORT to sort the data, set by ID. It is required to sort the data before using first. and last. variables.

FIRST./LAST. variables are temporary variables. That means they are not visible in the newly created data set. To make them visible, we need to create two new variables. In the program above, i have created First_ID and Last_ID variables.

Have a look at the SAS Proc Sort Procedure 

Selecting FIRST. Variable in SAS

Suppose you need to select only the first observation among a group of observations. It is very easy to do it with IF statement. The IF statement subsets data when IF is not used in conjunction with THEN or ELSE statements.

Example of FIRST. Variable in SAS-

Join DataFlair on Telegram
PROC SORT DATA = class1;
BY ID;
RUN;
DATA class2;
SET READIN;
BY ID;
IF FIRST.ID;
PROC PRINT;
RUN;

Example of First. Variable in SAS

It returns first observation among values of a group (total 7 observations).

Selecting Last. Variable in SAS

Suppose you are asked to include only last observation from a group. Like the previous example, we can use last. variable to subset data.
Example of Last. Variable in SAS-

PROC SORT DATA = class1;
BY ID;
RUN;
DATA class2;
SET READIN;
BY ID;
IF LAST.ID;
PROC PRINT;
RUN;

Selecting Last. Variable in SAS

Have you checked? – PROC SQL SAS Guide

How to calculate Cumulative Score in BY Group

Suppose you need to calculate cumulative score by variable ID.

Example of Cumulative Score-

Data class3;
set class1;
by ID;
if first.id then CumScore = Marks;
else CumScore + Marks;
proc print;
run;

In the above program, we are setting Cumscore = Marks when it is the first value of a group i.e. ID. On the other hand, adding Marks to Cumscore:

The Cumscore+Maarks implies CumScore = CumScore + Marks in BY group processing.

Calculate Cumulative Score by Group

This was all about FIRST. and LAST. Variables in SAS.

Summary

We hope you enjoyed this SAS tutorial. Now, you will now be able to efficiently use FIRST. And LAST. variables in SAS.

Any queries or feedback? Feel free to enter in the comment section.

Start a career in SAS technology by acquiring SAS certifications.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.