BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Steve_Shanghai
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
RMP
SAS Employee RMP
SAS Employee

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

12 REPLIES 12
Doc_Duke
Rhodochrosite | Level 12
Check out the END= option on the SET statement. I think that will do what you want.
RMP
SAS Employee RMP
SAS Employee
data test;
set sashelp.class end=eof;
if eof then output;
run;
Steve_Shanghai
Calcite | Level 5
I get it, Thanks alot!
data_null__
Jade | Level 19
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_;
RMP
SAS Employee RMP
SAS Employee

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;
data_null__
Jade | Level 19
> 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.
RMP
SAS Employee RMP
SAS Employee
Good point. What you lose in readability/maintainability you gain in robustness.
Peter_C
Rhodochrosite | Level 12
have we seen this topic before?
data_null__
Jade | Level 19
Do you mean how to find the last obs or the readability/maintainability of simple data steps?
Peter_C
Rhodochrosite | Level 12
just the last obs. I'm sure I've read your code before.
art297
Opal | Level 21
Peter,

How many questions are "really" new?

Art
> just the last obs. I'm sure I've read your code
> before.
bpowell
Obsidian | Level 7

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

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 12 replies
  • 95198 views
  • 3 likes
  • 7 in conversation