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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 774 views
  • 3 likes
  • 3 in conversation