BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
zimcom
Pyrite | Level 9

 

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 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

@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

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20
data want;
k='07JUN15:00:00:00'dt;
k2='01JAN01:14:15:00'dt;
k3=k+timepart(k2);
format k: datetime20.;
run;
novinosrin
Tourmaline | Level 20

@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

zimcom
Pyrite | Level 9
I really appreciated your help.
Thank you so much!
novinosrin
Tourmaline | Level 20

Hi @zimcom You are welcome. Have a great weekend!

Tom
Super User Tom
Super User

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

 

 

 

zimcom
Pyrite | Level 9

Hi Tom,

 

I really appreciated your detailed explanation and help.

Thank you so much!

 

zimcom

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2079 views
  • 1 like
  • 3 in conversation