DATA Step, Macro, Functions and more

last n distinct obs

Reply
N/A
Posts: 0

last n distinct obs

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
Super Contributor
Super Contributor
Posts: 3,174

Re: last n distinct obs

Posted in reply to deleted_user
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.
N/A
Posts: 0

Re: last n distinct obs

Hey

Thanks for the reply.

Regards
Avi
PROC Star
Posts: 1,760

Re: last n distinct obs

Posted in reply to deleted_user
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.
N/A
Posts: 0

Re: last n distinct obs

Hey , Thanks Chris

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

Regards
Avi
N/A
Posts: 0

Re: last n distinct obs

Posted in reply to deleted_user
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.
Occasional Contributor BPD
Occasional Contributor
Posts: 12

Re: last n distinct obs

Posted in reply to deleted_user
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
Ask a Question
Discussion stats
  • 6 replies
  • 496 views
  • 0 likes
  • 4 in conversation