Hi,
I have a variable (Date_In) which has the date and time (2016-05-21 10:06:08).
How can I remove the time from the variable and format the date as DDMMYYYY?
Thanks
You have a datetime variable (count of seconds from 1960-01-01T00:00:00). To extract the date value (count of days from 1960-01-01), use the datepart() function.
Use the DATEPART() function.
data example;
datetimevar='01NOV19:00:00:00'dt;
datevar=datepart(datetimevar);
format datevar ddmmyy10.;
run;
Use Datepart to extract date value from datetime value if your variable is numeric and format the variable with any date format you want
Use the DATEPART Function and an appropriate format
data _null_;
datetime='2016-05-21 10:06:08'dt;
date=datepart(datetime);
put date= ddmmyyn8.;
run;
Since the value you show is not a typical SAS format, I am going to ask is that variable character or actually a SAS datetime value with a custom format.
First, extract the date part. you can do it either by:
using the DATEPART function or
dividing DATE_IN by 86400 (the number of seconds in a day)
Second, format the result as DDMMYYn10., so that SAS would print/display it the way you want.
For example:
data _null_ ;
date_in = '11aug2018:12:34:56'dt ;
d1 = datepart (date_in) ;
d2 = divide (date_in, 86400) ;
format d1 d2 ddmmyyn8. ;
put d1= / d2= ;
run ;
Result in the log:
d1=11082018 d2=11082018
Kind regards
Paul D.
data new;
set old;
new_date=datepart(old_date);
informat new_date ddmmyy10.;
run;
@Jasminalves wrote:
data new;
set old;
new_date=datepart(old_date);
informat new_date ddmmyy10.;
run;
Attaching an INFORMAT to a variable after the values have already been stored in the variable does nothing. If you want to impact how the value are displayed you need to attach a FORMAT to the variable. Also if you display dates in either DMY or MDY order it will confuse half your audience. Either use DATE or YYMMDD format to display dates and avoid the confusion.
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!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.