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

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1148 views
  • 0 likes
  • 2 in conversation