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

Catch up on SAS Innovate 2026

Dive into keynotes, announcements and breakthroughs on demand.

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