Hello
I want to select first and last row of a data set.
I saw this code that say that in the case that data set is very big then we can tell SAS to read only first and last rows.
May anyone explian this code?
I know NOBS option in SET.
But I dont understand how this code work and how SAS knows to read only first and last observations.
What is the meaning of "STOP" in this code?
What is the meaning of "OUTPUT" in this code?
Why using 2 SET statements?
What is POINT var?
Thank you
data first_last_rows;
set SASHELP.cars nobs=_nobs_;
output;
set SASHELP.cars point=_nobs_;
output;
stop;
run;
The first SET statement reads the first observation from the source table as you're used to it. The OUTPUT statement then writes this row to the target table.
In the first statement there is also keyword nobs=_nobs_
This stores the total number of observations in variable _nobs_;
The second SET statement then uses direct access via keyword point=_nobs_
This reads the observation number stored in _nobs_ from the source table - and with _nobs_ containing the total number of observations this is going to be the very last observation.
And the last statement STOP then instructs SAS to not further iterate through the data.
You could even eliminate the STOP statement by using the (obs=1) dataset name parameter, as in:
data first_last_rows;
set SASHELP.cars (obs=1) ;
output;
set SASHELP.cars point=_nobs_ nobs=_nobs_;
output;
run;
If the input dataset has only one observation it will be output twice. Do you want that?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.