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!
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.
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.
Your code works with example data made from your tables. So your datasets must differ from what you gave us.
@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.
@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.
I know its well over 5 years since your post but if you're still troubled by this I think you should look into the CASE statement
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: