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;
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.