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

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 2052 views
  • 1 like
  • 3 in conversation