Desktop productivity for business analysts and programmers

Time format/conversion

Posts: 0

Time format/conversion

I have a file that has time values listed as number, example

090101. This is 09:01:01 but SAS sees it as a number.

Does anyone know how I can convert the 090101 into a time value so I can take two time values and find the difference?
Posts: 0

Re: Time format/conversion

is it so difficult to use the online help ?

there is an informat there that would help you.

When you search, remember the format we might use to descibe the time layout HMS

happy hunting

Posts: 0

Re: Time format/conversion

I had tried previously searching the Online Help and also Google and I am not sure how to use the informats.

Even after your help, I am still having a hard time getting my number to convert to a SAS time.

my data looks like this

data nick;

Posts: 0

Re: Time format/conversion

if possible, adapt your code, slightly. Here is my sas log [pre]14 data nick;
15 a='90101';
16 y=input(a,hhmmss.);
17 format y time. ;
18 put _all_ ;
19 run;

a=90101 y=9:01:01 _ERROR_=0 _N_=1
NOTE: The data set WORK.NICK has 1 obs[/pre]
Posts: 0

Re: Time format/conversion

SAS has only two data types: character strings and numbers.
SAS has tons of formats to display these things per how we need them displayed.
SAS has tons of informats to convert text and binary data into an appropriate character string or number.

You input data with either the input statement or input function (also ninput)
OR you access a data table, either SAS or database, and can then assign or re-assign a format and/or informat to the variable for use within SAS. The association is kept in the SAS dataset header.

Hope this helps your understanding of what you are trying to do.
Posts: 8,720

Re: Time format/conversion

I think one of the hardest things to do with SAS is figure out what kinds of conversions need to take place. First, I always find it helpful to reiterate the facts about SAS dates and times:

SAS dates and times are just numbers. But when they are stored, internally in the data set, they do NOT look like dates and times. They are numbers which represent an offset from 0. So for dates, 0 is Jan 1, 1960 and 11/15/1950 is 3334 days BEFORE Jan 1, 1960 so the internal number for 11/15/1950 is -3334 (this is the way the date is stored inside the SAS dataset). Today is April 18, 2008 -- that is 17640 days AFTER Jan 1, 1960 so the internally stored value for today's date is 17640. If you display SAS date or time values WITHOUT a format, you will see numbers that do not look like dates or times. But as soon as you use a SAS date or time format on one of these numbers, then SAS knows how to "translate" from the internally stored number and shows you the date or time in a readable, understandable form.

Times work the same way. There are 86400 seconds in a day (24 * 60 * 60). You can see how the seconds add up by running this program:
data sastime;
infile datalines;
input desc $ hr min sec;
timeval = hms(hr,min,sec);
begin 0 0 0
firstsec 0 0 1
hour1 1 0 0
three 3 0 0
six 6 0 0
nine 9 0 0
noon 12 0 0
recess 15 0 0
dinner 18 0 0
bedtime 21 0 0
news 22 0 0
lastsec 23 59 59
midnite 24 0 0

proc print data=sastime noobs;
title 'unformatted time values';


And the output from the program is this:
unformatted time values

desc hr min sec timeval

begin 0 0 0 0
firstsec 0 0 1 1
hour1 1 0 0 3600
three 3 0 0 10800
six 6 0 0 21600
nine 9 0 0 32400
noon 12 0 0 43200
recess 15 0 0 54000
dinner 18 0 0 64800
bedtime 21 0 0 75600
news 22 0 0 79200
lastsec 23 59 59 86399
midnite 24 0 0 86400


So, by 6 in the morning, more than 21000 seconds have already gone by. Any single time, measured in HH:MMSmiley FrustratedS for a single day, will have one of these numbers (from 1 to 86400) For 9:01:01, SAS would want to represent that time internally as 32461 (thirty-two thousand, four hundred and sixty one seconds past midnight).

A SAS variable that represents a particular date AND time (as a date/time stamp) represents the number of seconds between January 1, 1960 and an hour/minute/second within a specified date. But since you didn't ask about date/time values, I'll stop with the "facts" here.

Your issue is that you have a number, 90101. You can look at that number and because you know that it represents a time, you can "read" it as a time: 9:01:01 -- but SAS does not know that that number represents a time. SAS can't mentally insert colons between the hours, minutes and seconds, like we can. It only sees a number (ninety thousand one hundred and one). So you have to convert your number.

The reason that Peter's example worked is that he passed a character string to the INPUT function. The most common use of the INPUT function is to turn a character string into a number. INPUT is wonderful at turning character strings that are dates and character strings that are times into the internal SAS number that SAS wants to have to store the dates and/or times.

One way to make your NUMBER, 90101, work with the INPUT function is to turn it into a character string before you pass it to the INPUT function. (This program does that...and shows you how you can turn a 11151950 into a date value, too):
data nicktime;
nicknum = 90101;
charval = put(nicknum,z6.);
sastime = input(charval,hhmmss.);

olddate = 11151950;
chardate = put(olddate,8.);
sasdate = input(chardate,mmddyy8.);

chardt = '18Apr2008:09:09:01';
sasdt = input(chardt,datetime18.);

proc print data=nicktime;
title 'What is the formatted value for time and date';
format sastime time8. sasdate mmddyy10. sasdt datetime18.;

proc print data=nicktime;
title 'What is the internal value for time and date';


This program gets you these 2 outputs:
First proc print output:
What is the formatted value for time and date

nicknum charval sastime olddate chardate sasdate chardt sasdt

90101 090101 9:01:01 11151950 11151950 11/15/1950 18Apr2008:09:09:01 18APR08:09:09:01


Second proc print output:

What is the internal value for time and date

nicknum charval sastime olddate chardate sasdate chardt sasdt

90101 090101 32461 11151950 11151950 -3334 18Apr2008:09:09:01 1524128941


There are a couple of very good sections in the documentation about date and time values. This one is useful:
Dates, Times, and Intervals --> About SAS Date, Time, and Datetime Values

Here are some user group papers found by Googling the string:
SAS Date Time functions

Posts: 0

Re: Time format/conversion

Thanx you very much everyone.

Cynthia, your piece was especially helpful Message was edited by: VtNick
Ask a Question
Discussion stats
  • 6 replies
  • 2 in conversation