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
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
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?
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?
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
Thanks Linlin. That worked.
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.
Tom - I'll explore that option as well. Thanks for your quick responses.
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.
I don't think there is a TIMEAMPM. informat. Are you looking for the TIME. informat instead?
Hi,
I cannot seem to convert this 00:00 (character) to time, please help.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.