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

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

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