BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bobbyc
Fluorite | Level 6

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

2 REPLIES 2
Loko
Barite | Level 11

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;

 

Kurt_Bremser
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1313 views
  • 0 likes
  • 3 in conversation