BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MichaelGong
Fluorite | Level 6

Hi all,

I met a problem which really bothers me a lot.

here is my data set:

ID    Date    Shares

1     1991     70 

1     1991     65    

1     1991     62

1     1992     80

1     1992     79

1     1992     69

2     1991     55

2     1991     52

2     1991     44

2     1992     85

3     1991     78

3     1991     76

3     1992     59

I want to extract the TOP 2 observations WITHIN in each ID&Date group, so below is my desired data set

ID     Date     Shares

1     1991     70

1     1991     65

1     1992     80

1     1992     79

2     1991     55

2     1991     52

2     1992     85

3     1991     78

3     1991     76

3     1992     59

I was thinking to give each observation within each ID&Date group a number starting from 1 to number of observation of this subgroup, then I can delete the observation with number larger than 2.

But unfortunately I failed...Does anyone can help me to figure out how to do that? I really appreciate!!!

1 ACCEPTED SOLUTION

Accepted Solutions
sureshv
Fluorite | Level 6

use full code

proc sort data=dsn;

by id date;

run;

data dsn22;

set dsn;

by id date;

if first.date then cnt=1;

else cnt+1;

if cnt <=2;

drop cnt;

run;

View solution in original post

2 REPLIES 2
sureshv
Fluorite | Level 6

use full code

proc sort data=dsn;

by id date;

run;

data dsn22;

set dsn;

by id date;

if first.date then cnt=1;

else cnt+1;

if cnt <=2;

drop cnt;

run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 948 views
  • 3 likes
  • 3 in conversation