BookmarkSubscribeRSS Feed
Crubal
Quartz | Level 8

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!

10 REPLIES 10
Patrick
Opal | Level 21

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.

Crubal
Quartz | Level 8
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.
LinusH
Tourmaline | Level 20
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
Crubal
Quartz | Level 8

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. 

Crubal
Quartz | Level 8
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.
Patrick
Opal | Level 21

@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.

Kurt_Bremser
Super User

@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.

RoyBelderok
Calcite | Level 5

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 10 replies
  • 12387 views
  • 0 likes
  • 6 in conversation