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

I have been trying to convert a string variable that consists of date and time to a SAS datetime format. 

Can you recommend a method?

 

DATA test;

      input date $16.;

      datalines;

      202203020719

      202203030922

      202203070757

;

RUN;

 

DATA test2;

      SET test;

      new_date = put(input(substr(date, 1, 8), yymmdd8.), date9.);

      new_time = substr(date, 9, 4);

      time_numeric = input(substr(new_time,1,2) !! ':' !! substr(new_time,3,2),time5.);

 

      hr = input(substr(new_time,1,2),2.) ;

      min = input(substr(new_time,3,2),2.) ;

      sec = 00;

      *sasdt = dhms(new_date, hr, min, sec); /*This line of code does not work*/

run;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Use the proper informat and the INPUT function. There are several informats that might work including B8601DJ 

 

data want;
    set test;
    num_date_time=input(date,b8601dj12.);
    format num_date_time datetime16.;
run;

 

Hint: Always use built in SAS functions (in this case INPUT with the proper informat) to work with dates and date/times. Don't try to create your own.

 

Hint 2: Date/time values should not have a variable name of DATE which indicates it is a date value and not a date time value. 

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26

Use the proper informat and the INPUT function. There are several informats that might work including B8601DJ 

 

data want;
    set test;
    num_date_time=input(date,b8601dj12.);
    format num_date_time datetime16.;
run;

 

Hint: Always use built in SAS functions (in this case INPUT with the proper informat) to work with dates and date/times. Don't try to create your own.

 

Hint 2: Date/time values should not have a variable name of DATE which indicates it is a date value and not a date time value. 

--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 1 reply
  • 2163 views
  • 1 like
  • 2 in conversation