First. and Last. Variables
FIRST.VARIABLE assigns the value of 1 for the first observation in a BY group and the value of 0 for all other observations in the BY group.
LAST.VARIABLE assigns the value of 1 for the last observation in a BY group and the value of 0 for all other observations in the BY group.
Create Sample Dataset
Suppose you have a dataset consisting 3 variables and 12 observations. The variables are ID, Name and Score. The variable ID is a grouping variable and it contains duplicates.
Example:
data readin;
input ID Name $ Score;
cards;
1 David 45
1 David 74
2 Sam 45
2 Ram 54
3 Bane 87
3 Mary 92
3 Bane 87
4 Dane 23
5 Jenny 87
5 Ken 87
6 Simran 63
8 Priya 72
;
run;
Use PROC SORT to sort the data set by ID. It is required to sort the data before using first. and last. variables.
PROC SORT DATA = READIN;
BY ID;
RUN;
DATA READIN1;
SET READIN;
BY ID;
First_ID= First.ID;
Last_ID= Last.ID;
RUN;
Note : FIRST. and 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.
How it works
- FIRST.variable = 1 when an observation is the first observation in each group values of variable ID.
- FIRST.variable = 0 when an observation is not the first observation in each group values of variable ID.
- LAST.variable = 1 when an observation is the last observation in each group values of variable ID.
- LAST.variable = 0 when an observation is not the last observation in each group values of variable ID.
PROC SORT DATA = READIN;
BY ID;
RUN;
DATA READIN1;
SET READIN;
BY ID;
IF FIRST.ID;
PROC PRINT;
RUN;
Note : It returns first observation among values of a group (total 7 observations).