BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
buddha_d
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

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.

buddha_d
Pyrite | Level 9
Thanks for the help Tom
ChrisHemedinger
Community Manager

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?

 

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
buddha_d
Pyrite | Level 9

ChrisHemedinger , Your code works too. Thank you for all your help. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1199 views
  • 3 likes
  • 4 in conversation