BookmarkSubscribeRSS Feed
Malathi13
Obsidian | Level 7

Hi All,

I have a difficulty with SAS dates. I am trying to find the number of observations between two SAS datetime values. I have a dataset with a column datetime1 and column datetime2 and I want to see how many observations are within an hour in these two columns. For e.g.

 

Datetime1                              DateTime2

07/13/2017:15:04                 07/13/2017:16:04

 

I want to know how many observations are between these two datetimes. I have this statement in SAS

 

data new;

set test;

if input(Datetime1, mdyampm.) > input (Datetime2, mdyampm.) and input (datetime1, mdyampm.) < input(datetime2, mdyampm.)+3600;

run;

 

Any help is greatly appreciated!

 

M

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

What happens when you run the code? Is there an error in the LOG? (if so, please show us the log, the code AND the error messages) Or do you get the wrong answer?

 

Also, are datetime1 and datetime2 character variables or numeric variables?

--
Paige Miller
Malathi13
Obsidian | Level 7

Hi,

I'm getting an error when I run the code:

NOTE: Invalid numeric data, datetime1='9/12/2019 22:13' , at line 478 column 23.
WARNING: Limit set by ERRORS= option reached. Further errors of this type will not be printed.

 

I used this code:


DATA onehour2; SET matched2;
IF INPUT(datetime2, mdyampm.) > INPUT(datetime2, mdyampm.)
and
INPUT(datetime2, mdyampm.) < INPUT(datetime2, mdyampm.) + 3600;
RUN;

 

Datetime1 and Datetime2 are character variables

 

 

Thank you,

M

PaigeMiller
Diamond | Level 26

I specifically asked to see the LOG showing both the code and the error messages. Please provide that information.

 

And I also asked about what type of variable these datetimes are, numeric or character. Please provide that information.

--
Paige Miller

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
  • 3 replies
  • 985 views
  • 0 likes
  • 2 in conversation