Solved
Contributor
Posts: 62

# Equivalence using DATETIME

I have code that does the following sort of thing over and over:

if SUBJECT_NUMBER='300-0010' then do;
if input ('19AUG15:07:52:00', DateTime18.) <= Event_Time <= input ('20AUG15:07:30:00', DateTime18.) then AUC_Day_1 = 1;
if input ('20AUG15:07:30:00', DateTime18.) <= Event_Time <= input ('21AUG15:07:45:00', DateTime18.) then AUC_Day_2 = 1;
end;

I expect the result to look like this

Day_1   Day_2

1             .

1             .

1             .

1             1

.              1

.              1

.              1

In other words, the largest value in one interval and the smallest value in the other are the same.  So one will be 1 and the other will be missing except for when we hit that one overlapping value, at that point both Day_1 and Day_2 should be 1.

Mysteriously, sometimes I get the above but sometimes I don't and instead get only one of the other with a value of 1 at the point where the intervals overlap.

It's basically like

if   5  <=  x  <= 10 then S = 1;

if  10 <=  x  <= 20 then T = 1;

It seems to me that when x is 10, both S and T must be 1.  And yet I'm only getting it some of the time.  It's not typos on the datetimes that overlap because I'm copying them from elsewhere and then pasting them in the two places in the code.  And plus I can look at them with my eyes and see they are the same.  I am mystified.  Any help is greatly appreciated.

Accepted Solutions
Solution
‎09-25-2015 06:23 AM
Posts: 5,482

## Re: Equivalence using DATETIME

OK, then it could be that Event_Time is not always an integer. This happens to me when I import/convert dates from Excel. Try this then:

``````if SUBJECT_NUMBER='300-0010' then do;
if '19AUG15:07:52:00'DT <= round(Event_Time) <= '20AUG15:07:30:00'DT then AUC_Day_1 = 1;
if '20AUG15:07:30:00'DT <= round(Event_Time) <= '21AUG15:07:45:00'DT then AUC_Day_2 = 1;
end;``````

PG

All Replies
Posts: 5,482

## Re: Equivalence using DATETIME

If Event_Time is a SAS datetime, then you should compare it with SAS datetime constants, like this:

``````if SUBJECT_NUMBER='300-0010' then do;
if '19AUG15:07:52:00'DT <= Event_Time <= '20AUG15:07:30:00'DT then AUC_Day_1 = 1;
if '20AUG15:07:30:00'DT <= Event_Time <= '21AUG15:07:45:00'DT then AUC_Day_2 = 1;
end;``````

(untested)

PG
Contributor
Posts: 62

## Re: Equivalence using DATETIME

Thanks for the suggestion but when I try it that way I get the same result, namely some are good and some are not.  I notice that the example code I posted happened to be one of the good ones so here is one of the bad ones to balance it out, although why one is good and one is bad is a mystery to me.  I'll post it both the way I originally coded it and with the DT suggestion.

if SUBJECT_NUMBER='300-0002' then do;
if '12AUG15:08:10:00'DT <= Event_Time <= '13AUG15:07:10:00'DT then AUC_Day_1 = 1;
if '13AUG15:07:10:00'DT <= Event_Time <= '14AUG15:09:36:00'DT then AUC_Day_2 = 1;
end;

if SUBJECT_NUMBER='300-0002' then do;
if input ('12AUG15:08:10:00', DateTime18.) <= Event_Time <= input ('13AUG15:07:10:00', DateTime18.) then AUC_Day_1 = 1;
if input ('13AUG15:07:10:00', DateTime18.) <= Event_Time <= input ('14AUG15:09:36:00', DateTime18.) then AUC_Day_2 = 1;
end;

Solution
‎09-25-2015 06:23 AM
Posts: 5,482

## Re: Equivalence using DATETIME

OK, then it could be that Event_Time is not always an integer. This happens to me when I import/convert dates from Excel. Try this then:

``````if SUBJECT_NUMBER='300-0010' then do;
if '19AUG15:07:52:00'DT <= round(Event_Time) <= '20AUG15:07:30:00'DT then AUC_Day_1 = 1;
if '20AUG15:07:30:00'DT <= round(Event_Time) <= '21AUG15:07:45:00'DT then AUC_Day_2 = 1;
end;``````

PG
Contributor
Posts: 62

## Re: Equivalence using DATETIME

Beautiful!  That works perfectly.  Thanks a lot.

🔒 This topic is solved and locked.