BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alo_moon
Calcite | Level 5

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

5 REPLIES 5
Reeza
Super User

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;
alo_moon
Calcite | Level 5

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 

 

Tom
Super User Tom
Super User

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 ;
ballardw
Super User

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. 


 

Tom
Super User Tom
Super User

Why try to create datetime values from strings that are only time?  What date did you want to use?

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 737 views
  • 4 likes
  • 4 in conversation