BookmarkSubscribeRSS Feed
knveraraju91
Barite | Level 11

Dear,

Please suggest in my sql code. I am not getting the out put i need and  have warning messages in log

I need to check every record in one dataset with two set by id and check if date2 -date1 greater than 4 days

 

output needed

id date1 date2

1 2018-11-19 2019-12-01
1 2019-10-20 2019-11-10
1 2019-10-20 2019-10-25
1 2019-10-20 2019-12-01
1 2019-10-23 2019-12-01
1 2019-10-23 2019-11-10

 

 

 

data one;
input id date1 $10.;
datalines;
1 2019-10-20
1 2019-10-23
1 2018-11-19
;
data two;
input id date2 $10.;
datalines;
1 2019-10-25
1 2019-11-10
1 2019-12-01
;


proc sql;
create table three as
select * from one as a left join two as b
on a.id=b.id
group by a.id,date1
having input(date2,yymmdd10.)- input(date1,yymmdd10.) gt 4;
quit;

 

2 REPLIES 2
Shmuel
Garnet | Level 18

You have not show the expected results, so I hope next code will satisfy you:

data one;
input id date1 anydtdte10.;
format date1 yymmdd10.;
datalines;
1 2019-10-20
1 2019-10-23
1 2018-11-19
;
run;

data two;
input id date2 anydtdte10.;
format date2 yymmdd10.;
datalines;
1 2019-10-25
1 2019-11-10
1 2019-12-01
;
run;

proc sql;
create table three as
select coalesce(a.id,b.id) as ID, 
a.date1, b.date2 from one as a left join two as b on a.id = b.id and b.date2 - a.date1 gt 4 order by a.id,date1 ; quit;

output result:

Obs	ID date1	date2
1	1	2018-11-19	2019-12-01
2	1	2018-11-19	2019-11-10
3	1	2018-11-19	2019-10-25
4	1	2019-10-20	2019-11-10
5	1	2019-10-20	2019-10-25
6	1	2019-10-20	2019-12-01
7	1	2019-10-23	2019-12-01
8	1	2019-10-23	2019-11-10
Kurt_Bremser
Super User

For a.date = 2018-11-19, ALL dates in TWO satisfy your condition. Why do you keep only the last one, while for other dates you take three or two?

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