## How to decrease two min from date time field

Solved
Regular Contributor
Posts: 240

# 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
Posts: 5,539

## 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

All Replies
Posts: 3,215

## 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 ?

Posts: 1,147

## 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
Posts: 5,539

## 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
Posts: 3,215

## 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: 10,278

## 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
How to convert datasets to data steps
How to post code
🔒 This topic is solved and locked.