How to convert string variable startdate like 2023-10-02 14:44:07 into a datetime format
assuming the date time strings have consistent formatting
data have;
dt='2023-10-02 14:44:07';
run;
data want;
set have;
dt_num=input(dt,YMDDTTM.);
format dt_num datetime.;
run;
Does this string represent October 2 or February 10, or do you want SAS to make that choice for you?
october 02
assuming the date time strings have consistent formatting
data have;
dt='2023-10-02 14:44:07';
run;
data want;
set have;
dt_num=input(dt,YMDDTTM.);
format dt_num datetime.;
run;
if you want to read it directly into a SAS datetime variable, you can avoid reading the data as a string and then creating a second numerical variable. Here's how to read data directly. If you are not familiar with the ANYDTTMw. informat, you can read about the ANY* informats at https://blogs.sas.com/content/iml/2016/11/11/anydtdte-informat-read-any-date-sas.html
data have;
infile datalines dlm=',' dsd truncover; /* comma-separated input */
informat dt anydtdtm.; /* read directly into datetime; default width=19 */
format dt datetime.; /* format as datetime, too! */
input dt amount;
datalines;
2020-10-20 4:32:10, 1234.56
2023-10-02 14:44:07, 54321.00
2023-12-31 23:59:59, 876.54
;
proc print; run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.