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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 5 replies
  • 836 views
  • 2 likes
  • 6 in conversation