SAS Programming

DATA Step, Macro, Functions and more
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..

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 8046 views
  • 3 likes
  • 3 in conversation