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.
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;
data want;
datetime='24MAR2008:00:00:00'dt;
date=datepart(datetime);
format date date9. datetime datetime19.;
run;
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.