BookmarkSubscribeRSS Feed
CynthiaFan
Obsidian | Level 7

Hi everybody,

 

I have a question about data management. 

 

According to the labdate, extract the earliest 2 labs named as “b”  after testdate for each ID.

 

Thank you so much, 

 

Here is the dataset

 

id

lab

testdate

labdate

1

a

1/1/2015

1/2/2015

1

a

1/1/2015

1/3/2015

1

b

1/1/2015

12/1/2014

1

b

1/1/2015

1/2/2015

1

b

1/1/2015

1/3/2015

1

b

1/1/2015

1/4/2015

2

a

2/1/2015

1/2/2015

2

a

2/1/2015

2/1/2015

2

b

2/1/2015

2/1/2015

2

b

2/1/2015

2/2/2015

2

b

2/1/2015

2/3/2015

2

b

2/1/2015

2/4/2015

3

a

3/1/2015

2/1/2015

3

a

3/1/2015

3/1/2015

3

b

3/1/2015

4/1/2015

3

b

3/1/2015

3/2/2015

4

b

4/1/2015

1/1/2015

4

a

4/1/2015

4/1/2015

4

a

4/1/2015

5/1/2015

4

b

4/1/2015

5/2/2015

4

b

4/1/2015

5/3/2015

4 REPLIES 4
Patrick
Opal | Level 21

This looks like a study question to me. If so then you should first spend some of your own time with it and only post a question if you get stuck.

If you get stuck: Please post the code you've got already and explain what's not working. 

 

It would also help if you could post data as a SAS data step creating a SAS table.

CynthiaFan
Obsidian | Level 7

thank you. I will learn to ask question more professionally.

PGStats
Opal | Level 21

Is this a homework? In any case, experiment with the following to understand why it works.

 


proc sort data=have; by id lab labdate; run;

data want;
set have; by id;
where lab="b" and labdate>testdate;
if first.id then order = 0;
order + 1;
if order < 3 then output;
drop order;
run;

proc print data=want noobs; run;
PG
CynthiaFan
Obsidian | Level 7

Thank you for the response especially for the part of obtaining the first and second earliest dates.

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1263 views
  • 2 likes
  • 3 in conversation