DATA Step, Macro, Functions and more

How to output last observation of a SAS data set

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

How to output last observation of a SAS data set

[ Edited ]

AS known to us, _n_ can be used in IF-Then statement for manipulation for any speciifed observation when the observation number is known. How to do the same thing for last observation since we do not know it's obervation number


Accepted Solutions
Solution
‎07-19-2016 10:48 AM
SAS Employee RMP
SAS Employee
Posts: 52

Re: How to output last observation of a SAS data set

[ Edited ]

Editor's note: this is a very popular question.  To help others to find the answer, we have consolidated the most helpful answers into this one reply as an Accepted Solution.  

 

or ...

/* Simple approach */
data last;
    /* get number of records (N) */
    if 0 then
        set sashelp.class nobs=nobs end=eof;
    /* use POINT= to get nth record */
    set sashelp.class point=nobs;
    output;
    stop;
run;

More robust approach from @data_null__:

 

/* Create sample data */
data class;
    set sashelp.class;
run;
data class;
    modify class end=eof;
    /* uncomment this to create empty set for test */
    *remove;
    if eof or ranuni(12344) lt .2 then
        remove;
run;
/* print all records */
proc print;
run;

/* find last record, handle empty sets */
data last;
    if eof then
        stop;
    do _n_ = nobs to 1 by -1 until(_error_ eq 0);
        _error_ = 0;
        set class point=_n_ nobs=nobs;
        _obs_ = _n_;
    end;
    if _error_ eq 0 then
        output;
    stop;
    set class(drop=_all_) end=eof;
run;

/* print last record */
proc print;
run;

View solution in original post


All Replies
Trusted Advisor
Posts: 2,113

Re: How to do ouput last observation of a SAS data

Check out the END= option on the SET statement. I think that will do what you want.
SAS Employee RMP
SAS Employee
Posts: 52

Re: How to do ouput last observation of a SAS data

data test;
set sashelp.class end=eof;
if eof then output;
run;
Occasional Contributor
Posts: 5

Re: How to do ouput last observation of a SAS data

I get it, Thanks alot!
Respected Advisor
Posts: 3,777

Re: How to do ouput last observation of a SAS data

If the purpose of the exercise is to access the last obs "only" then a more direct approach can be used.

[pre]
data class;
set sashelp.class;
run;
proc print;
run;
data class;
modify class end=eof;
*remove;
if eof or ranuni(12344) lt .2 then remove;
run;
proc print;
run;
data last;
if eof then stop;
do _n_ = nobs to 1 by -1 until(_error_ eq 0);
_error_ = 0;
set class point=_n_ nobs=nobs;
_obs_ = _n_;
end;
if _error_ eq 0 then output;
stop;
set class(drop=_all_) end=eof;
run;
proc print;
run;
[/pre] Added code to check for data sets with zero obs.


Message was edited by: data _null_;
Solution
‎07-19-2016 10:48 AM
SAS Employee RMP
SAS Employee
Posts: 52

Re: How to output last observation of a SAS data set

[ Edited ]

Editor's note: this is a very popular question.  To help others to find the answer, we have consolidated the most helpful answers into this one reply as an Accepted Solution.  

 

or ...

/* Simple approach */
data last;
    /* get number of records (N) */
    if 0 then
        set sashelp.class nobs=nobs end=eof;
    /* use POINT= to get nth record */
    set sashelp.class point=nobs;
    output;
    stop;
run;

More robust approach from @data_null__:

 

/* Create sample data */
data class;
    set sashelp.class;
run;
data class;
    modify class end=eof;
    /* uncomment this to create empty set for test */
    *remove;
    if eof or ranuni(12344) lt .2 then
        remove;
run;
/* print all records */
proc print;
run;

/* find last record, handle empty sets */
data last;
    if eof then
        stop;
    do _n_ = nobs to 1 by -1 until(_error_ eq 0);
        _error_ = 0;
        set class point=_n_ nobs=nobs;
        _obs_ = _n_;
    end;
    if _error_ eq 0 then
        output;
    stop;
    set class(drop=_all_) end=eof;
run;

/* print last record */
proc print;
run;
Respected Advisor
Posts: 3,777

Re: How to do ouput last observation of a SAS data

> or ...
>
> data test;
> if 0 then set sashelp.class nobs=nobs end=eof;
> set sashelp.class point=nobs;
> output;
> stop;
> run;

No, because if the last observation was REMOVEd as in my example, your program won't work.
SAS Employee RMP
SAS Employee
Posts: 52

Re: How to do ouput last observation of a SAS data

Good point. What you lose in readability/maintainability you gain in robustness.
Valued Guide
Posts: 2,175

Re: How to do ouput last observation of a SAS data

have we seen this topic before?
Respected Advisor
Posts: 3,777

Re: How to do ouput last observation of a SAS data

Do you mean how to find the last obs or the readability/maintainability of simple data steps?
Valued Guide
Posts: 2,175

Re: How to do ouput last observation of a SAS data

just the last obs. I'm sure I've read your code before.
PROC Star
Posts: 7,366

Re: How to do ouput last observation of a SAS data

Peter,

How many questions are "really" new?

Art
> just the last obs. I'm sure I've read your code
> before.
Occasional Contributor
Posts: 6

Re: How to do ouput last observation of a SAS data

Ever any new questions? Well I asked this one back in 2004...

http://marc.info/?l=sas-l&m=1166250948107016&w=2

☑ This topic is solved.

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

Discussion stats
  • 12 replies
  • 25234 views
  • 1 like
  • 7 in conversation