Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Creating time 'bucket' variable

Creating time 'bucket' variable

Creating time 'bucket' variable


I'm a SAS newbie. I have a time variable (TIME) that is in milliseconds but I want to create a new variable (PERIOD) that tells me which minute during the day the observation falls within, while keeping all the original observations. The data runs from 8AM to 4.30PM.

For example, I would like between 08:00:00 and 08:01:00 to be Period = 1 and between 16:29:00 and 16:30:00 to be Period = 510.

The starting few lines of my current solution is below and as you can understand, this continues for another 500 odd lines. Can anyone suggest a more sophisticated solution?

data work.x; set work.x;

    if TIME >= '08:00:00' and TIME < '08:01:00' then PERIOD = 1;
    else if TIME >= '08:01:00' and TIME < '08:02:00' then PERIOD = 2;
    else if TIME >= '08:02:00' and TIME < '08:03:00' then PERIOD = 3;
    else if TIME >= '08:03:00' and TIME < '08:04:00' then PERIOD = 4;
    else if TIME >= '08:04:00' and TIME < '08:05:00' then PERIOD = 5;
    else if TIME >= '08:05:00' and TIME < '08:06:00' then PERIOD = 6;
    else if TIME >= '08:06:00' and TIME < '08:07:00' then PERIOD = 7;
    else if TIME >= '08:07:00' and TIME < '08:08:00' then PERIOD = 8;
    else if TIME >= '08:08:00' and TIME < '08:09:00' then PERIOD = 9;
    else if TIME >= '08:09:00' and TIME < '08:10:00' then PERIOD = 10;
    else if TIME >= '08:10:00' and TIME < '08:11:00' then PERIOD = 11;

... etc.

    else if TIME >= '16:29:00' and TIME < '16:30:00' then PERIOD = 510;
    else PERIOD = 511;


RE: Creating time 'bucket' variable


Try using an equation like this.  


data test;
format x y z time.;
x = '08:00:00't;
y = '08:01:00't;
z = '08:02:00't;

period1 = (x-(x-60))/60;
period2 = (y-(x-60))/60;
period3 = (z-(x-60))/60;



RE: Creating time 'bucket' variable

After thinking about it for another minute, since you data is probably in a column format, something like this can work. (Also, I adjusted the formula a little bit.  Took out the "-60" to get the periods to count like you wanted them to).


data yourdata;
input Time time.;
format Time time.;

data yourdata;
set yourdata;
period = (time-('08:00:00't))/60;


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close