In the sample dataset below, I want to select all rows for an ID if the begin date with the most recent date value has status W. I am trying to include date condition in the following code, but its not working. Any suggestions?
proc sql;
create table want as Select *
From have
where ID in (select ID From have where status in ('W'));
quit;
sample data | |||
ID | status | begin date | end date |
1 | W | 12/5/2017 | 1/31/2018 |
1 | W | 12/5/2017 | 1/31/2018 |
1 | W | 12/5/2017 | 1/31/2018 |
1 | J | 11/1/2017 | 12/4/2017 |
2 | F | 4/6/2017 | 4/6/2017 |
2 | W | 11/16/2016 | 4/5/2017 |
3 | C | 12/11/2017 | 2/4/2018 |
3 | C | 12/11/2017 | 2/4/2018 |
3 | W | 10/26/2017 | 12/10/2017 |
3 | W | 10/26/2017 | 12/10/2017 |
4 | W | 5/1/2016 | 5/31/2018 |
4 | W | 5/1/2016 | 5/31/2018 |
4 | W | 5/1/2016 | 5/31/2018 |
4 | W | 5/1/2016 | 5/31/2018 |
5 | C | 12/23/2017 | 2/7/2018 |
5 | W | 12/7/2017 | 12/22/2017 |
5 | W | 12/7/2017 | 12/22/2017 |
6 | W | 12/15/2017 | 1/10/2018 |
6 | W | 12/15/2017 | 1/10/2018 |
6 | W | 12/15/2017 | 1/10/2018 |
6 | F | 5/3/2017 | 12/14/2017 |
6 | F | 5/3/2017 | 12/14/2017 |
7 | S | 4/26/2017 | 4/30/2017 |
7 | S | 4/26/2017 | 4/30/2017 |
7 | W | 11/9/2016 | 4/25/2017 |
7 | W | 11/9/2016 | 4/25/2017 |
7 | W | 11/9/2016 | 4/25/2017 |
8 | F | 12/16/2017 | 3/29/2018 |
8 | F | 12/16/2017 | 3/29/2018 |
8 | W | 4/17/2017 | 12/15/2017 |
8 | W | 4/17/2017 | 12/15/2017 |
9 | W | 8/18/2016 | |
9 | W | 8/18/2016 | |
10 | W | 12/8/2017 | |
10 | J | 10/31/2017 | 12/7/2017 |
11 | W | 9/25/2017 | 2/20/2018 |
Dataset I want | |||
ID | status | begin date | end date |
1 | W | 12/5/2017 | 1/31/2018 |
1 | W | 12/5/2017 | 1/31/2018 |
1 | W | 12/5/2017 | 1/31/2018 |
1 | J | 11/1/2017 | 12/4/2017 |
4 | W | 5/1/2016 | 5/31/2018 |
4 | W | 5/1/2016 | 5/31/2018 |
4 | W | 5/1/2016 | 5/31/2018 |
4 | W | 5/1/2016 | 5/31/2018 |
6 | W | 12/15/2017 | 1/10/2018 |
6 | W | 12/15/2017 | 1/10/2018 |
6 | W | 12/15/2017 | 1/10/2018 |
6 | F | 5/3/2017 | 12/14/2017 |
6 | F | 5/3/2017 | 12/14/2017 |
9 | W | 8/18/2016 | |
9 | W | 8/18/2016 | |
10 | W | 12/8/2017 | |
10 | J | 10/31/2017 | 12/7/2017 |
11 | W | 9/25/2017 | 2/20/2018 |
In general, SQL is the wrong tool to use when you want an outcome that depends on the order of the observations. SQL does not guarantee an order. Here's one way that assumes your data is ordered by ID and descending BEGIN_DATE:
data want;
set have;
by id descending begin_date;
if first.id then do;
output_flag = (status="W");
retain output_flag;
end;
if output_flag;
run;
After adding descending to the code, the code worked.
Thank you so much. Appreciate your help.
You need to include your requirement that the W appear on the last date into your query.
create table want as
select *
from have
where ID in
(select ID
from have
group by id
having (max(begin_date)=begin_date)
and (status in ('W'))
)
;
This code worked too. Thank you. Appreciate your help.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.