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 🙂 '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.
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;
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.
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;
Hi Reeza,
thank you so much for the help, I got the correct one with the substr function.
Thanks
M
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
@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.
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;
Thank you 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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.