• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / SAS / Get Last Observation of SAS Dataset with end=

Get Last Observation of SAS Dataset with end=

September 15, 2022 Leave a Comment

To get the last observation of a dataset, you can use the end= data set option. end= creates a temporary numeric value whose value is used to detect the last observation.

data last_obs_only;
    set all_data end=last_obs;
    if last_obs;
run;

When working with datasets in any programming language, the ability to get snapshots of your data can be valuable for checking certain conditions.

One such case is if you want to get the last observation of a dataset in SAS.

To select the last observation of a dataset, you can use the end= data set option.

end= allows us to give a name to the last observation of a dataset. In the data step, we can check if we are on the last observation with an if statement and then output if we are there.

Below shows you a simple example of how you can select the last observation of a dataset in SAS.

data last_obs_only;
    set all_data end=last_obs;
    if last_obs;
run;

Get First Observation of Dataset in SAS

If you instead want to select the first observation of a dataset in SAS, use the SAS automatic variable _n_.

The SAS automatic variable _n_ represents the number of times the data step has iterated.

When a data step starts, _n_ is initialized to 1. Then, after each iteration through the data step, _n_ is incremented by one.

Therefore, we can use _n_ and check if _n_ is equal to 1. Then we can output those rows to a new dataset.

data first_obs;
    set all_data;
    if _n_ == 1;
run;

Hopefully this article has been useful for you to learn how to get the last observation of a SAS dataset.

Other Articles You'll Also Like:

  • 1.  SAS round – Rounding Numbers in a SAS Data Step
  • 2.  SAS find() Function – Check if Substring is in Character Variable
  • 3.  SAS Lowercase – Make String Lowercase with SAS lowcase function
  • 4.  Get Substring from Right in SAS
  • 5.  catx SAS – Concatenate String Variables with Delimiter in SAS Data Step
  • 6.  SAS Remove Formats from Dataset with PROC DATASETS
  • 7.  SAS mean() Function – Find Average Across Columns in Data Step
  • 8.  SAS max() Function – Find Maximum Value Across Columns in Data Step
  • 9.  SAS _n_ – How to Use the Automatic Variable _n_ in a Data Step
  • 10.  SAS Percent Format – Formatting Number as Percent in SAS Dataset

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy