Table of Contents
Selecting the first N rows of a dataset in SAS can be achieved by using the “obs=” option in the “set” statement. This option allows the user to specify the number of observations to be read from the dataset. By setting the value of “obs=” to N, the first N rows of the dataset will be selected and displayed. This functionality is particularly useful when dealing with large datasets and wanting to view only a specific number of rows. Additionally, the “obs=” option can also be combined with other SAS statements and procedures to perform various data manipulation tasks on the selected rows.
Select the First N Rows of a Dataset in SAS
Here are the two most common ways to select the first N rows from a dataset in SAS:
Method 1: Select First Row
data first_row;
set original_data;
if _N_ = 1thenoutput;
run;
Method 2: Select First N Rows
data first_N_rows;
set original_data;
if _N_ <= 5thenoutput; /*select first 5 rows*/run;The following examples show how to use each method with the following dataset in SAS:
/*create dataset*/
data original_data;
input team $ points rebounds;
datalines;
Warriors 25 8
Wizards 18 12
Rockets 22 6
Celtics 24 11
Thunder 27 14
Spurs 33 19
Nets 31 20
Mavericks 34 10
Kings 22 11
Pelicans 39 23
;
run;
/*view dataset*/proc printdata=original_data;
Example 1: Select First Row
The following code shows how to select just the first row of the dataset:
/*create new dataset that contains only the first row*/
data first_row;
set original_data;
if _N_ = 1thenoutput;
run;
/*view new dataset*/proc printdata=first_row;
We can see that the new dataset contains only the first row of the original dataset.
Example 2: Select First N Rows
The following code shows how to select the first five rows of the dataset:
/*create new dataset that contains first 5 rows of original dataset*/
data first_N_rows;
set original_data;
if _N_ <= 5thenoutput;
run;
/*view new dataset*/proc printdata=first_N_rows;

To select a different number of starting rows, simply change the value after _N_ in the code above.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
Cite this article
stats writer (2024). How can I select the first N rows of a dataset in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-select-the-first-n-rows-of-a-dataset-in-sas/
stats writer. "How can I select the first N rows of a dataset in SAS?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-select-the-first-n-rows-of-a-dataset-in-sas/.
stats writer. "How can I select the first N rows of a dataset in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-select-the-first-n-rows-of-a-dataset-in-sas/.
stats writer (2024) 'How can I select the first N rows of a dataset in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-select-the-first-n-rows-of-a-dataset-in-sas/.
[1] stats writer, "How can I select the first N rows of a dataset in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can I select the first N rows of a dataset in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
