Hello,
I am trying to re-categorize a time variable so that I can query how many the frequency of use of a medical device for each hour of the day.
I am trying to figure out how to put the following code in an array format so that I do not have to type this out 24 times (for each of the 24 hours of the day), but I am still pretty new to using arrays so I have not been able to figure it out.
data v2; set v1;
select;
when ("00:00:00"t <= start_time < "1:00:00"t) hour=1;
when ("01:00:00"t <= start_time< "2:00:00"t) hour=2;
/*[... etc until hour=24]*/
end;
run;
Is there an easier way to do this?
No arrays needed here.
Use the HOUR. format, and so the time of 08:43 will be converted to 8 and the time of 19:07 will be converted to 19 and so on. Almost every SAS analysis procedure will work with the formatted value rather than the original unformatted value.
Another thought is to simply take the time and using mathematics, turn it into an integer. Since time is measured in seconds after midnight, this code will do what you wan
newtime=floor(time/3600);
No arrays needed here.
Use the HOUR. format, and so the time of 08:43 will be converted to 8 and the time of 19:07 will be converted to 19 and so on. Almost every SAS analysis procedure will work with the formatted value rather than the original unformatted value.
Another thought is to simply take the time and using mathematics, turn it into an integer. Since time is measured in seconds after midnight, this code will do what you wan
newtime=floor(time/3600);
That works - thanks!
No need to use "magic" numbers in the code. You can use a time literal to figure how many seconds are in an hour.
newtime=floor(time/'01:00't);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.