DATA Step, Macro, Functions and more

Join two tables by date falls into the range of dates

Reply
Regular Contributor
Posts: 154

Join two tables by date falls into the range of dates

Hi, 

 

I have two tables: a & b. 

 

Table a:                                                          

Location |  Date_1 |  Price

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

L1           |  09/08   |   104

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

L1           |  09/09   |   134

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

L1           |  09/10   |    78

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

.........................................

 

Table b:

Location |  Start_Date |  End_Date  | Season_Id

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

L1           |  09/07         |  09/09         |      1

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

L1           |  09/10         |   11/30        |      2

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

L1           |  12/01         |   12/31        |      3

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

L1           |  01/01         |   09/06        |      4

 

I would like to join table a and table b: when 'Date_1' in table a falls in the range of 'Start_Date' & 'End_Date' in table b, corresponding 'Season_Id' could be added to a particular row. 

 

Want:

Location |  Date_1 |  Price  |  Season_Id

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

L1           |  09/08   |   104   |        1

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

L1           |  09/09   |   134   |        1    

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

L1           |  09/10   |    78    |         2

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

..............................................................

 

My sas code:

PROC SQL;
	 	Create Table Want as select 
			A.*, B.Season_Id from  A,  B
			where A.Date_1 >= B.Start_Date and A.Date_1 <= B.End_Date
			and A.Location = B.Location;
	Quit;

But it returns an empty table. 

 

How could I fix it?

 

Thank you!

Respected Advisor
Posts: 4,673

Re: Join two tables by date falls into the range of dates

The condition as such looks o.k. to me and should return rows so I assume something isn't o.k. with your variables. Are the Date variables numeric and do they contain SAS date values?

 

If you want us to post actually working and tested SAS code: Please post your sample data as a tested SAS data step creating the sample data.

Regular Contributor
Posts: 154

Re: Join two tables by date falls into the range of dates

Thanks for your reminding. It is resolved if I output the dataset into excel. As it has year information but I shortcut it to 'mmddyy5.' to let it show just month and date.
Super User
Posts: 5,851

Re: Join two tables by date falls into the range of dates

By optical examining it looks ok, even if your dates are treated as strings.
Please attach your data as datalines if you want someone else to try your code.
Data never sleeps
Regular Contributor
Posts: 154

Re: Join two tables by date falls into the range of dates

You are right, actually although in SAS date is in (month / date) format, it has year information if I output to excel. And year cannot match. 

Super User
Posts: 9,890

Re: Join two tables by date falls into the range of dates

Your code works with example data made from your tables. So your datasets must differ from what you gave us.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 154

Re: Join two tables by date falls into the range of dates

Posted in reply to KurtBremser
Thanks! I output my data into excel and found that my date is actually 03/09/2017. As I used 'mmddyy5.' format for 'Date_1' in table a, and It actually has year information. Table b has different year information. So that I cannot match.
Respected Advisor
Posts: 4,673

Re: Join two tables by date falls into the range of dates

@Crubal It would technically be possible to join just on the day and month part of a date - but I guess that wouldn't make business sense.

Super User
Posts: 9,890

Re: Join two tables by date falls into the range of dates


@Crubal wrote:
Thanks! I output my data into excel and found that my date is actually 03/09/2017. As I used 'mmddyy5.' format for 'Date_1' in table a, and It actually has year information. Table b has different year information. So that I cannot match.

I can only repeat the same old, same old:

POST EXAMPLE DATA IN A DATA STEP WITH DATALINES!

Without solid example data, all our attempts at helping you are just guesswork, and lack of such eample data only causes a totally unnecessary delay in the path to a solution.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 8 replies
  • 184 views
  • 0 likes
  • 4 in conversation