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

How do I extract the date and compare these 2 dates?

 

9/14/2020

2020-09-14 12:02:52

 

I've tried the following unsuccessfully to extract the date only

date_new = datepart(dt_var);
format date_new mmddyy10.;

proc print;
where date_new ^= dt_var;
run;

Thanks,

Margaret

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@urban58 wrote:
Hi ballardw



Examdate is
Num
Length 8
MMDDYY10

But visitdatetime is


Char
Length 500
$500.


I already tried

New_date= datepart(visitdatetime) but I get a bunch of error messages like this

Invalid numeric data, vistdatetime='2020-09-14 12:02:52' , at line 1278 column 21



Really appreciate your help!

Margaret

That means that your variable VISTdatetime is not a datatime value. It is character. So the first thing is to either convert the string into a datetime value in the comparson or create a new variable with the datetime (if you need it as such later).

Here is an example of creating an actual datetime value from the string.

data example;
  x ='       2020-09-14 12:02:52';
  y = input(strip(x),e8601dt.);
  format y datetime.;
run;

The Strip function is included because you character variable is 500 (!) characters long and strip will remove leading and trailing spaces if present before trying to convert the value. The E8601dt informat will read values as you show. The format statement is to display the new value with a different display format so you can see that the converted value is treated as needed.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

This needs further explanation.

 

Do you mean that you have both dates and datetime values in the same column (as your small example implies)? Or are they different variables (if so, what are the variable names)?

--
Paige Miller
urban58
Quartz | Level 8
different variables
ballardw
Super User

Since your example extracts the datepart from a datetime variable then you have change the units and as such will almost always be less than the datetime value (at least for dates and date times since 1960).

 

Typically the comparison between a date and datetime would be more

 

datevar le datepart(datetimevar)

 

or similar. But that is pretty meaningless if the datevar is created from datetimevar as you show as that would always be equal.

urban58
Quartz | Level 8
but there are many dates and datetimes that are not, e.g.
examdate visitdatetime
9/14/2020 2020-09-15 12:02:52
9/02/2020 2020-08-15 12:12:52
ballardw
Super User

@urban58 wrote:
but there are many dates and datetimes that are not, e.g.
examdate visitdatetime
9/14/2020 2020-09-15 12:02:52
9/02/2020 2020-08-15 12:12:52

Are not what?

The only example you provided earlier had the date created from the datetime value.

So the comparison would involve

examdate <comparison operator> datepart(visitdatetime)

 

urban58
Quartz | Level 8
Hi ballardw



Examdate is
Num
Length 8
MMDDYY10

But visitdatetime is


Char
Length 500
$500.


I already tried

New_date= datepart(visitdatetime) but I get a bunch of error messages like this

Invalid numeric data, vistdatetime='2020-09-14 12:02:52' , at line 1278 column 21



Really appreciate your help!

Margaret
ballardw
Super User

@urban58 wrote:
Hi ballardw



Examdate is
Num
Length 8
MMDDYY10

But visitdatetime is


Char
Length 500
$500.


I already tried

New_date= datepart(visitdatetime) but I get a bunch of error messages like this

Invalid numeric data, vistdatetime='2020-09-14 12:02:52' , at line 1278 column 21



Really appreciate your help!

Margaret

That means that your variable VISTdatetime is not a datatime value. It is character. So the first thing is to either convert the string into a datetime value in the comparson or create a new variable with the datetime (if you need it as such later).

Here is an example of creating an actual datetime value from the string.

data example;
  x ='       2020-09-14 12:02:52';
  y = input(strip(x),e8601dt.);
  format y datetime.;
run;

The Strip function is included because you character variable is 500 (!) characters long and strip will remove leading and trailing spaces if present before trying to convert the value. The E8601dt informat will read values as you show. The format statement is to display the new value with a different display format so you can see that the converted value is treated as needed.

urban58
Quartz | Level 8
Thank you so much for the explanation as well as the code to convert it to a datetime variable, you wouldn't believe how long I worked on it.
Margaret

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1896 views
  • 0 likes
  • 3 in conversation