BookmarkSubscribeRSS Feed
Gil_
Quartz | Level 8
I have a code gives me last services
Proc sql;
Create table temp as select
A.id,
A.seq,
A.day,
B.date1 as date2,
Abs(a day-b.date1) as diff
From paper as a
Left join dep1 as b
On a.id=b.id
Group by a.id
Order by id seq, diff

Proc sql;
Create table want
As select id,day,date2, seq
From temp
Group by id , seq
Having min(diff)
;quit;

Data want;
Set temp;
By id seq
If first.seq;
Run;

It works 80%of time

Id. Seq. Day. Date2. Diff
A1. 9401. 05/18/2018. 05/25/2018. 7

What i expect was
Id. Seq. Day. Date2. Diff
A1. 9401. 05/18/2018. 04/27/2018. 21


What im looking for last day there was a service from 5/18/2018. Whichj is 04/27/2018
5 REPLIES 5
HB
Barite | Level 11 HB
Barite | Level 11

 

 

Some sample data and explanation would really improve things. Paper? Dep1?  These things have meaning?

 

Without investing too much, I'll hazard that right now you are finding the closest date before or after and what you really want is the closest date before. I'll further hazard that a WHERE clause like "where table1.date < table2.date" might move you in that direction. 

 

 

Gil_
Quartz | Level 8
Sorry abt that my lack of explaining ... paper is order of ticket day and dep is date2 of service

Whst im looking for is based on the day of paper ticket day table when was the last date2 service from the dep1 table
These 1st 3 col come paper. Date2 comes from dep1
Id. Seq. Day. Date2. Diff
A1. 9401. 05/18/2018. 04/27/2018. 21

I think my issue might be seq number
Gil_
Quartz | Level 8
Ok got it to work. I do have a question
Id. Day. Date2. Seq. Diff
Id1. 05/18/2018. 05/15/3018. 9401. 3

Id1. 05/18/2018. 04/27/3018. 9401. 21

Id1. 05/18/2018. 04/11/3018. 9401. 37

Id1. 05/18/2018. 04/02/3018. 9401. 46


How do i extract
Id1. 05/18/2018. 05/15/3018. 9401. 3
And disregard the rest ...





ballardw
Super User

My basic approach would look something like this if I understand the rule:

proc sort data=have;
   by id descending day descending date2;
run;

data want;
   set have;
   by id ;
   if first.id;
run;
Gil_
Quartz | Level 8
Thank you

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
  • 5 replies
  • 1557 views
  • 0 likes
  • 3 in conversation