# 22.5 - SAS Time Basics

22.5 - SAS Time Basics

We won't spend as much time (no pun intended!) learning how to handle times in SAS as we did learning how to handle dates, but we should still learn the basics. In this section, we'll get a quick and broad overview of the fundamental things you need to know about working with times in SAS. We'll learn how SAS defines time and datetime values, how to use an informat to read a time into a SAS data set, how to use a format to display a SAS time, how to use the most common time functions, and how to define a SAS time constant.

## The Definition of a SAS Time and Datetime

SAS stores time values similar to the way it stores date values. Specifically, SAS stores time as a numeric value equal to the number of seconds since midnight. So, for example, SAS stores:

• a 60 for 12:01 am, since it is 60 seconds after midnight
• a 95 for 12:01:35 am, since it is 95 seconds after midnight
• a 120 for 12:02 am, since it is 120 seconds after midnight
• and so on ...

Since there are 86,400 seconds in a day, a SAS time value takes on a value between 0 and 86,400. No matter how you read a time, SAS converts the time to a number as just defined.

A SAS datetime is a special value that combines both date and time values. A SAS datetime value is stored as the number of seconds between midnight on January 1, 1960, and a given date and time. Okay, I don't feel like calculating one of these datetimes out myself. I'll trust the SAS manual that I'm looking at that tells me, for example, that the SAS datetime for April 22, 1989, at 4:10:45 pm equals 92,488,384 seconds. I guess that if you need the added accuracy of working with seconds, then datetimes are for you. I personally have never needed to use them.

## Using Informats and Formats to Input and Display a SAS Time

Just as we need to tell SAS what form a date should take, we need to tell SAS what form a time should take. As you'd probably expect, we use time informats in an INPUT statement to tell SAS the form of the times to be read in. And, we use time formats in a FORMAT statement to tell SAS the form of the times to be displayed.

### Example 22.19

The following SAS program reads five observations into a SAS data set called diet. One of the variables — weight time (wt_time) — is in hh:mm:ss format, and therefore SAS is told to read the dates using the time8. informat:

DATA diet;
input subj 1-4 l_name $18-23 weight 30-32 +1 wt_date mmddyy8. @43 b_date mmddyy8. @52 wt_time time8.; wtm_fmt1 = wt_time; wtm_fmt2 = wt_time; wtm_fmt3 = wt_time; format wtm_fmt1 hhmm. wtm_fmt2 hour5.2 wtm_fmt3 time8.; DATALINES; 1024 Alice Smith 1 65 125 12/1/05 01/01/60 00:01:00 1167 Maryann White 1 68 140 12/01/05 01/01/59 00:15:00 1168 Thomas Jones 2 190 12/2/05 06/15/60 12:00:00 1201 Benedictine Arnold 2 68 190 11/30/05 12/31/60 00:00:00 1302 Felicia Ho 1 63 115 1/1/06 06/15/58 23:59:59 ; RUN; PROC PRINT data=diet; title 'The diet data set with formatted weight times'; var subj wt_time wtm_fmt1 wtm_fmt2 wtm_fmt3; RUN; The diet data set with formatted weight times Obssubjwt_timewtm_fmt1wtm_fmt2wtm_fmt3 11024600:010.020:01:00 211679000:150.250:15:00 311684320012:0012.0012:00:00 4120100:000.000:00:00 513028639924:0024.0023:59:59 First, review the program so that you understand what it is doing. Specifically, pay attention to the time8. informat used to read in the wt_time variable. Also, note that three new weight time variables — wtm_fmt1, wtm_fmt2, wtm_fmt3 — are assigned to equal the values of the wt_time variable. The three new variables are each formatted differently, however. The FORMAT statement tells SAS to format wtm_fmt1 as hhmm., wtm_fmt2 as hour5.2, and wtm_fmt3 as time8. Now, launch and run the SAS program, and review the resulting output to familiarize yourself with the contents of the diet data set. Note, in particular, the numeric values that are stored for the unformatted wt_time variable. As expected, the 00:01:00 time is stored as a 60, the 00:00:00 time is stored as a 0, and the 00:15:00 time is stored as a 900. Then, note the formatted versions of the weight time variables. As you can see, the hhmm. format displays the time on a 24-hour clock. The hour5.2 format displays the time as hours and decimal fractions of hours. And, the time8. format displays the time as hours, minutes, and seconds in the form hh:mm:ss. ## Using SAS Time Functions Just as is the case for SAS dates, the best thing about SAS times is that, because SAS time values are numeric values, you can easily sort them, subtract them, and add them. You can also compare times. Or, you can use them in any of the available time functions. The most commonly used time functions are: • time( ) returns the current time as a SAS time value • hms(h, m, s) returns a SAS time value for the given hour (h), minutes (m), and seconds (s) • hour(time) returns the hour portion of a SAS time value (time) • minute(time) returns the minute portion of a SAS time value (time) • second(time) returns the second portion of a SAS time value (time) We won't look at an example of their use here, but the interval functions intnx( ) and intck( ) that we explored on SAS dates can also be used on SAS times. ### Example 22.20 The following SAS program illustrates the use of the five time functions mentioned above. Specifically, the variable curtime is assigned the current time using the time( ) function. Then, the hour( ), minute( ), and second( ) functions are used to extract the hours, minutes, and seconds from the wt_time variable. Finally, the hms( ) function is used to put the hours, minutes, and seconds back together again to create a new variable called wt_time2 that equals the old wt_time variable: DATA diet; input subj 1-4 l_name$ 18-23 weight 30-32
+1 wt_date mmddyy8. @43 b_date mmddyy8.
@52 wt_time time8.;
curtime = time();
wt_hr = hour(wt_time);
wt_min = minute(wt_time);
wt_sec = second(wt_time);
wt_time2 = hms(wt_hr, wt_min, wt_sec);
format curtime wt_time wt_time2 time8.;
DATALINES;
1024 Alice       Smith  1 65 125 12/1/05  01/01/60 00:01:00
1167 Maryann     White  1 68 140 12/01/05 01/01/59 00:15:00
1168 Thomas      Jones  2    190 12/2/05  06/15/60 12:00:00
1201 Benedictine Arnold 2 68 190 11/30/05 12/31/60 00:00:00
1302 Felicia     Ho     1 63 115 1/1/06   06/15/58 23:59:59
;
RUN;
PROC PRINT data=diet;
title 'The diet data set with five new variables';
var subj curtime wt_time wt_hr wt_min wt_sec wt_time2;
RUN;

The diet data set with five new variables
Obssubjcurtimewt_timewt_hrwt_minwt_secwt_time2
1102413:09:070:01:000100:01:00
2116713:09:070:15:0001500:15:00
3116813:09:0712:00:00120012:00:00
4120113:09:070:00:000000:00:00
5130213:09:0723:59:5923595923:59:59

First, review the program to make sure that you understand how to use each of the five functions. Then, launch and run  the SAS program, and review the output to convince yourself that the program does as claimed.

## Comparing Times

Again, because SAS time values are numeric values, you can easily compare two or more times The comparisons are made just as the comparisons between any two numbers would take place. For example, because the time 00:10:00 is stored as a 600 in SAS, it is considered smaller than the time 00:15:00, which is stored as a 900 in SAS.

### Example 22.21

The following SAS program illustrates how to compare the values of a time variable, not to the values of some other time variable, but rather to a time constant. Specifically, the WHERE= option on the DATA statement tells SAS to output to the diet data set only those individuals whose wt_time is between midnight and noon, inclusive:

DATA diet (where = ((wt_time ge '00:00:00't)
and (wt_time le '12:00:00't)));;
input subj 1-4 l_name \$ 18-23 weight 30-32
+1 wt_date mmddyy8. @43 b_date mmddyy8.
@52 wt_time time8.;
time_int = abs((wt_time - '05:00:00't)/3600);
format wt_time time8. time_int 4.1;
DATALINES;
1024 Alice       Smith  1 65 125 12/1/05  01/01/60 00:01:00
1167 Maryann     White  1 68 140 12/01/05 01/01/59 00:15:00
1168 Thomas      Jones  2    190 12/2/05  06/15/60 12:00:00
1201 Benedictine Arnold 2 68 190 11/30/05 12/31/60 00:00:00
1302 Felicia     Ho     1 63 115 1/1/06   06/15/58 23:59:59
;
RUN;

PROC PRINT data=diet;
title 'The subsetted diet data set';
var subj l_name wt_time time_int;
RUN;

The subsetted diet data set
Obssubjl_namewt_timetime_int
11024Smith0:01:005.0
21167White0:15:004.8
31168Jones12:00:007.0
41201Arnold0:00:005.0

First, review the program to make sure you understand what it is doing. Note, for example, the form of the SAS time constants:

        '00:00:00't

and

        '12:00:00't

used in the WHERE= option. In general, a SAS time constant takes the form 'hh:mm:ss't where hh is the hour in 24-hour time, mm is the minutes, and ss (optional) are the seconds. The letter t that follows the time in single quotes tells SAS to treat the time string like a constant. Note that regardless of how you have informatted or formatted your SAS times, the SAS time constant always takes the above form.

This program also illustrates how you can use SAS time variables easily in calculations. The variable time_int is assigned the absolute difference in time, in hours, between each individual's weight time and their expected weight time, say for example, 5 a.m.

Now, launch and run  the SAS program. Then, review the resulting output to convince yourself that only those individuals whose weight time is between midnight and noon are included in the output diet data set.

Well, I think that's enough for now about processing time values in SAS. As you can see, times are treated just as dates are in SAS. Once you understand one, you understand the other. Now, let's practice what we've learned in this lesson!

 [1] Link ↥ Has Tooltip/Popover Toggleable Visibility