BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fordcr2
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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);
--
Paige Miller

View solution in original post

4 REPLIES 4
Reeza
Super User
I suspect you're trying to scaffold a data set, ie see how many people are active at each hour of the day given the start and end time? If so, yes, there are easier ways. If you post some example data and expected output someone can help you further with this issue.

Feel free to make up data if needed.
PaigeMiller
Diamond | Level 26

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);
--
Paige Miller
fordcr2
Obsidian | Level 7

That works - thanks! 

Tom
Super User Tom
Super User

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);

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 513 views
  • 0 likes
  • 4 in conversation