- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please attach your data as datalines if you want someone else to try your code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Your code works with example data made from your tables. So your datasets must differ from what you gave us.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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