You can use the FIRST. and LAST. functions in SAS to identify the first and last observations by group in a SAS dataset.
Here is what each function does in a nutshell:
- FIRST.variable_name assigns a value of 1 to the first observation in a group and a value of 0 to every other observation in the group.
- LAST.variable_name assigns a value of 1 to the last observation in a group and a value of 0 to every other observation in the group.
The following examples show how to use each function in practice with the following dataset in SAS:
/*create dataset*/
data my_data;
input team $ points rebounds;
datalines;
Mavs 29 10
Mavs 13 6
Mavs 22 5
Mavs 20 9
Spurs 13 9
Spurs 15 10
Spurs 33 8
Spurs 27 11
Rockets 25 8
Rockets 14 4
Rockets 16 7
Rockets 12 4
;
run;
/*view dataset*/
proc print data=my_data;
Example 1: How to Use FIRST. in SAS
We can use the following FIRST. function in SAS to assign a value of 1 to the first observation for each team in the dataset:
/*sort dataset by team*/
proc sort data=my_data;
by team;
run;
/*create new dataset that labels first row for each team*/
data first_team;
set my_data;
by team;
first_team=first.team;
run;
/*view dataset*/
proc print data=first_team;
Notice that the first_team column assigns the first observation for each team a value of 1. All other values are assigned a value of 0.
You can also use the following code to create a new dataset that only contains the first observation for each team:
/*sort dataset by team*/
proc sort data=my_data;
by team;
run;
/*create new dataset only contains first row for each team*/
data first_team;
set my_data;
by team;
if first.team;
run;
/*view dataset*/
proc print data=first_team;
Notice that the dataset only contains the first observation for each team.
Example 2: How to Use LAST. in SAS
We can use the following LAST. function in SAS to assign a value of 1 to the first observation for each team in the dataset:
/*sort dataset by team*/
proc sort data=my_data;
by team;
run;
/*create new dataset that labels last row for each team*/
data last_team;
set my_data;
by team;
last_team=last.team;
run;
/*view dataset*/
proc print data=last_team;
Notice that the last_team column assigns the last observation for each team a value of 1. All other values are assigned a value of 0.
You can also use the following code to create a new dataset that only contains the last observation for each team:
/*sort dataset by team*/
proc sort data=my_data;
by team;
run;
/*create new dataset only contains last row for each team*/
data last_team;
set my_data;
by team;
if last.team;
run;
/*view dataset*/
proc print data=last_team;
Notice that the dataset only contains the last observation for each team.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Reorder Variables in SAS
How to Label Variables in SAS
How to Rename Variables in SAS