Lets say I want to join two tables, one of which has different values to join based on the date.
So data set 1:
Player | Date | Value |
---|---|---|
1 | 4/5/2012 | 5 |
1 | 5/23/2012 | 12 |
1 | 5/30/2012 | 7 |
dataset two:
Player | Team | start date |
---|---|---|
1 | ABC | 4/3/2012 |
1 | BCD | 5/12/2012 |
1 | CDE | 5/23/2012 |
So I want another column in dataset 1 to give me the team for the specific date. So the new column would have ABC for the first one, and CDE for the second and third observation.
Does that make sense?
Any help is appreciated.
If this is a common query, you may consider to store table two using SCD type 2 style, by creating an end data for each team membership row. The current row will have a "hig-date", a date that will never exist (at least during our life time.
You can easily create this on existing data by using retain for the previous records start data (in a data step).
In this way, queries like this gets much simpler, just using a between-and join condition:
proc sql;
create table three as
select one.player, one.date, one.value, two.team, two.startDate
from one inner join two
on one.player = two.player and
one.Date between two.startDate and two.EndDate
;
quit;
Try this :
proc sql;
create table three as
select one.player, one.date, one.value, two.team, two.startDate
from one inner join two on one.player=two.player and startDate<=date
group by one.player, one.date
having two.startDate=max(two.startDate);
quit;
PG
IT also looks like an opportunity for an interleaved SET statement
If this is a common query, you may consider to store table two using SCD type 2 style, by creating an end data for each team membership row. The current row will have a "hig-date", a date that will never exist (at least during our life time.
You can easily create this on existing data by using retain for the previous records start data (in a data step).
In this way, queries like this gets much simpler, just using a between-and join condition:
proc sql;
create table three as
select one.player, one.date, one.value, two.team, two.startDate
from one inner join two
on one.player = two.player and
one.Date between two.startDate and two.EndDate
;
quit;
I think this is working. Thanks!
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.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.