BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Maisha_Huq
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
hbi
Quartz | Level 8 hbi
Quartz | Level 8

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;

View solution in original post

2 REPLIES 2
hbi
Quartz | Level 8 hbi
Quartz | Level 8

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

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

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
  • 2 replies
  • 1282 views
  • 1 like
  • 3 in conversation