DATA Step, Macro, Functions and more

Converting time from character to time

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Converting time from character to time

Hi all,

I've raw data for time in character format (hhmm) and the variable name is raw_time:

0733

1132

0729...

I need to convert this into numeric format as:

7:33

11:32

7:29

While I'm able to get this output in the desired format, I'm getting a SAS note: Invalid argument to function INPUT at line nn column yy. Did anyone run into this issue before? or have any suggestions on how to correct it?

I've done the following programming:

....

time1=substr(raw_time,1,1);

time2=substr(raw_time,3,2);

time3=trim(time1)||':'||trim(time2);

format TIME4 time5.;

TIME_FINAL=input(time3,time5.);

Thanks,

varmac


Accepted Solutions
Solution
‎05-02-2012 09:50 PM
Super Contributor
Posts: 1,636

Re: Converting time from character to time

data have;

input raw_time $;

cards;

0733

1132

0729

710

715

11

1029

;

data want;

set have;

if length(raw_time)=2 then time=input(('0'||':'||raw_time),time5.);

   else

time = input(substr(raw_time,1,length(raw_time)-2)||':'||substr(raw_time,length(raw_time)-1),time5.);

format time time5.;

proc print;run;

                        Obs    raw_time     time

                              1       0733       7:33

                              2       1132      11:32

                              3       0729       7:29

                              4       710        7:10

                              5       715        7:15

                              6       11          0:11

                              7       1029      10:29

View solution in original post


All Replies
Super User
Super User
Posts: 7,076

Re: Converting time from character to time

Works fine for me with your sample records?

413  data want ;

414    input raw_time $4. ;

415    time = input(substr(raw_time,1,2)||':'||substr(raw_time,3),time5.);

416    format time time5.;

417    put (_all_) (=);

418  cards;

raw_time=0733 time=7:33

raw_time=1132 time=11:32

raw_time=0729 time=7:29

Are you sure you read it as character? Are you sure there are no invisible characters like CR , TAB or LF hiding in your character variable?

Are you sure the leading zeros are there in the character string? 

Occasional Contributor
Posts: 5

Re: Converting time from character to time

Tom,

Thanks for your reply. The raw data is in character format and there are no invisible characters. I realized that not all raw data have leading zeros. They are actually a mixed bag. For e.g.:

0733

1132

0729

710

715

11

1029

But I'm getting the output just fine. It's just that I'm getting that SAS Note. Do you suggest I make any changes?

Solution
‎05-02-2012 09:50 PM
Super Contributor
Posts: 1,636

Re: Converting time from character to time

data have;

input raw_time $;

cards;

0733

1132

0729

710

715

11

1029

;

data want;

set have;

if length(raw_time)=2 then time=input(('0'||':'||raw_time),time5.);

   else

time = input(substr(raw_time,1,length(raw_time)-2)||':'||substr(raw_time,length(raw_time)-1),time5.);

format time time5.;

proc print;run;

                        Obs    raw_time     time

                              1       0733       7:33

                              2       1132      11:32

                              3       0729       7:29

                              4       710        7:10

                              5       715        7:15

                              6       11          0:11

                              7       1029      10:29

Occasional Contributor
Posts: 5

Re: Converting time from character to time

Thanks Linlin. That worked.

Super User
Super User
Posts: 7,076

Re: Converting time from character to time

I usually convert to a number and back to a string using the Z format to add the leading zeros.

raw_time=put(input(raw_time,4.),Z4.);

Might not be as fast as pre-pending zeros to the string, but it more closely models my mental image of what is happening.

Occasional Contributor
Posts: 5

Re: Converting time from character to time

Tom - I'll explore that option as well. Thanks for your quick responses.

Occasional Contributor
Posts: 8

Re: Converting time from character to time

Hi,

I am facing similar problem.

Text = "The current time is 11:34:34 PM. Have a nice day";

We need to extract the time i.e. 11:34:34 PM from the Text and convert it to military time in the dataset i.e. 21:34:34.

What I did was -

data test;

Text = "The current time is 11:34:34 PM. Have a nice day";

time = input(substr(text,21,11),timeampm.);

run;

I am getting the following error: timeampm informat not found.

Please help.

Super Contributor
Posts: 394

Re: Converting time from character to time

Posted in reply to anshulgoel

I don't think there is a TIMEAMPM. informat. Are you looking for the TIME. informat instead?

Occasional Contributor
Posts: 12

Re: Converting time from character to time

Hi,

I cannot seem to convert this 00:00 (character) to time, please help.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 23006 views
  • 3 likes
  • 6 in conversation