BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aminkarimid
Lapis Lazuli | Level 10

Hello everybody,

I want to convert datetime variable (ex. 24MAR2008:00:00:00) to date variable (ex. 24MAR2008). I show attributes of datetime variable below:

Name & Label: TRD_EVENT_DT

Format & Informat: DATETIME19.

Type: Num.

 

I do not want to create new variable and just want to change current datetime var. to date var. format with same name.

 

How can I do that?

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

To change existing variable:

data want;
   set have;
   trd_event_dt = datepart(trd_event_dt);
   format trd_event_dt date9.;
run;

Note if you use

 

Data Have;

   set have;

 

to implement this code DO NOT EVER RUN THAT DATASTEP AGAIN. The second pass would attempt to tread the date as a datetime and result will likely be all of your dates as 01JAN1960. You would have to go back to a previous step to recreate your "have" data set prior to the datepart

data example;
  x= '31Dec2017:12:35:27'dt;
  put x= datetime20.;
  x = datepart(x);
  put 'first datepart ' x= date9.;
  x= datepart(x);
  put 'second datepart ' x= date9.;
run;

View solution in original post

2 REPLIES 2
Jagadishkatam
Amethyst | Level 16
data want;
datetime='24MAR2008:00:00:00'dt;
date=datepart(datetime);
format date date9. datetime datetime19.;
run;
Thanks,
Jag
ballardw
Super User

To change existing variable:

data want;
   set have;
   trd_event_dt = datepart(trd_event_dt);
   format trd_event_dt date9.;
run;

Note if you use

 

Data Have;

   set have;

 

to implement this code DO NOT EVER RUN THAT DATASTEP AGAIN. The second pass would attempt to tread the date as a datetime and result will likely be all of your dates as 01JAN1960. You would have to go back to a previous step to recreate your "have" data set prior to the datepart

data example;
  x= '31Dec2017:12:35:27'dt;
  put x= datetime20.;
  x = datepart(x);
  put 'first datepart ' x= date9.;
  x= datepart(x);
  put 'second datepart ' x= date9.;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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