BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

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;

 

 

3 REPLIES 3
Patrick
Opal | Level 21

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.

mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

If the input dataset has only one observation it will be output twice.  Do you want that?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1153 views
  • 0 likes
  • 4 in conversation