BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have a Data set but i dont know the no of observations are there now i want the last 5 th observation.
2 REPLIES 2
GertNissen
Barite | Level 11
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]
data_null__
Jade | Level 19
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]

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore Now →
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 1278 views
  • 0 likes
  • 3 in conversation