Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating time 'bucket' variable

Status
Not open for further replies.

kangaroo2

Programmer
Apr 23, 2010
2
0
0
GB
Hi,

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;

run;
 
kangaroo2,

Try using an equation like this.

Code:
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;

run;

dblan
 
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).

Code:
data yourdata; 
input Time time.;
format Time time.;
cards; 
08:00:00 
08:01:00
08:02:00
08:03:00
08:04:00
16:30:00
; 

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

dblan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top