EDIT: the code at the end works, I've just made a mistake in the example. Thanks @yabwon for pointing this out
Hi,
I have two datasets (ds1 and ds2), each of them containg date variable. Now I want to join them using the condition that date in ds2 is less than 30 days before or 30 days after the date in ds1. In the example below, in the outcome dataset (want) observations for B are not matched as there are more than 30 days between 2018-12-25 and 2019-04-20. For A and C the condition is met, so the observations from ds2 should be added.
data ds1;
input date :yymmdd10. id $2. var1;
format date yymmdd10.;
datalines;
2019-03-31 A 10
2018-12-25 B 20
2019-06-15 C 30
;
run;
data ds2;
input date :yymmdd10. id $2. var2;
format date yymmdd10.;
datalines;
2019-02-25 A 15
2019-04-20 B 25
2019-07-10 C 35
;
run;
data want;
input date :yymmdd10. id :$2. var1 var2;
format date yymmdd10.;
datalines;
2019-02-25 A 10 15
2019-04-20 B 20 .
2019-07-10 C 30 35
;
run;
I've tried the following code, but values for id=A are not matched while they should be (there is less than 30 days between 2019-02-25 and 2019-03-31). C is mergin properly. What am I doing wrong?
proc sql;
create table merge as
select * from ds1 A
left join ds2 B
on A.id=B.id and B.date < A.date+30 and B.date> A.date-30;
quit;
Best regards,
Chris
Comparison:
ods listing;
data test;
input date :yymmdd10. id $2. date1 :yymmdd10.;
format date date1 yymmdd10.;
diff = date - date1;
datalines;
2019-03-31 A 2019-02-25
2018-12-25 B 2019-04-20
2019-06-15 C 2019-07-10
;
run;
proc print;
run;returns:
Obs date id date1 diff 1 2019-03-31 A 2019-02-25 34 2 2018-12-25 B 2019-04-20 -116 3 2019-06-15 C 2019-07-10 -25
All the best
Bart
@yabwon All clear now, thanks!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.