SAS: How to Display Values in Time Format

Suppose you have a variable in SAS called duration that has a time value of 7:30:00.

You can use the following functions in SAS to format this time value in different ways:

  • PUT(duration, time8.) – Format as time with a total length of 8.
    • This will produce 7:30:00.
  • PUT(duration, hhmm.) – Format as time with only hours and minutes.
    • This will produce 7:30.
  • PUT(duration, hours5.2) – Format as time with decimal hours.
    • This will produce 7.50.
  • hour(duration) – Format as hours only.
    • This will produce 7
  • minute(duration) – Format as minute only.
    • This will produce 30.
  • second(duration) – Format as seconds only.
    • This will produce 0.

The following example shows how to use each of these formats in practice.

Example: Display Values in Time Formats in SAS

Suppose we have the following dataset in SAS that contains information about the duration that it took various athletes to finish some task:

/*create dataset*/
data my_data;
    input athlete $ duration time8.;
A 04:15:00
B 10:09:15
C 7:30:00
D 18:55:00
E 14:23:59
F 23:45:10

/*view dataset*/
proc print data=my_data;

By default, SAS stores time values as seconds.

For example, in the first line we entered 4 hours, 15 minutes and 0 seconds, which is equivalent to 15,300 seconds.

Note: There are 86,400 seconds in one day.

We can use the following syntax to create a new dataset in which we format the values in the duration column in different time formats:

/*create new dataset with duration printed in various time formats*/
data new_data;
    set my_data;
    duration_time8 = put(duration, time8.);
    duration_hhmm = put(duration, hhmm.);
    duration_hour52 = put(duration, hour5.2);
    duration_hour = hour(duration);
    duration_minute = minute(duration);
    duration_second = second(duration);

/*view new dataset*/
proc print data=new_data;

Notice that each of the new columns format the time values in a different way:

  • duration_time8 displays hours, minutes and seconds.
  • duration_hhmm displays only hours and minutes.
  • duration_hhmm displays decimal hours.
  • duration_hour displays only the hour value.
  • duration_minute displays only the minute value.
  • duration_second displays only the seconds value.

Feel free to use whichever format you’d like based on how you would like to display time values in your dataset.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

SAS: How to Display Values in Dollar Format
SAS: How to Display Values in Percent Format
SAS: How to Format Date Values in PROC SQL

Leave a Reply

Your email address will not be published. Required fields are marked *