BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi friends,
what is most efficient way to find last 10 distinct obs from a data set.

we dont have such facility in SAS proc sql
like ---
select last 10 distinct abc, xyz from tbl where xyz='ijk';

Thank you.

regards,
Avi
6 REPLIES 6
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Look at following SAS programming opportunity:

1) capture SAS-reserved variable _N_ as OBSNUM for highest location identification.
2) use PROC SUMMARY to generate a _FREQ_ count. Specify ID OBSNUM to get the last occurence.
3) use SORT with BY DESCENDING _FREQ_ DESCENDING OBSNUM.


Scott Barry
SBBWorks, Inc.
deleted_user
Not applicable
Hey

Thanks for the reply.

Regards
Avi
ChrisNZ
Tourmaline | Level 20
If the table is too large to process (sort, dedup, summarise) as suggested above,
you can read it starting from the end:
[pre]
data t;
if 0 then set SASHELP.CLASS nobs=NOBS;
do I= NOBS to 1 by -1;
set SASHELP.CLASS point=I;
put AGE=;
end;
stop;
run;[/pre]
You can add some logic in the data step to do what you want, like filling up an array with unique values as you go.
deleted_user
Not applicable
Hey , Thanks Chris

I have got the required output...
Thank you all for your help..

Regards
Avi
deleted_user
Not applicable
Hi,

To find last n observation from a dataset

data temp;
set sashelp.class nobs=LASTN;
if _n_ > (LASTN-n);
run;

This will get you the last n obs.

If you want to get distinct obseravtions,
Please create a dataset with distinct obseravtion first and apply this logic

Hope this will help you.
BPD
Obsidian | Level 7 BPD
Obsidian | Level 7
A slight variation on the post from Chris@newzealand.
Store the number of obs - 9 into a macro variable.

data t;
if 0 then set SASHELP.CLASS nobs=NOBS;
call symput('nobs',nobs-9);
run;

Then read the data from that point in the dataset.
data t1;
set SASHELP.CLASS(firstobs=&nobs);
run;

Regards,

BPD

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 6 replies
  • 2410 views
  • 0 likes
  • 4 in conversation