Do your actual values have a time component? I ask because sometimes we have uses of "datetime" values that are all 00:00:00 for time and the problem is a tad bit easier if we don't have to actually process a time component.
DT below has the desired datetime value.
data example; string='31.12.1970 00:00:00'; date = input(scan(string,1,' '),ddmmyy10.); dt = dhms(date,0,0,0); format date date9.; run;
If there is actual time value
data example2; string='31.12.1970 01:12:45'; date = input(scan(string,1,' '),ddmmyy10.); time = input(scan(string,2,' '),time.); dt = dhms(date,0,0,time); format date date9. time time8.; run;
The DHMS function takes a date, hour, minute and second value to create a datetime. So if the "time" is actually 0 hours, 0 minutes and 0 seconds we can use those and not bother to actually read the time component.
If a value is already a time value placing it in the seconds position suffices to provide the hours and minutes but you do need the 0 for the hour and minute.
If your SAS variable is not formatted as a datetime, the ACCESS module you use to connect to the database does not know that the target should be of type datetime. Apply an appropriate format in SAS.
Someone on here will have a slick way to do this, but crudely, you could do this:
data test;
dtstring="31.12.1970 00:00:00";
dtpart=scan(dtstring,1,' ');
timepart=scan(dtstring,2,' ');
dt_time=dhms(
mdy(scan(dtpart,2,'.')*1, scan(dtpart,1,'.')*1, scan(dtpart,3,'.')*1), /* this is the date*/
scan(timepart,1,':')*1, /* hour */
scan(timepart,2,':')*1, /* minute */
scan(timepart,3,':')*1 /* second */
);
format dt_time datetime.;
drop dtpart timepart;
run;
The input function only reads what it needs.
data EXAMPLE;
STR = '31.12.1970 00:00:00';
DATE = input(STRING, ddmmyy10.);
putlog DATE= date9.;
run;
DATE=31DEC1970
options datestyle=dmy;
data EXAMPLE;
STRING = '31.12.1970 00:00:00';
DATE = input(STRING, anydtdtm32.);
format date e8601dt.;
run;
proc print;run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.