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

 

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

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
n6
Quartz | Level 8 n6
Quartz | Level 8

 

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;

PGStats
Opal | Level 21

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
n6
Quartz | Level 8 n6
Quartz | Level 8

 

Beautiful!  That works perfectly.  Thanks a lot.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1699 views
  • 1 like
  • 2 in conversation