BookmarkSubscribeRSS Feed
SThomas
Calcite | Level 5

All, I have the following two data sets:

 

have1:

 

IDDate1
6774983128AUG2015:08:49:00.000000
6801112010SEP2015:11:03:00.000000

 

have2: 

IDDate2
6774983126AUG2015:08:49:00.000000
6774983127AUG2015:08:49:00.000000
67749831

29AUG2015:08:49:00.000000

 

I want the following dataset:

 

Want:

IDDate1Date2
6774983128AUG2015:08:49:00.00000027AUG2015:08:49:00.000000
6801112010SEP2015:11:03:00.000000 

 

The idea is to merge the two files based on the exact same ID, but I also want the Date2 to be the latest date that is still before Date1. There might be many Instances of a Date2 that come before Date1 (I want the most recent one), one instance, or no instances.

 

Does anyone have any ideas? 

 

Thanks!

4 REPLIES 4
ballardw
Super User

Pet peeve: Calling datetime values "dates".

So are your datetime variables SAS datetime values (have a format like datetime20.) or are they character?

SThomas
Calcite | Level 5

My apologies. They are both datetime20.

Astounding
PROC Star

Assuming both data sets are in order by both ID and date:

 

data want;

set have2 (rename=(date2=date1) in=in2) have1;

by id date1;

if first.id then date2=.;

retain date2;

if in2 then date2=date1;

else output;

run;

 

mkeintz
PROC Star

Assuming both have1 and have2 are sorted by id/date, then this interleaving operation will work:

 

data want;
  set have2 (in=in2 rename=(date2=date1))
      have1 (in=in1);
  by id date1;

  date2=ifn(in1 and lag(in2) and first.id=0,lag(date1),.);
  if in1;
  format date1 date2 datetime25.6;
run;

 

 

 

Notes:

  1.  In the SET statement HAVE2 must precede HAVE1  (for when dates are exactly tied)
  2. The IFN function tests whether the record-in-hand is from have1, and is immediately preceded by a HAVE2 record for the same id.  If true then the prior date1 value is really a date2 var that has an earlier date.
  3. The IFN ALWAYS updates the lag queues, even if the lagged value is not returned by IFN.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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
  • 764 views
  • 0 likes
  • 4 in conversation