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!
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 25. Read more here about why you should contribute and what is in it for you!
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.