Hi,
how to extract date from this type of date
' 2018-08-26 17:51:30.8770000 +00:00'?
Thank you.
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?
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.
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.)
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.