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 |
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.
thank you. I will learn to ask question more professionally.
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;
Thank you for the response especially for the part of obtaining the first and second earliest dates.
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!
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.