BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Xamius32
Calcite | Level 5

  Lets say I want to join two tables, one of which has different values to join based on the date.

So data set 1:

PlayerDateValue
14/5/20125
15/23/201212
15/30/20127

dataset two:

PlayerTeamstart date
1ABC4/3/2012
1BCD5/12/2012
1CDE5/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.

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20

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;

Data never sleeps

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

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

PG
Peter_C
Rhodochrosite | Level 12

IT also looks like an opportunity for an interleaved SET statement

LinusH
Tourmaline | Level 20

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;

Data never sleeps
Xamius32
Calcite | Level 5

I think this is working. Thanks!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1871 views
  • 3 likes
  • 4 in conversation