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
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.
I think you need to use if else if.
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.
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.
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
I found the solution adding
if not missing (timestop)
MISSING values are always less than any actual value.
Always.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.