BookmarkSubscribeRSS Feed
d0816
Quartz | Level 8

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  
IDstatusbegin dateend date
1W12/5/20171/31/2018
1W12/5/20171/31/2018
1W12/5/20171/31/2018
1J11/1/201712/4/2017
2F4/6/20174/6/2017
2W11/16/20164/5/2017
3C12/11/20172/4/2018
3C12/11/20172/4/2018
3W10/26/201712/10/2017
3W10/26/201712/10/2017
4W5/1/20165/31/2018
4W5/1/20165/31/2018
4W5/1/20165/31/2018
4W5/1/20165/31/2018
5C12/23/20172/7/2018
5W12/7/201712/22/2017
5W12/7/201712/22/2017
6W12/15/20171/10/2018
6W12/15/20171/10/2018
6W12/15/20171/10/2018
6F5/3/201712/14/2017
6F5/3/201712/14/2017
7S4/26/20174/30/2017
7S4/26/20174/30/2017
7W11/9/20164/25/2017
7W11/9/20164/25/2017
7W11/9/20164/25/2017
8F12/16/20173/29/2018
8F12/16/20173/29/2018
8W4/17/201712/15/2017
8W4/17/201712/15/2017
9W8/18/2016 
9W8/18/2016 
10W12/8/2017 
10J10/31/201712/7/2017
11W9/25/2017

2/20/2018

 

Dataset I want  
IDstatusbegin dateend date
1W12/5/20171/31/2018
1W12/5/20171/31/2018
1W12/5/20171/31/2018
1J11/1/201712/4/2017
4W5/1/20165/31/2018
4W5/1/20165/31/2018
4W5/1/20165/31/2018
4W5/1/20165/31/2018
6W12/15/20171/10/2018
6W12/15/20171/10/2018
6W12/15/20171/10/2018
6F5/3/201712/14/2017
6F5/3/201712/14/2017
9W8/18/2016 
9W8/18/2016 
10W12/8/2017 
10J10/31/201712/7/2017
11W9/25/20172/20/2018

 

 

4 REPLIES 4
Astounding
PROC Star

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;

d0816
Quartz | Level 8

After adding descending to the code, the code worked.

Thank you so much. Appreciate your help.

Tom
Super User Tom
Super User

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'))
 )
;
d0816
Quartz | Level 8

This code worked too. Thank you. Appreciate your help.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

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