DATA Step, Macro, Functions and more

Last n obs from each group

Accepted Solution Solved
Reply
Super Contributor
Posts: 266
Accepted Solution

Last n obs from each group

can anyone please help me to get last n obs from each group with out sort the dataset?

Thanks


Accepted Solutions
Solution
‎09-17-2014 08:03 AM
Super User
Posts: 10,044

Re: Last n obs from each group

Posted in reply to data_null__

I was so honor being mentioned by SAS Master  Mr.  NULL .

View solution in original post


All Replies
Trusted Advisor
Posts: 3,215

Re: Last n obs from each group

It are the first n obs of each group when processing in reversed order.  For small data re-sorting is easy and quickest way.

---->-- ja karman --<-----
Super Contributor
Posts: 266

Re: Last n obs from each group

Thanks, but i cant sort the data, i need to get from as data it is.

Trusted Advisor
Posts: 3,215

Re: Last n obs from each group

please elaborate your limitations. 
With an external RDBMS there is no physical order defined only logical ordering. In that case a question like this is humbug.
With streaming data you could define a queue of defined size. The lag function of SAS is looking like that. Also a hash can be used for that
Big data with SAS-datasets (relative to your machine) can be processed using a point but than you are losing the first/last options.

What to do if there are less than n records in a group?   

---->-- ja karman --<-----
Super Contributor
Posts: 266

Re: Last n obs from each group

Hi, Thanks my issue has been solved.

Should i delete this post?

Respected Advisor
Posts: 3,156

Re: Last n obs from each group

No. On the contrary, I think you should share your solution with the forum. So should someone else have the similar issue, the answer becomes available.

Regards,

Haikuo

Respected Advisor
Posts: 3,799

Re: Last n obs from each group

Isn't this same questions showed us the clever solution to within the last month?

Solution
‎09-17-2014 08:03 AM
Super User
Posts: 10,044

Re: Last n obs from each group

Posted in reply to data_null__

I was so honor being mentioned by SAS Master  Mr.  NULL .

Trusted Advisor
Posts: 3,215

Re: Last n obs from each group

To add, the double dow loop http://support.sas.com/resources/papers/proceedings09/038-2009.pdf looks to uses the merge by with a reset at the same position / group order.

In that case you have a generic approach for a sorted dataset. 

---->-- ja karman --<-----
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 336 views
  • 0 likes
  • 5 in conversation