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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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

9 REPLIES 9
Tom
Super User Tom
Super User

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? 

varmac
Calcite | Level 5

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?

Linlin
Lapis Lazuli | Level 10

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

varmac
Calcite | Level 5

Thanks Linlin. That worked.

Tom
Super User Tom
Super User

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.

varmac
Calcite | Level 5

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

anshulgoel
Calcite | Level 5

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.

Tim_SAS
Barite | Level 11

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

ahtinuS
Fluorite | Level 6

Hi,

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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