user3831696
user3831696

Reputation: 87

Reading Time Data using INFILE option

I have a .csv file which has some flight information. Sample data is shown below.

date|sched_dep_time|dep_time|sched_arr_time|arr_time
1/1/2013|515|517|819|830

The 515 here actually means 5:15Hrs. How can I read this data into SAS correctly? If I use the time. format, it is coming up with some strange timings. I have seen some code snippets, which has to be written exclusively to do these time conversions. But is there are more straight forward method available?

Upvotes: 1

Views: 35

Answers (2)

DomPazz
DomPazz

Reputation: 12465

I didn't realize the HHMMSS. INFORMAT would work. Reeza's answer is best. If you want a custom function, here you go.

options cmplib=work.fns;

proc fcmp outlib=work.fns.time;
function to_time(x);
    minutes = mod(x,100);
    hour = (x-minutes)/100;
    time = hms(hour,minutes,0);
    return (time);
endsub;
run;

data test;
format in_val   best. 
       out_time time.;
in_val = 512;
out_time = to_time(in_val);
put in_val out_time;
run;

Upvotes: 0

Reeza
Reeza

Reputation: 21274

Use the informat HHMMSS, which will read it in correctly.

data have;
informat date ddmmyy10. sched_dep_time dep_time sched_arr_time arr_time hhmmss.;
format sched_dep_time dep_time sched_arr_time arr_time time.;
input date sched_dep_time dep_time sched_arr_time arr_time;
cards;
1/1/2013 515 517 819 830
;
run;

proc print data=have;run;

Upvotes: 1

Related Questions