Need to convert a datetime number into Date format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Need to convert a datetime number into Date format

I have a Date column that has been formatted as a number and the datetime is reflected as a number, eg 1753920000

 

I need to convert this to a date format, eg 20151001 or 01OCT.

 

Thanks


Accepted Solutions
Solution
‎09-21-2016 07:40 AM
Super User
Posts: 6,933

Re: Need to convert a datetime number into Date format

Since valid_end_dt1 contains valid SAS datetime values (otherwise the datetime20. format would never work), you can apply the datepart function directly to it. No need for the double conversion.

So your code would be

data VALID_DATE;
set source table;
valid_end_date_1=datepart(valid_end_dt1);
format valid_end_date_1 date5.;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
SAS Employee
Posts: 51

Re: Need to convert a datetime number into Date format

data _NULL_;
 a='22JUN2019:23:14:12'dt;
 put a=;
 put a= datetime.;
 b=datepart(a);
 put b=;
 put b= date9.;
run;
Occasional Contributor
Posts: 7

Re: Need to convert a datetime number into Date format

I solved the problem as follows:

 


data VALID_DATE;
set source table;

valid_end_date_1=datepart(input(put(valid_end_dt1,datetime20.),datetime20.));

format valid_end_date_1 date5. ;
run;

 

Results:

 

VALID_END_DT1 VALID_END_DATE_1
1753920000 31-Jul
1753920000 31-Jul

 

 

 

Solution
‎09-21-2016 07:40 AM
Super User
Posts: 6,933

Re: Need to convert a datetime number into Date format

Since valid_end_dt1 contains valid SAS datetime values (otherwise the datetime20. format would never work), you can apply the datepart function directly to it. No need for the double conversion.

So your code would be

data VALID_DATE;
set source table;
valid_end_date_1=datepart(valid_end_dt1);
format valid_end_date_1 date5.;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 7

Re: Need to convert a datetime number into Date format

Thanks Kurt, it works!
Valued Guide
Posts: 2,174

Re: Need to convert a datetime number into Date format

Rather than use datepart() function to convert the value, you could use the DTDATE. format to present just the date without any other conversion.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 497 views
  • 3 likes
  • 4 in conversation