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

Hello,

 

I have two datasets (testa and testb) which I like to merge by the date-values . But I can not merge by the date-variable itself because sometimes the dates differ by some days but should be written to the same observation. Therfore I want to introduce a range according to the date of the first dataset und merge the information of the second dataset only if that date fits into that range. On the bottomline I try to solve this problem:

 

data testa;

input nr date_a date_low date_up int $@@;

cards;

1 3 1 5 e

1 5 3 7 ee

2 4 2 6 f

3 8 6 10 g

4 8 6 10 h

4 13 11 15 i

;run;

data testb;

input nr date_b empf $@@;

cards;

1 2 a

1 2 b

3 8 c

4 9 d

;run;

 

*This is what I want to get;

data testz;

input nr date_a date_low date_up int $ date_b empf $@@;

cards;

1 3 1 5 e 2 a

1 3 1 5 e 2 b

1 5 3 7 ee . .

2 4 2 6 f . .

3 8 6 10 g 8 c

4 8 6 10 h 9 d

4 13 11 15 i . .

;

run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

Yes, just use <= with the range:

data testa;
  input nr date_a date_low date_up int $@@;
cards;
1 3 1 5 e
1 5 3 7 ee
2 4 2 6 f
3 8 6 10 g
4 8 6 10 h
4 13 11 15 i
;
run;
data testb;
  input nr date_b empf $@@;
cards;
1 2 a
1 2 b
3 8 c
4 9 d
;
run;
proc sql;
  create table WANT as
  select  A.*,
          B.DATE_B,
          B.EMPF
  from    WORK.TESTA A
  full join WORK.TESTB B
  on      A.NR=B.NR
  and     A.DATE_LOW <= B.DATE_B <= A.DATE_UP;
quit;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

Yes, just use <= with the range:

data testa;
  input nr date_a date_low date_up int $@@;
cards;
1 3 1 5 e
1 5 3 7 ee
2 4 2 6 f
3 8 6 10 g
4 8 6 10 h
4 13 11 15 i
;
run;
data testb;
  input nr date_b empf $@@;
cards;
1 2 a
1 2 b
3 8 c
4 9 d
;
run;
proc sql;
  create table WANT as
  select  A.*,
          B.DATE_B,
          B.EMPF
  from    WORK.TESTA A
  full join WORK.TESTB B
  on      A.NR=B.NR
  and     A.DATE_LOW <= B.DATE_B <= A.DATE_UP;
quit;
Gundi
Fluorite | Level 6
Hi,
thank you, that helped a lot!
AnnaBrown
Community Manager

Hi Gundi,

 

I'm glad you found some useful info! If one of the replies was the exact solution to your problem, can you "Accept it as a solution"? Or if one was particularly helpful, feel free to "Like" it. This will help other community members who may run into the same issue know what worked.

 

Thanks!

Anna


Join us for SAS Community Trivia
SAS Bowl XXIX, The SAS Hackathon
Wednesday, March 8, 2023, at 10 AM ET | #SASBowl

sas-innovate-2024.png

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.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1517 views
  • 1 like
  • 3 in conversation