BookmarkSubscribeRSS Feed
KC_16
Fluorite | Level 6

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

 

8 REPLIES 8
Kurt_Bremser
Super User

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.

PaigeMiller
Diamond | Level 26

Use the DATEPART() function.

 

data example;
    datetimevar='01NOV19:00:00:00'dt;
    datevar=datepart(datetimevar);
    format datevar ddmmyy10.;
run;
--
Paige Miller
novinosrin
Tourmaline | Level 20

Use Datepart to extract date value from datetime value if  your variable is numeric and format the variable with any date format you want

PeterClemmensen
Tourmaline | Level 20

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;

 

ballardw
Super User

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.

hashman
Ammonite | Level 13

@KC_16:

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.

Jasminalves
Calcite | Level 5

data new;

set old;

new_date=datepart(old_date);

informat new_date ddmmyy10.;

run;

Tom
Super User Tom
Super User

@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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 20039 views
  • 1 like
  • 9 in conversation