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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 4 replies
  • 481 views
  • 1 like
  • 4 in conversation