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.
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!
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.
Ready to level-up your skills? Choose your own adventure.