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.pngRodrigue_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-wordmark-gradient-background 3.pngSAS Innovate

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 3001 views
  • 3 likes
  • 4 in conversation