Time format Issue

Reply
Contributor
Posts: 60

Time format Issue

Hi,

I have a table in which time is in num 8 and format time8.. I am trying to convert it in to HH:MMSmiley FrustratedS AM/OM format.

When I am using below code

data test1;

infile datalines;

length time 6. time1 6.;

input time 6. time1 6.;

format time time12. time1 timeampm.;

datalines;

464633 464633

464628 464628

464632 464632

464624 464624

464634 464634

464632 464632

;

run;

OUTPUT is:

Time               Time1

129:03:5312:54:23 PM
129:03:4812:54:22 PM
129:03:5212:54:23 PM
129:03:4412:54:22 PM
129:03:5412:54:23 PM
129:03:5212:54:23 PM

For below code output is

data test1;

infile datalines;

length time 6.;

input time 6. ;

format time time8.;

datalines;

464633

464628

464632

464624

464634

464632

;

run;

OUTPUT

Time

129:03

129:03

129:03

129:03

129:03

129:03

and same variable is giving me below output

data test1;

infile datalines;

length time 6.;

input time 6. ;

format time timeampm.;

datalines;

464633

464628

464632

464624

464634

464632

;

run;

9:03:53 AM

9:03:48 AM

9:03:52 AM

9:03:44 AM

9:03:54 AM

9:03:52 AM

Can anyone let me know which one is the correct one and how to convert Time into HH:MMSmiley FrustratedS format.

My objective is to concatenate date and time field together?

Super User
Posts: 19,098

Re: Time format Issue

Is this a duration measure? Time is measured in seconds. So max value for a 24 hour period is 60*60*24 = 86400.

Your data indicates 6 days and 9 hours + some minutes. 

data time;

    do i=1 to 86400;

    format i timeampm.;

    output;

    end;

run;

data test1;

infile datalines;

input time  time1 ;

format time timeampm. time1 datetime21.;

datalines;

464633 464633

464628 464628

464632 464632

464624 464624

464634 464634

464632 464632

;

run;

Contributor
Posts: 60

Re: Time format Issue

Hi Reza,

I am also not sure just trying to figure it out. I put all analysis and out in my question.

Thanks

Super User
Posts: 19,098

Re: Time format Issue

Well basically all are valid options so you probably need to consult the documentation or ask someone who works with that data what the field should represent.

Contributor
Posts: 23

Re: Time format Issue

Are you sure it is a time variable?

I have seen numbers like that be the number of days since 1Jan1900 - a system recorded all dates like that.

464633 + 1Jan1900 = 14 Feb 1972

Would that make more sense?

Or the number of seconds since a particular date? In which case you need to know the base date which time is calculated from.

Good Luck!

Contributor
Posts: 60

Re: Time format Issue

Hi,

It's time varaiable and time is calculated in hrs. as sas stored it in numerical value starting from 1Jan 1060.

Super User
Posts: 11,121

Re: Time format Issue

SAS treats dates as days from 1 Jan 1960 and datetime values as seconds from 1 Jan 1960. Time is number of seconds since midnight. You are getting 129 hours because that's how hours are in 464633 seconds. The difference when you use the timeampm format is that it is discarding any extra hours of the 5 days worth and showing the clock time. If you use a datetime format you'll see that value could also be 06JAN60:09:03:53 which agrees with the clock time part. It looks like this variable may be a datetime. If you want just the time portion you can get that by TimVar=timepart(time);

I would recommend against setting a length on date, time and especially datetime variables as you may start truncating your data.

New Contributor
Posts: 4

Re: Time format Issue

ballardw make a very good point about setting date/time lengths. The way you have the length statements written, e.g.:

length time 6. time1 6.;

suggests that you may be confusing format specifications with length specifications. In a length statement, do not include a period after the numerical length specified. If you actually wanted the time and time1 variables to be internally stored as 6 bytes, specify:

length time 6 time1 6;

without the periods.

Ask a Question
Discussion stats
  • 7 replies
  • 360 views
  • 0 likes
  • 5 in conversation