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

Hi all,

 

I have longitudinal data (1 obs per person per minute) with two time variables: date and time. I would like to create summary variables between 7pm on one day and 12 pm on the next day. How can I create this time interval using these two variables???

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

On second thought, not sure it matters either. Try this:

 

data want;
set have;

if hour(timepart(datetime)) >= 19 or hour(timepart(datetime)) <= 12 then interval='Night';
else interval = 'Day';

run;

View solution in original post

10 REPLIES 10
Reeza
Super User
Show an example of your data please.
Instructions on how to do so are here. I would probably recommend a custom format.
https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...
lalaktgrau
Fluorite | Level 6

I combined date and time into one variable. I'd still only like to count people between certain time intervals.

 

data WORK.CLASS;
infile datalines truncover;
input Name:$8. sleep datetime;
informat datetime datetime20.;
format datetime datetime20.;
datalines;
Alfred 0 04MAR14:23:55:00
Alfred 0 04MAR14:23:56:00
Alfred 1 04MAR14:23:57:00
Alfred 1 04MAR14:23:58:00
Alfred 1 04MAR14:23:59:00
Alfred 1 04MAR14:00:00:00
Alfred 1 04MAR14:01:01:00
Alfred 1 04MAR14:01:02:00
Alfred 1 04MAR14:01:03:00
Alfred 1 04MAR14:01:04:00

mary 0 04MAR14:23:55:00
mary 0 04MAR14:23:56:00
mary 1 04MAR14:23:57:00
mary 1 04MAR14:23:58:00
mary 1 04MAR14:23:59:00
mary 1 04MAR14:00:00:00
mary 1 04MAR14:01:01:00
mary 1 04MAR14:01:02:00
mary 1 04MAR14:01:03:00
mary 1 04MAR14:01:04:00
;

Reeza
Super User
Are your date intervals all over the place or do they have an upper and lower limit?
lalaktgrau
Fluorite | Level 6

I have one week per person, every single minute!

Reeza
Super User

I understand that, but is there a starting date and end date that you know, ie the data is all between 2014 and 2018?

 


@lalaktgrau wrote:

I have one week per person, every single minute!


 

lalaktgrau
Fluorite | Level 6

No! It's all over the place. Different people had different starts and ends. I just want to pull all the nighttime data, so I think we can disregard the dates (except to know start of night one day, morning the next day 7pm-12pm).

Reeza
Super User
You can't disregard the dates, since you need to account for the time, unless you're absolutely positive you have a record for every single minute. In that case you could just do a simple IF/THEN comparison. But from the first to last date you would have no breaks.
Reeza
Super User
I can't test with your sample data because there's no day, it's all night.
Reeza
Super User

On second thought, not sure it matters either. Try this:

 

data want;
set have;

if hour(timepart(datetime)) >= 19 or hour(timepart(datetime)) <= 12 then interval='Night';
else interval = 'Day';

run;
shahparth260
Quartz | Level 8

Hello, can you explicitly specify your output ? if possible. 

  • what is meaning for 0 and 1 ? 
  • is it yes or no ?
PS

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 1728 views
  • 0 likes
  • 3 in conversation