How can SAS make calculations with time variables?

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

How can SAS make calculations with time variables?

Dear all,

I have a dataset (sampel attached) which contains a time and a date variable.

I would like to create a new variable that searches time variable and locates those records which are within a specific interval, lets say between 4pm and 4.30:30 (seconds) pm

Something like the below does not work.Any other ideas?

data lib.fixingtime;

set lib.fixing;
if time>'16:00:00' and time<'16:30:30' then test=1 ;

else test=0;

run;

Hope you are able to help!

Thanks

Neo


Accepted Solutions
Solution
‎07-04-2014 06:36 AM
Contributor
Posts: 29

Re: How can SAS make calculations with time variables?

Yes your code is fine you need to attach t at the end while using time values. hope this is what ur expecting.

data final;

set new;

if '16:30:30't =>time=> '16:00:00't then test= 1;

else test=0;

run;

Sudeer

View solution in original post


All Replies
Super User
Super User
Posts: 7,942

Re: How can SAS make calculations with time variables?

Hi,

Not sure which variables you are using in that, or what format they are, but assuming that TIME column is in TIME5. format:

data want;

     set have;

     where '16:00't <= time <= '16:30't;

run;

If you want to use the date/time one then:

data want;

     set have;

     where '16:00't <= timepart(dt) <= '16:30't;

run;

If they are not in time format then input them:

data want;

     set have;

     where '16:00't <= input(time,time8.) <= '16:30't;

run;

Contributor
Posts: 57

Re: How can SAS make calculations with time variables?

RW9,

I would like to retain the old dataset and add only a new dummy variable (e.g test) equalling 1 if the records are within the mentioned time. Will that work with an 'if then' statement or do i need something else:

data want;

set have;

if   '16:30:30' =>time=>'16:00:00' then test=1;

else test=0;

run;

I think using the where statement will only create a new dataset containing those time records...?

Thanks

Neo

Solution
‎07-04-2014 06:36 AM
Contributor
Posts: 29

Re: How can SAS make calculations with time variables?

Yes your code is fine you need to attach t at the end while using time values. hope this is what ur expecting.

data final;

set new;

if '16:30:30't =>time=> '16:00:00't then test= 1;

else test=0;

run;

Sudeer

Super User
Super User
Posts: 7,942

Re: How can SAS make calculations with time variables?

Yes, Chrishi is correct.  The suffix t indicates that SAS will read the preceding string in and convert it to time (which is actualy just a number).  Then your time variable which is already a number is then checked against that conversion.  The same can be done with d, which is for date data, e.g. '01JAN2014'd <= date <= '02JAN2014'd.  Also, maybe have a look at the interval function - INTCK as that might help for more complicated time windows.

Contributor
Posts: 57

Re: How can SAS make calculations with time variables?

thanks to both of you, this was very helpful

Neo

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 350 views
  • 6 likes
  • 3 in conversation