BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

How to convert string variable startdate like 2023-10-02 14:44:07 into a datetime format

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

View solution in original post

4 REPLIES 4
Astounding
PROC Star

Does this string represent October 2 or February 10, or do you want SAS to make that choice for you?

PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
Rick_SAS
SAS Super FREQ

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1612 views
  • 1 like
  • 4 in conversation