Help using Base SAS procedures

If And Not Working

Accepted Solution Solved
Reply
Regular Contributor
Posts: 240
Accepted Solution

If And Not Working

Hi I have a data step where I used a IF and Statement. When it runs it doesn't use the starttime and end time provided

STARTtime       EndTime                                  Day_Diff

05:32:00AM      10:45:00 AM                                  0

01/26/12             01/26/12

DATA TEST;

SET TEST;

IF  StartTime >= '20:00:00PM'T  /*Ticket open after 8PM anyday*/

   AND ENDTime <= '12:00:00PM'T  /*Ticket open after 8PM anyday*/

   AND DAY_DIFF < 1

                THEN PAY="YES";

RUN;

I don't know what I'm doing wrong. I've it formatted  TimeAMPM.

Thanks for any as.. FYI I'm looking for in Day_Diff 0 and 1


Accepted Solutions
Solution
‎01-25-2012 07:33 AM
Valued Guide
Posts: 2,177

If And Not Working

Patrick

I expect I am in agreement with you, but start with different objectives:

"Midnight" is specific and the only trouble is deciding to which "date" it belongs. Clearly for SAS, in a datetime value, midnight belongs to the following day (0 seconds into the day). The datetime value 86399 is still in 01Jan1960 and one second later  datepart(86400) returns the next day.

This is consistent with other SAS interval values which start having the sub-interval= 0 which is another way of saying datetime is "at the start of the interval".

I can live with this interpretation that SAS provides - midnight is at the beginning of a day and has time value 0 or 00:00:00. It is the addition of AM or PM to the 12th hour that I find really unhelpful --> it requires more text than the military time and doesn't clarify (for me). For clarity, I would never recommend using AM and PM with time constants. I haven't found anyone unclear about 24hour clock time.

Perhaps I should enter the modern world of 12PM as it seems I'm very much in a 'midday' minority.

...

and also pay more attention to the OP questions.

Peter

View solution in original post


All Replies
Respected Advisor
Posts: 4,173

If And Not Working

If you're using AM and PM what is the meaning of '20:00:00PM'T ?

Shouldn't this be '08:00:00PM'T ?

Regular Contributor
Posts: 240

If And Not Working

Patrick,

I've been using diffrent formats ..I must copy my last attempt ( Reason for the other format). I did try your recomendation ...same output.. It Seems like Starttime and Endtime isn't read correctly

DATA TEST;

SET TEST;

IF  StartTime >= '08:00:00PM'T  /*Ticket open after 8PM anyday*/

   AND ENDTime <= '12:00:00PM'T  /*Ticket open after 8PM anyday*/

   AND DAY_DIFF < 1

                THEN PAY="YES";

RUN;

Respected Advisor
Posts: 4,173

If And Not Working

I believe the other issue you're having is that '12:00:00PM'T is interpreted as noon and not midnight.

I suggest to use a 24 hours day and function HMS() to avoid any further problems.

DATA _null_;
  StartTime='08:00:00PM'T;
  put StartTime=;
  ENDTime = '12:00:00PM'T;
  put ENDTime=;
  ENDTime = '12:00:00AM'T;
  put ENDTime=;

  /* use hms() */
  StartTime=hms(20,0,0);
  put StartTime=;
  Endtime=hms(24,0,0);
  put EndTime=;
RUN;

Trusted Advisor
Posts: 1,301

If And Not Working

Are starttime and endtime stored as only time data or as datetime?

The data you present makes me beleive datetime:

BETO wrote:

STARTtime       EndTime                                  Day_Diff

05:32:00AM      10:45:00 AM                                  0

01/26/12             01/26/12

If so try:

DATA TEST;

SET TEST;

IF  timepart(StartTime) >= '08:00:00PM'T  /*Ticket open after 8PM anyday*/

   AND timepart(ENDTime) <= '12:00:00PM'T  /*Ticket open after 8PM anyday*/

   AND DAY_DIFF < 1

                THEN PAY="YES";

RUN;


Valued Guide
Posts: 2,177

If And Not Working

FriedEg has provided the solution.

I would add that if you are happy with times like 20:00:00 then use that style and drop the AM / PM. SAS can work out that 21:23:59PM is the same as 21:23:59.

That removes the question of what is intended by 12:00:00PM 

Sadly SAS doesn't support the classic solution NOON or MIDNIGHT in time constants not the TIME nor TIMEAMPM formats, but we can construct them with code like

proc format ;

invalue mytime (upcase default=32)

    'NOON'     = '12:0't

    'MIDNIGHT' = '00:0't

   other      = [time.]

    ;

   value mytime (default=8 )

    '12:0't  = 'NOON' 

    '00:0't  = 'MIDNIGHT'

      other  = [time8.]

    ;

run ;

data DEMO_times ;

noon = '12:0:0't ;

do time = noon-2 to noon+                  /* test NOON and AM/PM        */

       ,  noon + '0:59:58't to noon+'1:0:2't /* demo 13:00PM is 1:00PM */

       , -2 to 2                                        /* test MIDNIGHT              */

       , '0:59:58't to '1:0:2't              /* test 12:00 AM to 1:00 AM   */

      ;

   tim = timepart( time) ;                   /* avoid negative time        */

   xxx = lowcase( put( tim, mytime. )) ;     /* read lowcase time string ok*/

   chk = input( xxx, mytime. ) ;

   put tim= mytime. chk= timeAMPM. chk= mytime. ;

end ;

run ;

Respected Advisor
Posts: 4,173

Re: If And Not Working

@Peter

The question is always: Is midnight the end of a day or the beginning of a new day? Is the time value for midnight '0' or '86400'?

As much as I can see SAS time informats treat midnight as beginning of a new day (= 0)

The way the OP formulated the selection midnight needs to be "end of the day" (=86400)

HMS(24,0,0) will resolve into 86400.

The code you've posted still uses a value of '0' for midnight.

Solution
‎01-25-2012 07:33 AM
Valued Guide
Posts: 2,177

If And Not Working

Patrick

I expect I am in agreement with you, but start with different objectives:

"Midnight" is specific and the only trouble is deciding to which "date" it belongs. Clearly for SAS, in a datetime value, midnight belongs to the following day (0 seconds into the day). The datetime value 86399 is still in 01Jan1960 and one second later  datepart(86400) returns the next day.

This is consistent with other SAS interval values which start having the sub-interval= 0 which is another way of saying datetime is "at the start of the interval".

I can live with this interpretation that SAS provides - midnight is at the beginning of a day and has time value 0 or 00:00:00. It is the addition of AM or PM to the 12th hour that I find really unhelpful --> it requires more text than the military time and doesn't clarify (for me). For clarity, I would never recommend using AM and PM with time constants. I haven't found anyone unclear about 24hour clock time.

Perhaps I should enter the modern world of 12PM as it seems I'm very much in a 'midday' minority.

...

and also pay more attention to the OP questions.

Peter

Regular Contributor
Posts: 240

Re: If And Not Working

First I want to thank you guys for assisting me ... I normally reach out when I hit a wall .

startdate

starttime

date

ENDdate

ENDtime

1/5/2012

12:03:00 AM

1/5/2012

1/5/2012

10:12:00 AM

Enclose is  a snapshot of the output I don't know if its me not getting it but I tried the example code you all three provided and it work 90% of the time. On the snapshot enclose it should have been a pass. The script  looks at Starttime if it falls between 8AM-8PM and its with 4hr or less duration then its a pass that part  working fine . The second part is when  starttime is  after 8PM  and endtime 12PM next day that is consider a pass. The EndTime can be between 8PM thru 12PM next day Thats where I've been having my issue  with the If and Statement, time  since it crosses over to a new day I tried duration time of less than or EQ 12.0 but it looks at the whole  day ... Thanks again 

PROC Star
Posts: 7,468

Re: If And Not Working

In your example the end datetime is before the start datetime.  Was that a typo?

If it was, as suggested by others, if you have dates and times create a datetime variable.  That way you could change your criteria to only look for x number of seconds and not have to build extra logic to account for crossing day boundaries.

Regular Contributor
Posts: 240

If And Not Working

Thank You Guys for assiting me ... It took awhile but I was able to get what I needed based on your inputs...

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 262 views
  • 6 likes
  • 5 in conversation