- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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. ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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. ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content