99Valk
99Valk

Reputation: 243

Convert to a SAS Time

I have a txt file containing time in this format 161058.262 which is intended to be 16:10:58.262.

I cannot find an INFORMAT that will turn this value into the correct SAS numeric time value. TIME12.3 will convert into a numeric. It gets stored as 579824520 and displays using format TOD12.3 format as 22:22:00.000

Any suggestions?

Thanks Dan

Upvotes: 3

Views: 2438

Answers (3)

rkoopmann
rkoopmann

Reputation: 1136

code

data _null_;
    input int_val $10.;
    format time_val timeampm15.3;
    time_val = input(
                 prxchange('s/(\d?\d)(\d\d)(\d\d\.\d\d\d)/$1:$2:$3/',
                          -1, int_val),
                 time10.3);
    put int_val
    @15 time_val timeampm15.3
    @30 time_val 10.3;
datalines;
000000.001
012345.678
12345.678
161058.262
235959.999
run;

log

000000.001    12:00:00.000 AM     0.000
012345.678     1:23:45.600 AM  5025.600
12345.678      1:23:45.670 AM  5025.670
161058.262     4:10:58.200 PM 58258.200
235959.999    11:59:59.900 PM 86399.900
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds

Upvotes: 1

stevepastelan
stevepastelan

Reputation: 1304

I would do a quick transformation to make an existing informat work. For example, your format is the same as what B8601TM expects, except for the dot that separates the fraction of a second. You can strip out the dot from your string and then apply the informat.

Example:

data test;
    input t $10.;

    format tt TOD12.3;

    tt = inputn(compress(t, , "kn"),  "B8601TM9.3");

datalines;
161058.262
; run;

Upvotes: 5

Joe
Joe

Reputation: 63424

I don't know about a specific informat, but you certainly can work in colons, or do the math to make it into a time using HMS.

data test;
informat t_pre $10.;
input t_pre $;
t = input(catx(':',substr(t_pre,1,2),substr(t_pre,3,2),substr(t_pre,5)),TIME12.3);
*CATX concatenates using a delimiter, so this generates a string like 16:10:58.262;
*Then converts to TIME12.3;
put t= TIME12.3 t_pre=;
datalines;
161058.262
;;;;
run;

data test;
input t_pre;
t = hms(floor(t_pre/10000),floor(mod(t_pre,10000)/100),mod(t_pre,100));
*HMS generates a time value from hours, minutes, seconds, and allows milliseconds in the seconds;
*So we use combination of division and modulo to separate the number into component parts;
put t= TIME12.3;
datalines;
161058.262
;;;;
run;

Upvotes: 1

Related Questions