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

Hi ! Please I need you help+++

I have a time variable HH:MM called "timestop" and I would like to create a new categorical variable "timestopnight" stating 1 if the timestop was between 23:00 and 7:00 and 0 if the timestop was between 07:00 and 23:00.

I have tried several codes like the following but none worked.

 

data dataset ; set dataset;

if timestop < '07:00't then timestopnight=1;

if timestop > '23:00't then timestopnight=1;

if  '07:00't =<timestop =< '23:00't then timestopnight=0;

run;

 

 

Thanks a lot for your help 🙏

 

Rodrigue

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Please post the complete log from that step.

If timestop is a SAS time, this will work:

timestopnight = ifn('07:00't le timestop le '23:00't,0,1);

Be observant if timestop is a datetime instead of just time.

View solution in original post

6 REPLIES 6
tarheel13
Rhodochrosite | Level 12

I think you need to use if else if.

tarheel13
Rhodochrosite | Level 12

Can also do a boolean. 

timestopnight=07:00't le timestop le 23:00't;

 

This will assign 1 if the condition is true and 0 if it's false.

Kurt_Bremser
Super User

Please post the complete log from that step.

If timestop is a SAS time, this will work:

timestopnight = ifn('07:00't le timestop le '23:00't,0,1);

Be observant if timestop is a datetime instead of just time.

Rodrigue
Obsidian | Level 7

Thank you very much Kurt. It seems to work perfectly well for subjects who have a time in the timestop variable.

The only problem is that I have a 1 in the timestopnight column in patients who don't have value for the timestop variable.

I've tried to insert  before but it didn't work.

timestopnight = .;

Could you help me with that ?

 

I have attached the log but the variable names are different since I've tried to simplify for the post.

Thanks again for your help+++

Rodrigue

Rodrigue_0-1624282770076.png

 

Rodrigue
Obsidian | Level 7

I found the solution adding 

if not missing (timestop) 
ballardw
Super User

MISSING values are always less than any actual value.

Always.

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
  • 6 replies
  • 782 views
  • 3 likes
  • 4 in conversation