Hi,
I have two columns like below, STDTC and ENDTC in string format ($100). I want to calculate the time difference between them. I tried to convert stdtc to datetime format but failed bacause only one row with time and other rows are date. Seems it cannot make this column to be both date and datetime. How to calculte ?
STDTC ENDTC
2022-01-03 2022-01-03
2022-03-22 2022-05-02
2022-06-02T10:30 2022-06-04
Thanks for your help!
You have to create new numeric variables from the character variables in order to work with these.
You show five dates, and one date/time ... exactly what result do you want here anyway?
Maybe this?
data have;
input STDTC $16. ENDTC :$16.;
cards;
2022-01-03 2022-01-03
2022-03-22 2022-05-02
2022-06-02T10:30 2022-06-04
;
data want;
set have;
stdtn=input(stdtc,B8601DT.);
endtn=input(endtc,B8601DT.);
delta=endtn-stdtn;
format delta time16.;
run;
You have to create new numeric variables from the character variables in order to work with these.
You show five dates, and one date/time ... exactly what result do you want here anyway?
Maybe this?
data have;
input STDTC $16. ENDTC :$16.;
cards;
2022-01-03 2022-01-03
2022-03-22 2022-05-02
2022-06-02T10:30 2022-06-04
;
data want;
set have;
stdtn=input(stdtc,B8601DT.);
endtn=input(endtc,B8601DT.);
delta=endtn-stdtn;
format delta time16.;
run;
This isn't a very good answer ... but B8601DT seems happy when the value is just a date and no time component, while E8601DT seems to only work when there is a time component. However, I don't see where the documentation says that.
@PaigeMiller wrote:
This isn't a very good answer ... but B8601DT seems happy when the value is just a date and no time component, while E8601DT seems to only work when there is a time component. However, I don't see where the documentation says that.
The difference in the documentation is that B8601dt includes:
If the hour, minute, or second values are omitted, SAS uses a value of 0 for the hour, minute, or second.
but E8601dt does not have a similar piece in the description.
@ballardw wrote:
@PaigeMiller wrote:
This isn't a very good answer ... but B8601DT seems happy when the value is just a date and no time component, while E8601DT seems to only work when there is a time component. However, I don't see where the documentation says that.
The difference in the documentation is that B8601dt includes:
If the hour, minute, or second values are omitted, SAS uses a value of 0 for the hour, minute, or second.but E8601dt does not have a similar piece in the description.
That answers the question!!
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.