Quartz | Level 8

## date and time computations

I need to compute a date-time variable given the number of seconds (dstarttim, below) from 01.01.1960 (I think, but let's say it's true) and then do a comparison of that date with another date. Fortunately Cody has a chapter on date arithmetic in his book but unfortunately it doesn't include time. But from it i got the intnx function.

I wrote

``````ivuedone=intx('second','01jan60:00:00:00'dt,dstarttim,'same');

put ivuedone datetime18;``````

I get errors: iwuedone in line 1 is marked as an error and line 2 likewise not liked. I'm doing something wrong; what is it?

That's part A, now part B. I want to say

``if (iwuedone lt '15mar2020' d9) then precovid=1;``

Will that work?

Alternatively, how would this be written:

``If (dstarttime le 15mar2020 in seconds) then precovid=1;``

Thanks,

Gene Maguin

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: date and time computations

1. There is no need to use INTNX to create a datetime out of the dstarttim, it's already a DATETIME because that's how SAS stores datetimes. Read the reference from the prior link for more details. Apply a format to the variable to have it display correctly.

``format dstarttim datetime22.;``

2. Specify a data constant in the format of "DDMONYYYY"d - no 9.  Since your variable is a datetime and you're comparing it to a date, you need to use DATEPART to get just the date portion as well.

``if (datepart(dtstarttim) lt '15mar2020'd) then precovid=1;``

3. If you want a seconds comparison, both need to have a time component.

``if (dstarttim lt  dhms('15mar2020'd, 0, 0, 0) ) then precovid=1;``

@emaguin wrote:

I need to compute a date-time variable given the number of seconds (dstarttim, below) from 01.01.1960 (I think, but let's say it's true) and then do a comparison of that date with another date. Fortunately Cody has a chapter on date arithmetic in his book but unfortunately it doesn't include time. But from it i got the intnx function.

I wrote

``````ivuedone=intx('second','01jan60:00:00:00'dt,dstarttim,'same');

put ivuedone datetime18;``````

I get errors: iwuedone in line 1 is marked as an error and line 2 likewise not liked. I'm doing something wrong; what is it?

That's part A, now part B. I want to say

``if (iwuedone lt '15mar2020' d9) then precovid=1;``

Will that work?

Alternatively, how would this be written:

``If (dstarttime le 15mar2020 in seconds) then precovid=1;``

Thanks,

Gene Maguin

2 REPLIES 2
Super User

## Re: date and time computations

FYI - I've formatted your question to make it more legible. Please try and use the code blocks in future posts and space out your text.

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

Super User

## Re: date and time computations

1. There is no need to use INTNX to create a datetime out of the dstarttim, it's already a DATETIME because that's how SAS stores datetimes. Read the reference from the prior link for more details. Apply a format to the variable to have it display correctly.

``format dstarttim datetime22.;``

2. Specify a data constant in the format of "DDMONYYYY"d - no 9.  Since your variable is a datetime and you're comparing it to a date, you need to use DATEPART to get just the date portion as well.

``if (datepart(dtstarttim) lt '15mar2020'd) then precovid=1;``

3. If you want a seconds comparison, both need to have a time component.

``if (dstarttim lt  dhms('15mar2020'd, 0, 0, 0) ) then precovid=1;``

@emaguin wrote:

I need to compute a date-time variable given the number of seconds (dstarttim, below) from 01.01.1960 (I think, but let's say it's true) and then do a comparison of that date with another date. Fortunately Cody has a chapter on date arithmetic in his book but unfortunately it doesn't include time. But from it i got the intnx function.

I wrote

``````ivuedone=intx('second','01jan60:00:00:00'dt,dstarttim,'same');

put ivuedone datetime18;``````

I get errors: iwuedone in line 1 is marked as an error and line 2 likewise not liked. I'm doing something wrong; what is it?

That's part A, now part B. I want to say

``if (iwuedone lt '15mar2020' d9) then precovid=1;``

Will that work?

Alternatively, how would this be written:

``If (dstarttime le 15mar2020 in seconds) then precovid=1;``

Thanks,

Gene Maguin

Discussion stats
• 2 replies
• 379 views
• 1 like
• 2 in conversation