top of page
  • Writer's pictureEkta Aggarwal

FIRSTOBS and OBS in SAS

In this tutorial we shall learn about:

in SAS with examples.


FIRSTOBS: Starting point (row number) to read the lines of an already existing dataset.

OBS: Ending point (row number) to read the lines of an already existing dataset.


Firstly let us define our library location:

libname mylib '/home/u50132927/My_datasets';

For this tutorial we shall be making use of SAS' inbuilt dataset: SASHELP.SHOES. Let us firstly understand what DATA and SET statement are!

DATA: Location of new dataset

SET: Dataset which needs to be copied.


In the following query we are copying all the rows from SASHELP.SHOES to our new dataset FIRSTOBS_OBS. Note our data has 395 rows and 7 columns!

DATA MYLIB.FIRSTOBS_OBS;
SET SASHELP.SHOES;
RUN;

OBS keyword:


OBS keyword defines the ending point (row number) to read the lines of an already existing dataset.


In the following code chunk we are telling SAS to read first 100 lines of dataset SASHELP.SHOES and copy it to our new data.

DATA MYLIB.FIRSTOBS_OBS;
SET SASHELP.SHOES (OBS = 100);
RUN;

FIRSTOBS keyword:


FIRSTOBS keyword defines the starting point (row number) to read the lines of an already existing dataset.

In the following code chunk we are telling SAS to read dataset SASHELP.SHOES from line 101 and copy it to our new data. Note that our new dataset has 295 rows.

DATA MYLIB.FIRSTOBS_OBS;
SET SASHELP.SHOES (FIRSTOBS = 101);
RUN;

FIRSTOBS and OBS together!


In the following query SAS will read data for rows 26-100 from SASHELP.SHOES and then will copy it to FIRSTOBS_OBS. Note our resulant data has 75 rows!

DATA MYLIB.FIRSTOBS_OBS;
SET SASHELP.SHOES (FIRSTOBS = 26 OBS = 100);
RUN;

FIRSTOBS and OBS can NEVER come with DATA or WHERE step


One should never use FIRSTOBS and OBS in DATA step and WHERE step. Sometimes it causes error or it does not lead to the desired result.

DATA MYLIB.FIRSTOBS_OBS(FIRSTOBS = 26 OBS = 100);
SET SASHELP.SHOES ;
RUN;
bottom of page