Help using Base SAS procedures

How to decrease two min from date time field

Accepted Solution Solved
Reply
Regular Contributor
Posts: 240
Accepted Solution

How to decrease two min from date time field

hi I have a table that has a startdate/time I'm being ask to give it a 2 min buffer I need to reduce 2 min from it... Prefer as a data step if easier ....thanks

Example

ID.   Datetime     

A      05/01/15 15:25:00

what I need

id.       datetime

A.         05/01/15 15:23:00


Accepted Solutions
Solution
‎05-31-2015 10:27 PM
Respected Advisor
Posts: 4,663

Re: How to decrease two min from date time field

No need to bother with datetime internal representation, substract a time constant (thanks Jag)

data dates;

input ID$   Datetime & :anydtdtm20.;

new_date = datetime - '00:02:00't;

format datetime new_date datetime20.;

datalines;

A      05/01/15 15:25:00

;

proc print; run;

PG

PG

View solution in original post


All Replies
Valued Guide
Posts: 3,208

Re: How to decrease two min from date time field

120 seconds to substract  (see the basics of datetime).  If you prefer working on intervals you could use that also having the option of dropping time (eg non office hours). 

---->-- ja karman --<-----
Regular Contributor
Posts: 240

Re: How to decrease two min from date time field

HI

is it like this 05/30/15 18:25:00 -120 = 05/30/15 18:23:00 ?

Trusted Advisor
Posts: 1,131

Re: How to decrease two min from date time field

You could also use intnx function

data dates;

input ID$   Datetime : anydtdte20.;

new_date=intnx('second',datetime,-120,'same');

format datetime new_date datetime20.;

cards;

A      05/01/15 15:25:00

;

Thanks,

Jag

Thanks,
Jag
Solution
‎05-31-2015 10:27 PM
Respected Advisor
Posts: 4,663

Re: How to decrease two min from date time field

No need to bother with datetime internal representation, substract a time constant (thanks Jag)

data dates;

input ID$   Datetime & :anydtdtm20.;

new_date = datetime - '00:02:00't;

format datetime new_date datetime20.;

datalines;

A      05/01/15 15:25:00

;

proc print; run;

PG

PG
Valued Guide
Posts: 3,208

Re: How to decrease two min from date time field

Correct PGstats, your example is showing the usage of knowing the internal representation in a way not being aware it anymore.
It is having:

- a datetime variable being input from a string. That internal representation is a counter based on seconds. (1jan1960)

- substracting those 120 seconds as a converted string being defined as string-time constant

This will be a new valid datetime variabele wit a value as OP's wanted.


Not being aware why something is working is making it difficult to explain to the newbies.
This is having a good background not everything is having a background and can be explained or is having a reason.
    

---->-- ja karman --<-----
Super User
Posts: 6,977

Re: How to decrease two min from date time field

The secret behind it all is that SAS stores dates as simple numbers, counting the days from 01/01/1960. Similarly, datetimes (and times) are stored as numbers counting seconds.

Therefore, '00:02:00't and 120 are the same constant.

What is needed is to correctly input date/datetime/time values into a SAS dataset, using the proper informat.

As shown by

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 296 views
  • 2 likes
  • 5 in conversation