BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AnnaNZ
Quartz | Level 8

Hi

 

I have a dataset with many identical IDs, which were taken up at different times (datetime 18). 

I want to know the earlierst and the last time that an ID was taken up ( and calculate the timeinterval).

 

So some REFER_ID appeas on multiple different times (LAT_UPDT_DT) as indicted below in red. 

The aim is to calculate the timedifference between the fist time and the last time they appear. 

 

Capture.PNG

 

What I have: 

 

 

proc sort data = main_vic; by REFER_ID ;
run;

proc sql;
  create table want as
    select min(LST_UPDT_DT) as min_date format=datetime18.,
                 max(LST_UPDT_DT) as max_date format=datetime18.
             from main_vic;
quit;

This of course gives me only the first and the last date of all REFER_ID

How can I group REFER_ID so  that I always receive the first and last visit of that REFER_ID?

 

I tried to add a by statement, but was not sucessful. 

Any ideas are highly appreaciated. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try the below code, it is untested but should work

 

also included the difference in days and hours

 

proc means data=have ;
class refer_id;
var lst_updt_dt;
output out=want min=min max=max;
run;

data want2;
set want;
diffinhours=(max-min)/3600;
diffindays=(max-min)/86400;
run;
Thanks,
Jag

View solution in original post

4 REPLIES 4
Jagadishkatam
Amethyst | Level 16

Please try the below code, it is untested but should work

 

also included the difference in days and hours

 

proc means data=have ;
class refer_id;
var lst_updt_dt;
output out=want min=min max=max;
run;

data want2;
set want;
diffinhours=(max-min)/3600;
diffindays=(max-min)/86400;
run;
Thanks,
Jag
AnnaNZ
Quartz | Level 8
Great solution for this problem, thanks a lot !
kiranv_
Rhodochrosite | Level 12

Something like this. I took a similar example instead of date time. I have used somenum

data abc;
input id someotherval $ somenum;
datalines;
1 a 20
1 b 30
1 c 40
2 a 40
2 a 50
2 a 45
;
run;

proc sql;
select a.*, max(somenum)-min(somenum) as diff from abc a
group by id;
;
quit;

 

Ksharp
Super User
data abc;
input id someotherval $ somenum;
datalines;
1 a 20
1 b 30
1 c 40
2 a 40
2 a 50
2 a 45
;
run;

proc sql;
select a.*, range(somenum) as diff from abc as a
group by id;
;
quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 596 views
  • 3 likes
  • 4 in conversation