Dear SAS community,
I am trying to fix datetime field to convert from GMT time zone to CST time zone. My code doesn't correct for Day light saving time. The daylight savings time will be -5 hrs. Other times it is -6 hrs from GMT time zone. My code does -6 hrs and doesn't take into the daylight savings time. Any help with code is much appreciated.
data HAVE;
infile datalines;
input START_DATE datetime26.6;
format START_DATE DATETIME26.6;
datalines;
28JAN2024:21:58:06.423000
06FEB2024:21:23:46.697000
16MAR2024:21:23:45.950000
28APR2024:21:58:03.737000
06MAY2024:21:23:44.590000
28JUN2024:21:58:02.457000
16JUL2024:19:30:09.413000
06AUG2024:21:23:40.557000
16SEP2024:19:30:08.810000
19OCT2024:00:30:08.810000
16NOV2024:05:30:08.810000
06DEC2024:03:23:38.137000
;
run;
data WANTED;
set HAVE;
cst_datetime = START_DATE + tzoneoff('CST');
FORMAT cst_datetime DATETIME26.6;
TIME_DIFF=START_DATE-cst_datetime;
FORMAT TIME_DIFF TIME8.;
run;
Thanks
Use TZONEU2S
data want;
set have;
central_time = tzoneu2s(start_date,'America/Chicago');
format central_time datetime26.6 ;
run;
PS: Do NOT include decimal places in an INFORMAT unless you know that the decimal point was purposely removed from the text to save one byte of storage.
input START_DATE datetime26.;
You are just asking to have your values divided by that power of ten. Might not matter for datetime values but definitely can cause unwanted surprises for normal numbers.
Use TZONEU2S
data want;
set have;
central_time = tzoneu2s(start_date,'America/Chicago');
format central_time datetime26.6 ;
run;
PS: Do NOT include decimal places in an INFORMAT unless you know that the decimal point was purposely removed from the text to save one byte of storage.
input START_DATE datetime26.;
You are just asking to have your values divided by that power of ten. Might not matter for datetime values but definitely can cause unwanted surprises for normal numbers.
I think you want to do 2 things. First, specify the target timezone as a region, not a standard. 'America/Chicago' is for Central.
Then also specify the base time in your TZONEOFF function, so the date is taken into account.
data WANTED;
set HAVE;
cst_datetime = START_DATE + tzoneoff('America/Chicago',START_DATE);
FORMAT cst_datetime DATETIME26.6;
TIME_DIFF=START_DATE-cst_datetime;
FORMAT TIME_DIFF TIME8.;
run;
Edit: @Tom beat me to it with a similar but different answer. More than one way to do this...what could be more SAS than that?
@ChrisHemedinger wrote:
Then also specify the base time in your TZONEOFF function, so the date is taken into account.
Every day I learn something new in the SAS Communities.
ChrisHemedinger , Your code works too. Thank you for all your help.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.