DATA Step, Macro, Functions and more

How to Change a chracter in DateTime

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

How to Change a chracter in DateTime

Hi,

I want to change one character or replace one character 'T' to ':' in datetime variable using SAS. If it is just few observations then I can use if-then-else statements but if there are thousands of records and they have different time values, how can I change it.

e.g. I have a time value as

'2017-12-01T12:43:28-05:00' and I have to replace T with (colon Smiley Happy '2017-12-01:12:43:28-05:00'

 

if date_time = '2017-12-01T12:43:28-05:00' then date_time = '2017-12-01:12:43:28-05:00';

It works with if-then-else to replace T with colon but I have different dates and times and I have thousands of records to do it.

 

Any help is greatly appreciated.


Accepted Solutions
Solution
‎12-04-2017 11:54 AM
Super User
Posts: 23,778

Re: How to Change a chracter in DateTime

Posted in reply to Malathi13

Use SUBSTR - RIGHT option to replace the T

Or use INPUT to convert it directly to a SAS datetime and then you can apply whatever format you want afterwards.

 

data demo;

have='2017-12-01T12:43:28-05:00' ;
z=input(have, anydtdtm.); *Convert to SAS datetime;
format z datetime.;

have_char = have;
substr(have_char, 11, 1) = ':'; *replace character if desired;

run;

proc print data=demo;
run;

View solution in original post


All Replies
Super User
Super User
Posts: 9,599

Re: How to Change a chracter in DateTime

Posted in reply to Malathi13

So you have a character field which contains ISO dates and you want to change T to a colon.  Is this what you are asking?  If so then you can use the tranwrd() function.  However, that being said, it really isn't a good idea to do this.  ISO dates are standard datetime format character data, and removing the T makes them no longer a compatible data item.  So the question is why do you want to do this?  Is it because you want to have a date, date/time numeric SAS variable?  If so then read them in using the appropriate format, e8601dt generally works for me.  Need more info really, but really don't change T to : for no apparent reason.

Solution
‎12-04-2017 11:54 AM
Super User
Posts: 23,778

Re: How to Change a chracter in DateTime

Posted in reply to Malathi13

Use SUBSTR - RIGHT option to replace the T

Or use INPUT to convert it directly to a SAS datetime and then you can apply whatever format you want afterwards.

 

data demo;

have='2017-12-01T12:43:28-05:00' ;
z=input(have, anydtdtm.); *Convert to SAS datetime;
format z datetime.;

have_char = have;
substr(have_char, 11, 1) = ':'; *replace character if desired;

run;

proc print data=demo;
run;

Frequent Contributor
Posts: 76

Re: How to Change a chracter in DateTime

Hi Reeza,

thank you so much for the help, I got the correct one with the substr function.

 

Thanks

M

Frequent Contributor
Posts: 76

Re: How to Change a chracter in DateTime

As a follow-up to my previous question, I'm also trying to calculate the difference (in minutes, seconds) from one time variable with other.

 

e.g. difference in

'2017-12-01T12:47:19-05:00' and

'2017-12-01T12:54:36-05:00'. I'm not sure if I have to use INTCK or any other functions or just (time2-time1).

 

Can someone suggest me on this?

 

Thanks

M

 

Super User
Posts: 23,778

Re: How to Change a chracter in DateTime

Posted in reply to Malathi13

Malathi13 wrote:

As a follow-up to my previous question, I'm also trying to calculate the difference (in minutes, seconds) from one time variable with other.

 

e.g. difference in

'2017-12-01T12:47:19-05:00' and

'2017-12-01T12:54:36-05:00'. I'm not sure if I have to use INTCK or any other functions or just (time2-time1).

 

Can someone suggest me on this?

 

Thanks

M

 


You can subtract datetimes to get the difference in seconds. And then you can convert that to the units of your choice. 

You can also use INTCK.

SAS Super FREQ
Posts: 825

Re: How to Change a chracter in DateTime

[ Edited ]
Posted in reply to Malathi13

Noticed, that you have a time zone offset in your datetime value.

 

Have a look at the doc, Reading Dates and Times By Using the ISO 860 Basic and Extended Notations there are specific informats that will make use of the time zone offset information.

 

Here is an example:

data demo;
  have='2017-12-01T12:43:28-05:00';
  dt1 = input(have, anydtdtm.);
  dt2 = input(have, E8601DZ.);

  format dt: datetime19.;
run;

proc print data=demo;
run;
Frequent Contributor
Posts: 76

Re: How to Change a chracter in DateTime

Posted in reply to Bruno_SAS

Thank you Bruno_sas.

Frequent Contributor
Posts: 76

Re: How to Change a chracter in DateTime

Posted in reply to Bruno_SAS

Hi Bruno_sas,

your code worked and gives the +5 time zone instead of -5. e.g. for  01DEC2017:12:43:28, it gave me  01DEC2017:17:43:28. I think this is +5. I'm not sure how to work with time zones yet, maybe you are correct. I need to learn about time zones. But I really appreciate you giving me the code to work with the time zones.

 

Thanks a lot

M

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 176 views
  • 2 likes
  • 4 in conversation