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

Hello SAS community,

I have the dataset below:

id    term    med            start             

1     1          w             20090110         

1     1          x              20060324      

1     1          y              20070704       

1     1          z              20081124         

1     2          w              20090110           

1     2          x              20080324        

1     2          y              20070704       

1     2          z              20061124    

2     ....         

2          

2          

What I want to do is pick the earliest three start dates for each id-term pair. 

id    term    med            start 

1     1          x              20060324      

1     1          y              20070704       

1     1          z              20081124   

1     2          x              20080324        

1     2          y              20070704       

1     2          z              20061124 

2    ....

2

2

I am not sure how to choose these dates using SAS Proc SQL and would appreciate your help.  Thank you. 

1 ACCEPTED SOLUTION

Accepted Solutions
MadhuKorni
Quartz | Level 8

proc sort data = have;

by id term start;

data want;

set have;

retain cnt ;

if first.id or first.term then cnt = 1;

else cnt+1;

by id term;

if cnt le 3;

drop cnt;

run;

View solution in original post

3 REPLIES 3
gergely_batho
SAS Employee

proc rank data=have out=want(where=(start_rank<=3));

     var start;

     ranks start_rank;

     by id term;

run;

Steelers_In_DC
Barite | Level 11

Here's another solutions:

data have;

infile cards;

input id    term    med$            start;

cards;

1     1          w             20090110

1     1          x             20060324

1     1          y             20070704

1     1          z             20081124

1     2          w             20090110

1     2          x             20080324

1     2          y             20070704

1     2          z             20061124

;

data all want(drop=count);

set have;

by id term;

count + 1;

if first.id then count = 1;

if first.term then count = 1;

output all;

if count < 4 then output want;

run;

MadhuKorni
Quartz | Level 8

proc sort data = have;

by id term start;

data want;

set have;

retain cnt ;

if first.id or first.term then cnt = 1;

else cnt+1;

by id term;

if cnt le 3;

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 759 views
  • 0 likes
  • 4 in conversation