BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
emaguin
Quartz | Level 8

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
Reeza
Super User

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

 

 

 

 

 

 

 

 

 


 

View solution in original post

2 REPLIES 2
Reeza
Super User

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/...

Reeza
Super User

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

 

 

 

 

 

 

 

 

 


 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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