user3801272
user3801272

Reputation: 27

convert character variable to time in SAS

I have a variable called post_time which is of character type

Type     : Character
Length   :  5 
Foramt   : $5. 
Informat : $5.

eg: 00300 ,01250

How do I get time from this? can someone pls help me out? need the time to look like 03:00AM 12:50PM Need to display all the time in standard time zone

Upvotes: 1

Views: 3815

Answers (2)

Joe
Joe

Reputation: 63424

SAS will work directly with this via the HHMMSS informat.

data _null_;
  x = input('02100',HHMMSS5.);
  put x= timeampm9.;
run;

Any time zone concerns can be handled using either a time zone sensitive format, such as NLDATMTZ., and/or the TZONES2U or TZONEU2S functions, which work with DATETIME but may be able to work with your time values if you never go across a date boundary (though using timezones that's risky).

See the SAS Documentation page on Timezones for a more detailed explanation.

Upvotes: 4

Reeza
Reeza

Reputation: 21264

Assuming that the first three digits are hours and last two are minutes this may work:

data have;
input time $;
cards;
00300
00400
00234
;
run;

data want;
    set have;
    time_var=hms(
                input(substr(time, 1,3), 3.),
                input(substr(time, 4,2), 2.), 
                0);
    format time_var timeampm8.;
run;

Upvotes: 0

Related Questions