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

You need to provide more information, specifically the code you ran, the log and what isn't working.

 

What part of this is unclear?

 

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Reeza
Super User
Code can be wrong via syntax, which the log picks up, or wrong as in logically wrong, where it doesn't do what you think it's doing or want it to do. I'm guessing you're in the latter but without the code we can't help you any further. It's your choice on how to proceed.
lydiawawa
Lapis Lazuli | Level 10
proc format;
value timerng
"06:00"t -< "15:59"t= "6:00am - 3:59pm: Daytime"
"16:00"t -< "21:59"t= "4:00pm - 9:59pm: Evening"
"22:00"t -< "23:59"t, "00:00"t -< "05:59"t, = "10:00pm - 5:59am: Overnight";
run;

I added "<"  next to all the slashes, but the same problem persisted. 

Reeza
Super User

You never fixed the intervals. See my earlier response, which may not have been that clear, also below. Notice that ending goes to 00, not to 59. When it goes to 59, values such as 12:59.45 don't get grouped anywhere because it's not included in any of your ranges. So you need to change the ends to be the same as the beginning of the next interval AND use the exclude option to exclude it. Or put them the same and let SAS figure it out, I think the default is what you want. See the section in red below. 

 

Re: Format time in grouped blocks

 
That's because you have seconds so 9:59.45 seconds will not be in those intervals. Just put them at the start of the next interval and SAS will sort it out, or use the -< explicitly exclude and include your endpoints.

"06:00"t - < "16:00"t = ......

http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#n03qskwoints2an1ispy5...
ScottBass
Rhodochrosite | Level 12

Review this and tell us which observation numbers are incorrect:

 

data have;
   do time="00:00:00"t to "23:59:59"t;
      output;
   end;
   format time time.;
run;

proc format;
   value timerng
      "06:00:00"t -< "16:00:00"t  = "6:00am - 3:59pm: Daytime"
      "16:00:00"t -< "22:00:00"t  = "4:00pm - 9:59pm: Evening"
      "22:00:00"t -  "23:59:59"t, 
      "00:00:00"t -< "06:00:00"t  = "10:00pm - 5:59am: Overnight";
run;

data want;
   set have;
   time2=put(time,timerng.);
run;

Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
lydiawawa
Lapis Lazuli | Level 10
I will let you know tomorrow. You guys are awesome!
lydiawawa
Lapis Lazuli | Level 10
Thank you for the explanation, the intervals defined by your code fixed the problem. The clarification from @Reeza is also very helpful.. Can I accept both answers?
Reeza
Super User
I definitely don't need any more answers, feel free to mark Scott's answer as correct 🙂
ScottBass
Rhodochrosite | Level 12

@Reeza wrote:
I definitely don't need any more answers, feel free to mark Scott's answer as correct 🙂

 

Yeah @Rezza 's got 28K+ posts, I've got 653 🙂  Throw me a bone!

 

@lydiawawa , glad your problem is solved.  I revisited your original post:

 

I have a time variable in the format of datetime32.4(ex: 21MAR2019:10:19:15.2970

 

Beware of fractional seconds falling through the "gaps" in the format.  You may need to truncate the fractional seconds from your data then apply the format, or else expand the format ranges to include the granularity of your source data.

 

Edit:  Actually, the only gap is for 23:59:59, since all the other end points use the -< operator.  If you're worried about fractional seconds, perhaps change "23:59:59"t to "23:59:59.999999"t (and test!)

 


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 23 replies
  • 1873 views
  • 13 likes
  • 4 in conversation