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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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