DATA Step, Macro, Functions and more

Min date/time by multiple groups

Reply
Senior User
Posts: 1

Min date/time by multiple groups

Hey guys,

I am trying to query the min date and min time by multiple groups but no luck.

Sample data

LOC CAR DATE TIME
A 123 5/10 02:35
A 123 5/10 13:45
B 123 5/12 04:45
B 123 5/15 00:35
B 124 5/10 15:55
C 123 5/17 12:00
C 124 5/11 05:30
C 124 5/12 18:00

I want the result dataset to show

LOC CAR DATE TIME
A 123 5/10 2:35
B 123 5/12 4:45
B 124 5/10 15:55
C 123 5/17 12:00
C 124 5/11 05:30

So I want the minimum date and time for each car at each location.

I tried proc sql but over partition by function isn’t allowed and the min function isn’t giving me the desired output.

Any help would be greatly appreciated!


Thanks
PROC Star
Posts: 1,805

Re: Min date/time by multiple groups

Posted in reply to anhkha1205
proc sql;
create table want as
select *
from have
group by loc,car
having date=min(date) and time=min(time);
quit;
Respected Advisor
Posts: 4,736

Re: Min date/time by multiple groups

Posted in reply to anhkha1205

@anhkha1205

There is a glitch in @novinosrin SQL and it wouldn't return the desired result.

 

If you want us to post tested code then please make the effort and post sample data in the form of a working and tested SAS data step as I've done below.

Don't expect us to do such prep work for you or else get less answers, slower and less responses and untested code.

data have;
  infile datalines truncover dlm=' ';
  input LOC $ CAR DATE:ddmmyy. TIME :time.;
  format date date9. time time.;
  datalines;
A 123 01/04/10 13:35
A 123 01/05/10 02:35
A 123 01/05/10 13:45
B 123 01/05/12 04:45
B 123 01/05/15 00:35
B 124 01/05/10 15:55
C 123 01/05/17 12:00
C 124 01/05/11 05:30
C 124 01/05/12 18:00
;
run;

proc sql;
  create table want as
    select *
    from have
    group by loc,car
    having (date*86400+time)=min((date*86400+time))
    ;
quit;
Super User
Posts: 10,778

Re: Min date/time by multiple groups

Posted in reply to anhkha1205
data have;
  infile datalines truncover dlm=' ';
  input LOC $ CAR DATE:ddmmyy. TIME :time.;
  format date date9. time time.;
  datalines;
A 123 01/04/10 13:35
A 123 01/05/10 02:35
A 123 01/05/10 13:45
B 123 01/05/12 04:45
B 123 01/05/15 00:35
B 124 01/05/10 15:55
C 123 01/05/17 12:00
C 124 01/05/11 05:30
C 124 01/05/12 18:00
;
run;
proc sort data=have;
by _all_;
run;
data want;
 set have;
 by loc car;
 if first.car;
run;
Respected Advisor
Posts: 3,018

Re: Min date/time by multiple groups

[ Edited ]
Posted in reply to anhkha1205

This minimum by group is done very simply in PROC SUMMARY if you use actual SAS datetime values (which you should be using anyway). No sorting needed. No writing your own code in a datastep needed.

 

proc summary data = have nway;
    class loc car;
    var datetime;
    output out=want min=min_datetime;
run;
--
Paige Miller
Esteemed Advisor
Posts: 5,529

Re: Min date/time by multiple groups

Posted in reply to anhkha1205

Similar to @Patrick's code, maybe a bit less cryptic :

 

proc sql;
create table want as
select 
    loc,
    car,
    dhms(date,hour(time),minute(time),0) as datetime format=datetime16.
from have
group by loc, car
having datetime = min(datetime);
quit; 
PG
Ask a Question
Discussion stats
  • 5 replies
  • 171 views
  • 2 likes
  • 6 in conversation