BookmarkSubscribeRSS Feed
yfan777
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.

yfan777
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
yfan777
Obsidian | Level 7

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

 

 

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