BookmarkSubscribeRSS Feed
Eugenio211
Quartz | Level 8

Hi,

how to extract date from this type of date

' 2018-08-26 17:51:30.8770000 +00:00'?

 

Thank you.

5 REPLIES 5
PaigeMiller
Diamond | Level 26

If this is a numeric value in a numeric variable, what you are showing (without the quotes) is a date/time value, and so the DATEPART function extracts the date.

 

If it is character ... well ... it shouldn't be. Date values and date/time values should always be numeric.

 

What does PROC CONTENTS show for this variable?

--
Paige Miller
Eugenio211
Quartz | Level 8
this is what I get when using datepart:
ERROR: Function DATEPART requires a numeric expression as argument 1.
ERROR: Character expression requires a character format.
Eugenio211
Quartz | Level 8
this is my step:
proc sql;
create table PaidAhead as
select l.CUSTOMER_KEY,l.LOAN_KEY,l.DATE_ENTERED
,datepart(PAYOFF_DATE) as PAID_OFF_DATE format date9.
,l.product_type
from DataSet1 L
join DataSet2 d on l.LOAN_KEY = d.LOAN_KEY
;quit;
PaigeMiller
Diamond | Level 26

Obvi you have a character variable that looks like a date/time; but it clearly is not a date/time since date/time values cannot be character.

 

You have to convert this to numeric, with INPUT function and the proper informat.

 

data want;
    datetime=' 2018-08-26 17:51:30.8770000 +00:00';
    datetime_numeric=input(datetime, anydtdtm.);
    date=datepart(datetime_numeric);
    format date date9.;
run;

 

Please do yourself a favor and do not use character strings for dates, times and datetimes. These variables should be NUMERIC, always, 100% of the time, no exceptions.

 

 

 

--
Paige Miller
Astounding
PROC Star

Perhaps a little simpler:

data want;
   set have;
   date = input(big_long_datetime_string, : yymmdd10.);
   drop big_long_datetime_string;
   formate date yymmdd10.;
run;

This gets you the DATE as a proper, numeric SAS date, while getting rid of the time part.  If memory serves, however, you will need to use a DATA step because SQL will not allow the INPUT function.  (Unless times have changed, but I doubt it.)

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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