SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Sally_Caffrey
Obsidian | Level 7

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
Sally_Caffrey
Obsidian | Level 7
That's exactly I want. Thanks! BTW, when I try to create numeric variables from stdtc by using input(stdtc, E8601DT.), the log reports "Invalid argument to function INPUT".

The SAS help shows that B8601DT. writes datetime values in the ISO 8601 basic notation yyyymmddThhmmssffffff. , while E8601DT. in extended notation yyyy-mm-ddThh:mm:ss.ffffff.

Why E8601DT. reports the note ?
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ballardw
Super User

@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.

 

PaigeMiller
Diamond | Level 26

@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!!

--
Paige Miller

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 2044 views
  • 2 likes
  • 3 in conversation