Desktop productivity for business analysts and programmers

Convert character variable to DateTime

Reply
Frequent Contributor
Posts: 104

Convert character variable to DateTime

Hi all, I want to convert StartTime and EndTime which are character variable to datetime variable so I can do calculation.  Please see our they are structured in my data source.

Capture.PNG

For example, the first row, I want my calculation to say that customer  started at 11:00 AM and ended 11:00 AM.  You are welcome to offer me another option of getting my desire calculation without any conversion.

Thank you ahead.

SAS Super FREQ
Posts: 706

Re: Convert character variable to DateTime

You can convert these type of text strings using the ANYDTDTM informat. Unfortunately it will not read the milliseconds. In the code below two methods are shown, one using the ANYDTDTM informat, the other uses a combination of two informats for the date and time part and then combine the date and seconds together using the DHMS function.

data want;
  dateHave = "2015.03.03 11:19:41.440";

  dateWant = input(dateHave,
anydtdtm19.);

  dateWant2 = dhms(
    input(substr(dateHave,
1, 10), yymmdd10.)
    ,
0
    ,
0
    , input(substr(dateHave,
12), time16.)
  );

 
format dateWant: datetime22.3;
run;
Frequent Contributor
Posts: 104

Re: Convert character variable to DateTime

Thank you, it was helpful.

Ask a Question
Discussion stats
  • 2 replies
  • 306 views
  • 3 likes
  • 2 in conversation