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.
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.
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;
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;
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;
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.