3.1 C
London
Friday, December 20, 2024
HomeSASData Munging in SASHow to Use FIRST. and LAST. Variables in SAS

How to Use FIRST. and LAST. Variables in SAS

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories