BookmarkSubscribeRSS Feed
anhkha1205
Calcite | Level 5
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
5 REPLIES 5
novinosrin
Tourmaline | Level 20
proc sql;
create table want as
select *
from have
group by loc,car
having date=min(date) and time=min(time);
quit;
Patrick
Opal | Level 21

@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;
Ksharp
Super User
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;
PaigeMiller
Diamond | Level 26

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
PGStats
Opal | Level 21

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1553 views
  • 2 likes
  • 6 in conversation