BookmarkSubscribeRSS Feed
deleted_user
Not applicable
This is my input data set with 2 variables id and sal and my output should only contain top 5 observations for each id value.
Input Data set:
data sam;
input id sal;
cards;
1 4000
1 4000
1 4000
2 5000
2 6000
2 3000
2 3000
2 3000
2 3000
2 3000
2 3000
3 5000
3 5000
3 5000
3 5000
3 5000
3 5000
3 5000
4 6000
4 6000
run;

Output Dataset:

1 4000
1 4000
1 4000
2 5000
2 6000
2 3000
2 3000
2 3000
3 5000
3 5000
3 5000
3 5000
3 5000
4 6000
4 6000
2 REPLIES 2
LinusH
Tourmaline | Level 20
First, sort you data by id and descending sal.
Then go through your data with a data step, counting no of observations in each by-group and output the first five each. You can accomplish this using first.id and a retain variable.

/Linus
Data never sleeps
deleted_user
Not applicable
Hi Linus,

Thank you very much for ur reply.

sam

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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