BookmarkSubscribeRSS Feed
Scott86
Obsidian | Level 7

Hi everyone,

 

I looking at doing a left join with two tables. I have a data set 'X' that I want to left join to another data set where a trading group ID matches to data set 'Y' and the date modified from table 'X' is >= than start_date in table 'Y' and less than end_date in table 'Y' or end_date is blank.

 

I hope the above makes sense. Below is an attempt at a portion of the code to show you what I'm trying to do:

 

on a.trading_group_id = b.trading_group_id and a.date_modified >= b.start_date and a.date_modified < end_date or end_date = '';

 

Not to sure on how to do this. Maybe a where data step first to order by dates. 

 

Any help is appreciated.

Thanks

4 REPLIES 4
Reeza
Super User
For this issue, I'll usually recode the blank end dates and make them 2099.
Then you can use the following, which is easier to follow IMO.

And date between start_date and end_date

One thing to make sure when you're dealing with multiple conditions is to enclose them with parentheses especially if you're mixing AND/OR logic.
Scott86
Obsidian | Level 7

Thanks Reeza that is a good tip.

 

Where should I be adding parentheses?

 

Please rewrite the code below with the parentheses:

 

on a.trading_group_id = b.trading_group_id and a.date_modified >= b.start_date and a.date_modified < b.end_date or end_date = '';

 

Thanks mate

Reeza
Super User

@Scott86 wrote:

 

 

Please rewrite the code below with the parentheses:

 


I don't know what logic you want....you do 🙂

Make a small data set and test it out, which you should be doing anyways really.

You can work with subsets of your data by using the OBS= option.

 

option obs=1000;
Tom
Super User Tom
Super User

Is your END_DATE variable a character varaible? If so then how are you able to make a greater than or less than comparison between two values?  If your variable is coded to look like 'YYYY-MM-DD' then relative order will be preserved when comparing character versions of dates, but if you are using 'MM-DD-YYYY' or 'DD-MM-YYYY' or even 'ddMONyyyy' format then the order as character strings will not match the order as dates.

 

I assume you want to group your comparisons in this way so that you do not include every record with a missing end_date. Note the MISSING() function will work with both numeric and character variables.

 

on (a.trading_group_id = b.trading_group_id)
 and ( (a.date_modified >= b.start_date
         and (a.date_modified < b.end_date or missing(end_date))
       )
     )
;

 

If your varaibles are actual date values then perhaps you can simplify using BETWEEN and COALESCE()? Use the -1 to make the upper bound comparison of the BETWEEN match the < used in your original query instead of <= that is normally used.

on a.trading_group_id = b.trading_group_id
 and a.date_modified between b.start_date and coalesce(b.end_date,mdy(2099,1,1))-1

 

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 904 views
  • 0 likes
  • 3 in conversation