Hello,
Can you please walk me through how to convert this inconsistent text column to either time AM/PM or a 24hour time format?
time_column:
9:28 AM
20:20
5:11 PM
7:30
11:12 PM
2:20 PM
23:20
I have tried and it's not working.
data consist;
set dataset;
want=input(time_column, mdyampm23.);
format want dateampm.;
run;
I really appreciate any help you can provide.
Try ANYDTTME informat.
data have;
input time_Char $20.;
cards;
9:28 AM
20:20
5:11 PM
7:30
11:12 PM
2:20 PM
23:20
;;;;
run;
data want;
set have;
time_sas = input(time_char, anydttme.);
format time_sas time8.;
run;
proc print data=want;
run;
Try ANYDTTME informat.
data have;
input time_Char $20.;
cards;
9:28 AM
20:20
5:11 PM
7:30
11:12 PM
2:20 PM
23:20
;;;;
run;
data want;
set have;
time_sas = input(time_char, anydttme.);
format time_sas time8.;
run;
proc print data=want;
run;
Thanks Reeza.
From your help, I was able to convert this to AM/PM
data want;
set have;
convert_ampm=input(column_data, time8.);
format convert_ampm timeampm8.;
run;
final_hour output:
9:30am
1:43am
9:06pm
Make sure to test it with the strings like '12:00' and '12:30'.
I seem to remember that SAS will default that to midnight and most humans instead would consider those as being in the middle of the day.
If that is a problem then do something like:
convert_ampm=input(column_data, time8.);
if column_data=:'12:' and not findw(column_data,'pm','i') then convert_ampm+'12:00't ;
This seems to work for creating 24-hour time for the example values.
data example; infile datalines dlm=','; input time_column :time16.; format time_column time16.; datalines; 9:28 AM, 20:20 , 5:11 PM, 7:30 , 11:12 PM, 2:20 PM, 23:20, ;
Why use MYDYAMPM? That expects date components like mm-dd-yy at the beginning of the value, like 06-07-2022.
And you would need to have date component for the format dateampm to be meaningful.
If you have actual date elements then you need to provide an actual example of the entire value.
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates, times and datetimes.
@alo_moon wrote:
Hello,
Can you please walk me through how to convert this inconsistent text column to either time AM/PM or a 24hour time format?
time_column:
9:28 AM
20:20
5:11 PM
7:30
11:12 PM
2:20 PM
23:20
I have tried and it's not working.
data consist;
set dataset;
want=input(time_column, mdyampm23.);
format want dateampm.;
run;
I really appreciate any help you can provide.
Why try to create datetime values from strings that are only time? What date did you want to use?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.