BookmarkSubscribeRSS Feed
chris2377
Quartz | Level 8

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

2 REPLIES 2
yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



chris2377
Quartz | Level 8

@yabwon All clear now, thanks!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 839 views
  • 0 likes
  • 2 in conversation