How to convert string to time?

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

How to convert string to time?

The attached dataset has time in 24 hour format, see below:

000257

000341

000419

000433

000437

000451

002538

002719

002903

003029

003135

003141

So 133846 indicates 1 pm 38 minutes 46 seconds.  But the value is in string.  How could I convert the TIME string into SAS time?  I used the following code:

data WANG;

     set HAVE;

     time_numeric = input(time, time5.);

run;

But it doesn't seem to work.  SAS reports:

NOTE: Invalid argument to function INPUT at line 24 column 8.

time=000257 timej=. _ERROR_=1 _N_=11

NOTE: Invalid argument to function INPUT at line 24 column 8.

time=000341 timej=. _ERROR_=1 _N_=12

Could you guys help me~~

Attachment

Accepted Solutions
Solution
‎03-03-2014 03:02 AM
Super User
Posts: 6,928

Re: How to convert string to time?

Do this:

data wang;

set have;

  format time_numeric time8.;

  time_numeric = input(substr(time,1,2) !! ':' !! substr(time,3,2) !! ':' !! substr(time,5,2),time8.);

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎03-03-2014 03:02 AM
Super User
Posts: 6,928

Re: How to convert string to time?

Do this:

data wang;

set have;

  format time_numeric time8.;

  time_numeric = input(substr(time,1,2) !! ':' !! substr(time,3,2) !! ':' !! substr(time,5,2),time8.);

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 106

Re: How to convert string to time?

Hi ,

Use  time_numeric=input(time,hhmmss6.);


this should read your time.

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 2634 views
  • 0 likes
  • 3 in conversation