Ije
Ije

Reputation: 43

Convert numerical variable into SAS time variable

I imported a cvs file and have a variable 'departure time' in my sas file that consists of four digits, e.g. 0856 for 08.56 AM. I would like SAS to recognise it as time and want it to appear as 08:56. I have tried:

put DEP_TIME hhmm5.;

format DEP_TIME hhmm5.;

Doesn't work. Can't seem to figure this out.

Any clues?

Upvotes: 0

Views: 144

Answers (2)

data _null_
data _null_

Reputation: 9109

Informat B8601TM.

33         data _null_;
34            t='0856';
35            time = input(t,B8601TM.);
36            format time time.;
37            put 'NOTE: ' (_all_)(=);
38            run;

NOTE: t=0856 time=8:56:00

Upvotes: 2

Chris J
Chris J

Reputation: 7769

I don't think there's an informat which will convert a 4-digit string to a time.

There's a couple of ways to do this, either using hms() and substr() functions, or a custom picture format :

proc format ;                                                   
  picture TM
    low-high = '00:00' ;                                
run ;

data want ;
  set have ;
  /* hms and substr method */
  new_time1 = hms(substr(dep_time,1,2),substr(dep_time,3,2)) ;
  format new_time1 hhmm5. ;

  /* input / put with picture format */
  new_time2 = input(put(input(dep_time,4.),tm.),time5.) ;
  format new_time2 hhmm5. ;
run ;

Upvotes: 0

Related Questions