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

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!

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.

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