BookmarkSubscribeRSS Feed
gstover
Fluorite | Level 6

I am trying to figure out the syntax to subset a dataset based on the date and time.  First to grab all records that are prior to 12:01 a.m.  Then within these records query another date variable for the date and time between 12:01 am and 4:00 am.

1 REPLY 1
Reeza
Super User

Assuming you have SAS dates/times and your variables are times it's fairly straightforward:

 

where timeVariable < '00:01:00't and  timepart(secondDateTimeVariable) between '00:01:00't and '04:00;00't;

Key concepts:

  • Use TIMEPART() to get the time portion of a datetime variable
  • Specify time as 'HH:MM:SS't
  • FYI - If you use a WHERE statement you can use BETWEEN. If you use an IF statement, then you cannot use BETWEEN and need to structure that condition more similar to the first condition.

@gstover wrote:

I am trying to figure out the syntax to subset a dataset based on the date and time.  First to grab all records that are prior to 12:01 a.m.  Then within these records query another date variable for the date and time between 12:01 am and 4:00 am.


 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1 reply
  • 428 views
  • 0 likes
  • 2 in conversation