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

Hi Team,

 

How shall I convert below mentioned data into 24 hours format of hh:mm:ss (char).

 

data want ;
time="04:56 PM";output;
time="11:59 PM";output;
time="12:00 PM";output;
time="12:51 PM";output;
time="00:00 AM";output;
time="10:19 AM";output;
time="01:19 AM";output;
run;

 

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Use the TIME informat to convert those strings to actual time values (number of seconds) and then use the TOD format to convert it back to a string.

data have ;
  input time $8. ;
cards;
04:56 PM
11:59 PM
12:00 PM
12:51 PM
00:00 AM
10:19 AM
01:19 AM
;

data want;
  set have;
  time2=put(input(time,time8.),tod8.);
run;

proc print;
run;
Obs      time       time2

 1     04:56 PM    16:56:00
 2     11:59 PM    23:59:00
 3     12:00 PM    12:00:00
 4     12:51 PM    12:51:00
 5     00:00 AM    00:00:00
 6     10:19 AM    10:19:00
 7     01:19 AM    01:19:00

But it might be easier to deal with the values if you left them as numeric time values instead of strings.

  time2=input(time,time8.) ;
  format time2 tod8. ;

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Use the TIME informat to convert those strings to actual time values (number of seconds) and then use the TOD format to convert it back to a string.

data have ;
  input time $8. ;
cards;
04:56 PM
11:59 PM
12:00 PM
12:51 PM
00:00 AM
10:19 AM
01:19 AM
;

data want;
  set have;
  time2=put(input(time,time8.),tod8.);
run;

proc print;
run;
Obs      time       time2

 1     04:56 PM    16:56:00
 2     11:59 PM    23:59:00
 3     12:00 PM    12:00:00
 4     12:51 PM    12:51:00
 5     00:00 AM    00:00:00
 6     10:19 AM    10:19:00
 7     01:19 AM    01:19:00

But it might be easier to deal with the values if you left them as numeric time values instead of strings.

  time2=input(time,time8.) ;
  format time2 tod8. ;
pdhokriya
Pyrite | Level 9
Thank you so much for your quick reply. Its helpful for me. My approch was too long and also data was not proper.

data have ;
set want;
hh = scan(time,1,":") ;
mm = substr(time,4,2);
am_pm = scan(time,-1," ");
if hh ne "12" and am_pm = "PM" then hours = (hh + 12);
else if am_pm ne "PM" then hours = hh;
else if hh in ("00" "12") then hours=hh;
cm_time= cat(strip(hours) ||":"||strip(mm)||":00");
run;


Thank you again.
Ksharp
Super User
data want ;
time="04:56 PM"t;output;
time="11:59 PM"t;output;
time="12:00 PM"t;output;
time="12:51 PM"t;output;
time="00:00 AM"t;output;
time="10:19 AM"t;output;
time="01:19 AM"t;output;
format time tod.;
run;
pdhokriya
Pyrite | Level 9
Thank you for the solution..

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 8613 views
  • 3 likes
  • 3 in conversation