proc sql noprint;
create table test as
select a.*, b.one
from dat1 as a left join
dat2 as b
on a.id eq b.id and ((a.date1 eq b.date1) or (b.date1 <= a.date1 <= b.date2))
quit;
How do I write this using merge statement ?
I was able to write the below, but how do I add the condition (b.date1 <= a.date1 <= b.date2)) ?
data test;
merge dat1(in=a) dat2(in=b);
by id date1;
if a;
run;
Thanks for your help
I guess that this is one of the cases where SQl can do things that the data step can't, since we could do the merge on id alone, but a subsequent subsetting if with the further conditions would prevent the output of an "a" record if there were only matching "b" IDs that don't fulfil the additional conditions.
Hello,
data test;
merge dat1(in=a) dat2(in=b rename=(date1=b_date1 date2=b_date2));
by id ;
if a and b_date1 <= date1 <= b_date2;
run;
I guess that this is one of the cases where SQl can do things that the data step can't, since we could do the merge on id alone, but a subsequent subsetting if with the further conditions would prevent the output of an "a" record if there were only matching "b" IDs that don't fulfil the additional conditions.
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.
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.