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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.