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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1377 views
  • 0 likes
  • 4 in conversation