Help using Base SAS procedures

TO FInd the last 5th observation

Reply
N/A
Posts: 0

TO FInd the last 5th observation

I have a Data set but i dont know the no of observations are there now i want the last 5 th observation.
SAS Employee
Posts: 160

Re: TO FInd the last 5th observation

Here is one way of doing it.

[pre]
data input(keep=CharColumn);
input CharColumn $;
datalines;
H090301
C090302
I090303
H090304
C090305
I090306
H090307
C090308
I090309
;
run;

proc sql NOPRINT;
select nobs into :nobs
from sashelp.vtable
where libname='WORK' and memname ='INPUT';
quit;
%put &nobs;

data last5;
set input END=last;
if _n_ > (&nobs - 5) then output;
run;[/pre]
Respected Advisor
Posts: 3,777

Re: TO FInd the last 5th observation

How about a more direct(access) approach?

[pre]
data input;
input CharColumn $;
obs + 1;
datalines;
H090301
C090302
I090303
H090304
C090305
I090306
H090307
C090308
I090309
;;;;
run;
data last5;
do point=nobs-4 to nobs;
set input point=point nobs=nobs;
output;
end;
stop;
run;
proc print;
run;

*** OR just compute FIRSTOBS and start reading from there.
proc sql NOPRINT;
select nobs-4 into :firstobs separated ' '
from sashelp.vtable
where libname='WORK' and memname ='INPUT';
quit;
%put FIRSTOBS=&firstobs;

proc print data=input(firstobs=&firstobs);
run;
[/pre]
Ask a Question
Discussion stats
  • 2 replies
  • 106 views
  • 0 likes
  • 3 in conversation