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

Hello!

I am trying to merge two datasets, but I am not sure if what I want it is possible?

I want the DATE (in data2) to fit within the correct period (in data1), which has a start_date and an end_date…

Data1:

ID        Period   Start_date       End_date

10        1          01.01.2012      31.12.2013

10        2          01.01.2013      31.12.2014

10        3          01.01.2014      31.12.2015

20        1          01.06.2012      31.05.2013

20        2          01.06.2013      31.05.2014

Data2:

ID        DATE              YIELD

10        01.03.2013      20

10        01.05.2013      30

10        01.10.2014      50

10        01.11.2015      60

20        01.09.2013      70

This is how I want the output to look like:

ID        Number  Start_date       End_date       DATE             YIELD

10        1          01.01.2012      31.12.2013      01.03.2013      20

10        1          01.01.2012      31.12.2013      01.05.2013      30

10        2          01.01.2013      31.12.2014      01.10.2014      50

10        3          01.01.2014      31.12.2015      01.11.2015      60

20        1          01.06.2012      31.05.2013      .           .

20        2          01.06.2013      31.05.2014      01.09.2013      70

So far my SAS-skills haven't succeed, hopefully there are someone out there who could help?

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I disagree that you want a full join. However, I also think you are missing some matches that exist in your data. I suggest trying the following:

proc sql noprint;

  create table want as

    select *

      from data1 a

        left join data2 b

          on a.id=b.id

            where a.start_date<=b.date<=a.end_date

  ;

quit;

View solution in original post

5 REPLIES 5
Reeza
Super User

You can use SQL join where the join is using a between. One of the few cases where SQL is better than a data step.

untested:

proc sql;

create table want as

select a.*, b.*

from data1 as a

full join data2 as b

on b.date between a.start_date and b.end_date;

quit;

Kristinos
Calcite | Level 5

Amazing how easy it can be done... Should have learned SQL, but there is no time...

THANK YOU VERY MUCH!! Smiley Happy

art297
Opal | Level 21

I disagree that you want a full join. However, I also think you are missing some matches that exist in your data. I suggest trying the following:

proc sql noprint;

  create table want as

    select *

      from data1 a

        left join data2 b

          on a.id=b.id

            where a.start_date<=b.date<=a.end_date

  ;

quit;

Reeza
Super User

There's an obs in Data2 that is in the final file, so I assumed full join...I'm assuming the OP's post is correct of course Smiley Happy

Kristinos
Calcite | Level 5

You are right .

With the first version all data in data2 were matched with both ID,

but your SQL seems to work. Smiley Happy

THANK YOU!!

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!

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.

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
  • 5 replies
  • 593 views
  • 3 likes
  • 3 in conversation