Help using Base SAS procedures

PROC PRINT

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

PROC PRINT

How do I print only the first and the last observations of a dataset using the proc print?


Accepted Solutions
Solution
‎09-13-2016 03:01 AM
Super User
Posts: 5,083

Re: PROC PRINT

You can't.  You have to create a data set (or a view) containing the first and last observation.  Luckily, that is not so difficult:

 

data want;

do _n_=1, _nobs_;

   set have nobs=_nobs_ point=_n_;

   output;

end;

stop;

run;

View solution in original post


All Replies
Solution
‎09-13-2016 03:01 AM
Super User
Posts: 5,083

Re: PROC PRINT

You can't.  You have to create a data set (or a view) containing the first and last observation.  Luckily, that is not so difficult:

 

data want;

do _n_=1, _nobs_;

   set have nobs=_nobs_ point=_n_;

   output;

end;

stop;

run;

Contributor
Posts: 45

Re: PROC PRINT

Can you please explain me how this code works?

Super User
Posts: 5,083

Re: PROC PRINT

Here's how the pieces interact.

 

The NOBS= option on the SET statement creates a temporary variable, holding the number of observations in the data set.

 

Normally, the SET statement starts at the beginning of the data set and reads the next observation every time.  The POINT= option changes that, reading the observation designated by the POINT= variable.

 

The OUTPUT statement outputs the current observation, without waiting for later DATA step processing to occur.

 

The STOP statement halts the DATA step, to prevent looping that would read the same observatiosn over and over again.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 317 views
  • 2 likes
  • 2 in conversation