## converting character \$4 to military time

Solved
Occasional Contributor
Posts: 8

# converting character \$4 to military time

[ Edited ]

I have tried the following in Windows 9.3:

``````data want;
set have;
Varnew=input(Varold,time.);
FORMAT Varnew time5.;
run;``````

The procedure runs, but only one observation and two variables result when there should be 1000s.

``````NOTE: Numeric values have been converted to character values at the places given by:
(Line):(Column).
548:16
NOTE: There were 1 observations read from the data set have.
NOTE: The data set want has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.04 seconds``````

The data is 4 characters long and in military time. I ultimately want to convert the observations to numeric format so I can group them by the hour. They are currently by the minute.

Accepted Solutions
Solution
‎10-25-2017 12:49 PM
SAS Super FREQ
Posts: 830

## Re: converting character \$4 to military time

You can use the HHMMSS. informat to read a hhmm char value and translate it to a SAS time value.

See code below:

``````data t;
do charTime =
"0001"
,"0002"
,"0003"
,"2356"
,"2357"
,"2358"
,"2359"
;
sasTime = input(charTime, hhmmss4.);
output;
end;
format sasTime tod8.;
run;``````

All Replies
Super User
Posts: 9,840

## Re: converting character \$4 to military time

I don't know what you mean by military time?  This is where showing example data helps.

Occasional Contributor
Posts: 8

## Re: converting character \$4 to military time

The character data is 4 characters long:

0001

0002

0003

....

2356

2357

2358

2359

I want to convert this character variable to a numeric time format.

Posts: 1,848

## Re: converting character \$4 to military time

[ Edited ]

From your log lines

``NOTE: There were 1 observations read from the data set Varold.``
`NOTE: The data set Varnew has 1 observations and 2 variables.`

it seems that you have not psted the right code.

According to posted code varold and varnew are variables not datasets !

Occasional Contributor
Posts: 8

## Re: converting character \$4 to military time

you are correct:

``````NOTE: Numeric values have been converted to character values at the places given by:

(Line):(Column).

548:16

NOTE: There were 1 observations read from the data set have.

NOTE: The data set want has 1 observations and 2 variables.

NOTE: DATA statement used (Total process time):

real time 0.03 seconds

cpu time 0.04 seconds``````
SAS Super FREQ
Posts: 4,275

## Re: converting character \$4 to military time

Your log messages do not match the program that you submitted.

The TIME. format displays time in 'military time' (24-hour clock) but does not print a leading zero for 06:00.

If you want the leading zero, use the TOD. format.

I suggest you use the ANYDTTME. informat to convert the strings to a time value, as follows:

``````data t;
length s \$8.;
input s;                 /* a string */
t = input(s, ANYDTTME.); /* a (numeric) time value */
format t tod8.;          /* tell SAS to format the number as a 24-hour time */
datalines;
23:02:03
22:57:16
06:00:00
04:40:22
run;

proc print;run;
``````

Occasional Contributor
Posts: 8

## Re: converting character \$4 to military time

why do datalines need to be included? Isn't the data being fed from 't'?
SAS Super FREQ
Posts: 4,275

## Re: converting character \$4 to military time

I used DATALINES so that everyone could see the data.  If you have a data set 'have' that contains the variable s, you can use

``````data t;
SET have; /* the 'have' dataset contains the character variable s */
t = input(s, ANYDTTME.); /* a (numeric) time value */
format t tod8.; /* tell SAS to format the number as a 24-hour time */
run;``````

Occasional Contributor
Posts: 8

## Re: converting character \$4 to military time

605 data want;

606 set have;

607 Varnew=input(varold,anydttme.);

608 FORMAT varnew tod8.;

609 run;

NOTE: There were 1167321 observations read from the data set have.

NOTE: The data set new has 1167321 observations and 113 variables.

NOTE: DATA statement used (Total process time):

real time 3.88 seconds

cpu time 3.52 seconds

610 proc freq data= want;

611 tables varnew/nocum;

612 run;

NOTE: There were 1167321 observations read from the data set WORK.TRAVELTIME.

NOTE: PROCEDURE FREQ used (Total process time):

real time 1.24 seconds

cpu time 1.21 seconds

``````
The FREQ Procedure
endtimeN Frequency Percent
[][][] /*3 column table here with blank cells*/
Frequency Missing = 1167321

``````

The output table still produces no data, BUT at least all observations are now being read.

SAS Super FREQ
Posts: 4,275

## Re: converting character \$4 to military time

If the data are really of the form

0001

0002

etc

then those are invalid data strings, which is why the informat is converting them to missing. Try creating a new string that inserts a colon between the second and third characters, like this:

``````data t;
length varOld \$4. s \$5.;
input varOld;                 /* a string */
s = catx(":", substr(varOld,1,2), substr(varOld,3,2));
t = input(s, ANYDTTME.); /* a (numeric) time value */
format t tod8.;          /* tell SAS to format the number as a 24-hour time */
datalines;
0001
0002
0003
2356
2357
2358
2359
;

proc print; run;
``````
Occasional Contributor
Posts: 8

## Re: converting character \$4 to military time

it works with datalines but when I put the set statement in, I get errors again.

``````24   data want;
25   set have;
26   length varold \$4. varnew \$5.;
27   input varold;
28   varnew =catx(":", substr(varold,1,2),substr(varold,3,2));
29   want=input(varnew,anydttme.);
30   format want tod8.;
31

ERROR: No DATALINES or INFILE statement.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set want may be incomplete.  When this step was stopped there were
0 observations and 114 variables.
WARNING: Data set want was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time           0.04 seconds
cpu time            0.04 seconds

32   proc print;
33   run;

NOTE: There were 1 observations read from the data set want.
NOTE: PROCEDURE PRINT used (Total process time):
real time           0.03 seconds
cpu time            0.01 seconds

``````
Solution
‎10-25-2017 12:49 PM
SAS Super FREQ
Posts: 830

## Re: converting character \$4 to military time

You can use the HHMMSS. informat to read a hhmm char value and translate it to a SAS time value.

See code below:

``````data t;
do charTime =
"0001"
,"0002"
,"0003"
,"2356"
,"2357"
,"2358"
,"2359"
;
sasTime = input(charTime, hhmmss4.);
output;
end;
format sasTime tod8.;
run;``````
Occasional Contributor
Posts: 8

## Re: converting character \$4 to military time

Posted in reply to Bruno_SAS

where in the code does the data file go if I am not using datalines (very much a novice here)

☑ This topic is solved.

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

Discussion stats
• 12 replies
• 236 views
• 3 likes
• 5 in conversation