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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.