## Functions for maximum time values

Solved
Frequent Contributor
Posts: 131

# Functions for maximum time values

Hi, Are there any specific functions for finding the maximum for time variables as opposed to other numeric variables? I need to find the maximum time from an array of time variables.

Thank you!

Accepted Solutions
Solution
‎11-04-2015 07:50 AM
Contributor
Posts: 66

## Re: Functions for maximum time values

Posted in reply to Maisha_Huq

I would try something like this:

``````/* if your variables are datetime variables, use the next two examples ... */
DATA have1;
FORMAT dtm1 dtm2 dtm3 dtm4 DATETIME20.;
dtm1 = '02NOV2015:19:31:12'dt;
dtm2 = '02NOV2015:14:53:32'dt;
dtm3 = '02NOV2015:06:06:11'dt;
dtm4 = '02NOV2015:06:29:14'dt;
RUN;

DATA want1;
SET have1;
FORMAT max_of_dtm DATETIME20.;
ARRAY dtm_array{*} dtm1-dtm4;
max_of_dtm = MAX(of dtm_array{*});
RUN;

/* if your variables are time variables, use the next two examples ... */
DATA have2;
FORMAT time_jim time_mary time_peter time_john TIME8.;
time_jim   = '19:31:12't;
time_mary  = '14:53:32't;
time_peter = '06:06:11't;
time_john  = '06:29:14't;
RUN;

DATA want2;
SET have2;
FORMAT max_of_time TIME8.;
ARRAY time_array{*} time_jim time_mary time_peter time_john;
max_of_time = MAX(of time_array{*});
RUN;
``````

All Replies
Solution
‎11-04-2015 07:50 AM
Contributor
Posts: 66

## Re: Functions for maximum time values

Posted in reply to Maisha_Huq

I would try something like this:

``````/* if your variables are datetime variables, use the next two examples ... */
DATA have1;
FORMAT dtm1 dtm2 dtm3 dtm4 DATETIME20.;
dtm1 = '02NOV2015:19:31:12'dt;
dtm2 = '02NOV2015:14:53:32'dt;
dtm3 = '02NOV2015:06:06:11'dt;
dtm4 = '02NOV2015:06:29:14'dt;
RUN;

DATA want1;
SET have1;
FORMAT max_of_dtm DATETIME20.;
ARRAY dtm_array{*} dtm1-dtm4;
max_of_dtm = MAX(of dtm_array{*});
RUN;

/* if your variables are time variables, use the next two examples ... */
DATA have2;
FORMAT time_jim time_mary time_peter time_john TIME8.;
time_jim   = '19:31:12't;
time_mary  = '14:53:32't;
time_peter = '06:06:11't;
time_john  = '06:29:14't;
RUN;

DATA want2;
SET have2;
FORMAT max_of_time TIME8.;
ARRAY time_array{*} time_jim time_mary time_peter time_john;
max_of_time = MAX(of time_array{*});
RUN;
``````
Esteemed Advisor
Posts: 5,540

## Re: Functions for maximum time values

Posted in reply to Maisha_Huq

SAS dates and datetimes are just plain numbers counting days and seconds, respectively. So any numeric operator or function can be used with them. Sum, difference, min, max, and range work just as you would expect, as long as all operands are of the same type (all times or all dates). SAS will even let you do silly things like myDate**2 without complaining, that is, until you try to print the resulting value as a date.

hth.

PG
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 2 replies
• 202 views
• 1 like
• 3 in conversation