Hello all,
I am new to SAS and facing an issue in combining a datetime type date [XEDDAT] (e.g. 07JUN15:00:00:00) with a datetime type time [XEDTIM] (01JAN01:14:15:00) into a long date like "07JUN2015:14:15:00" that I can use to calculate time interval.
But when I wrote
newday = XEDDAT + XEDTIM;
format newday datetime20.;
the result date ended as "07JUN2056:14:15:00" instead of "07JUN2015:14:15:00".
This might be caused by the SAS cutoff date? any help would be greatly appreciated.
Urgently waiting and thank you so much for your reply!!!
zimcom
@zimcom Your approach is partially right but you should extract the time component from your second date variable before you add that to your other datetime var
data want;
k='07JUN15:00:00:00'dt;
k2='01JAN01:14:15:00'dt;
k3=k+timepart(k2);
format k: datetime20.;
run;
@zimcom Your approach is partially right but you should extract the time component from your second date variable before you add that to your other datetime var
Hi @zimcom You are welcome. Have a great weekend!
You can definitely add a TIME value to a DATETIME value to calculate a new DATETIME value. Both values are counts of seconds. A TIME value is the number of seconds since midnight. A DATETIME values is the number of seconds since midnight of 1960.
But your second variable is NOT a time value. Time values are not associated with any particular calendar day.
You can use the TIMEPART() function to extract just the time of day part of the value (and throw away the calendar day part). You can also use the DATEPART() function to generate a DATE value (number of days since 1960) from a DATETIME value.
data test;
XEDDAT = '07JUN2015:00:00:00'dt ;
XEDTIM = '01JAN2001:14:15:00'dt ;
XED_DATE = datepart(xeddat);
XED_TIME = timepart(xedtim);
XED_DATETIME = dhms(xed_date,0,0,xed_time);
XED_DATETIME2 = xeddat + timepart(xedtim);
format xeddat xedtim xed_datetime: datetime20. xed_date date9. xed_time time8. ;
put / 'FORMATTED VALUES' / (_all_) (=/);
put / 'RAW VALUES' / (_all_) (=comma32./);
run;
FORMATTED VALUES XEDDAT=07JUN2015:00:00:00 XEDTIM=01JAN2001:14:15:00 XED_DATE=07JUN2015 XED_TIME=14:15:00 XED_DATETIME=07JUN2015:14:15:00 XED_DATETIME2=07JUN2015:14:15:00 RAW VALUES XEDDAT=1,749,254,400 XEDTIM=1,293,977,700 XED_DATE=20,246 XED_TIME=51,300 XED_DATETIME=1,749,305,700 XED_DATETIME2=1,749,305,700
Hi Tom,
I really appreciated your detailed explanation and help.
Thank you so much!
zimcom
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.