I'm trying to get the hang of working with SAS timezones. Bottom of Page 42, SAS NLS: Reference, 5th Ed. states:
"When you specify a time zone, SAS adds a time-zone-specific timestamp to data sets and SAS catalogs when the data set or catalog is created or modified".
The following calls the datetime() and tzones2u()
functions, which are influenced by the timezone option in effect:
NOTE: This session is executing on the X64_10PRO platform.
1 %let dt_fmt = dateampm.;
2
3 options tz='America/New_York';
4 data ny;
5 ny_dt = datetime();
6 ny_2_utc = tzones2u(ny_dt);
7 *ny_tz_off = cat(put(tzoneoff(ny_dt) / 3600, 8.), ':00');
8 run;
NOTE: The data set WORK.NY has 1 observations and 2 variables.
9
10 options tz='Asia/Shanghai';
11 data sha;
12 sha_dt = datetime();
13 sha_2_utc = tzones2u(sha_dt);
14 *sha_tz_off = cat(put(tzoneoff(sha_dt) / 3600, 8.), ':00');;
15 run;
NOTE: The data set WORK.SHA has 1 observations and 2 variables.
16
17 options tz='America/New_York';
18 data both;
19 merge ny
20 sha;
21 diff = intck('hour', ny_dt, sha_dt, 'Continuous');
22
23 put 'New York Datetime: ' ny_dt &dt_fmt /
24 'New York 2 UTC: ' ny_2_utc &dt_fmt //
25 'Shanghai Datetime: ' sha_dt &dt_fmt /
26 'Shanghai 2 UTC: ' sha_2_utc &dt_fmt //
27 'Time Difference NY and SHA: ' diff ' Hours';
28 run;
New York Datetime: 04FEB19:02:10:23 PM
New York 2 UTC: 04FEB19:07:10:23 PM
Shanghai Datetime: 05FEB19:03:10:23 AM
Shanghai 2 UTC: 04FEB19:07:10:23 PM
Time Difference NY and SHA: 13 Hours
29
30 proc contents data = ny;run;
31 proc contents data = sha;run;
NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
The output from PROC CONTENTS does not appear to display such an attribute.
The CONTENTS Procedure
Data Set Name WORK.NY Observations 1
Member Type DATA Variables 2
Engine V9 Indexes 0
Created 02/04/2019 14:10:23 Observation Length 16
Last Modified 02/04/2019 14:10:23 Deleted Observations 0
Protection Compressed NO
Data Set Type Sorted NO
Label
Data Representation WINDOWS_64
Encoding wlatin1 Western (Windows)
Engine/Host Dependent Information
Data Set Page Size 65536
Number of Data Set Pages 1
First Data Page 1
Max Obs per Page 4062
Obs in First Data Page 1
Number of Data Set Repairs 0
ExtendObsCounter YES
Filename C:\Users\randy\AppData\Local\Temp\SAS Temporary
Files\_TD9972_RANDY-PC_\ny.sas7bdat
Release Created 9.0401M5
Host Created X64_10PRO
Owner Name randy-PC\randy
File Size 128KB
File Size (bytes) 131072
Alphabetic List of Variables and Attributes
# Variable Type Len
2 ny_2_utc Num 8
1 ny_dt Num 8
The CONTENTS Procedure
Data Set Name WORK.SHA Observations 1
Member Type DATA Variables 2
Engine V9 Indexes 0
Created 02/04/2019 14:10:23 Observation Length 16
Last Modified 02/04/2019 14:10:23 Deleted Observations 0
Protection Compressed NO
Data Set Type Sorted NO
Label
Data Representation WINDOWS_64
Encoding wlatin1 Western (Windows)
Engine/Host Dependent Information
Data Set Page Size 65536
Number of Data Set Pages 1
First Data Page 1
Max Obs per Page 4062
Obs in First Data Page 1
Number of Data Set Repairs 0
ExtendObsCounter YES
Filename C:\Users\randy\AppData\Local\Temp\SAS Temporary
Files\_TD9972_RANDY-PC_\sha.sas7bdat
Release Created 9.0401M5
Host Created X64_10PRO
Owner Name randy-PC\randy
File Size 128KB
File Size (bytes) 131072
Alphabetic List of Variables and Attributes
# Variable Type Len
2 sha_2_utc Num 8
1 sha_dt Num 8
If someone supplies a dataset with datetime values created with a SAS instance where the timezone in effect is unknown, then how would I convert the incoming datatimes to a timezone other than the timezone in effect in this current instance of SAS.
Thanks in advance,
Randy
Very interesting question.
I think there is not flag or "stamp" or attribute set or added anywhere. So this can be a misread:
"When you specify a time zone, SAS adds a time-zone-specific timestamp to data sets and SAS catalogs when the data set or catalog is created or modified".
What happens as far as I understand is that if time zones are known in the SAS session, SAS uses that information to shift the datetime values. This shift takes place both when storing values (as you have shown) and when retrieving them (as @PGStats showed).
I think the dataset timestamp is UTC and reported by proc contents according to your locale, as demonstrated by:
options tz='America/New_York';
data ny;
Hello = "World";
run;
proc contents data = ny; run;
options tz='Asia/Shanghai';
proc contents data = ny; run;
The CONTENTS Procedure Data Set Name WORK.NY Observations 1 Member Type DATA Variables 1 Engine V9 Indexes 0 Created 2019-02-04 17:01:13 Observation Length 5 Last Modified 2019-02-04 17:01:13 Deleted Observations 0 . . . The CONTENTS Procedure Data Set Name WORK.NY Observations 1 Member Type DATA Variables 1 Engine V9 Indexes 0 Created 2019-02-05 06:01:13 Observation Length 5 Last Modified 2019-02-05 06:01:13 Deleted Observations 0 ...
Thanks to both @PGStats and @ChrisNZ for the helpful responses. And I verified it with:
4 options tz='America/New_York';
5 data both;
6 merge ny
7 sha;
8 diff = intck('hour', ny_dt, sha_dt, 'Continuous');
9
10 sha_tzone_off_now = cat(put(tzoneoff(sha_dt) / 3600, 8.), ':00');
11
12 put 'New York Datetime: ' ny_dt &dt_fmt /
13 'New York 2 UTC: ' ny_2_utc &dt_fmt //
14 'Shanghai Datetime: ' sha_dt &dt_fmt /
15 'Sha TZ offset then: ' sha_tz_off_then /
16 'Sha TZ offset now: ' sha_tz_off_then //
17 'Shanghai 2 UTC: ' sha_2_utc &dt_fmt /
18 'Time Difference NY and SHA: ' diff ' Hours';
19 run;
New York Datetime: 04FEB19:05:16:44 PM
New York 2 UTC: 04FEB19:10:16:44 PM
Shanghai Datetime: 05FEB19:06:16:44 AM
Sha TZ offset then: 8:00
Sha TZ offset now: 8:00
Shanghai 2 UTC: 04FEB19:10:16:44 PM
Time Difference NY and SHA: 13 Hours
I reported this documentation error.
The value is shifted. No “time-zone-specific timestamp” is “added”.
Good news. The documentation is being revised.
From:
When you specify a time zone, SAS adds a time-zone-specific timestamp to data sets and SAS catalogs when the data set or catalog is created or modified.
The time-zone-specific timestamp is also added to the SAS log when SAS starts and to the output at execution.
To:
When you specify a time zone, SAS shifts the datetime value according to the time zone and stores the new value.
The time-zone-specific timestamp is displayed to the SAS log.
@ChrisNZ, thank you for the follow-up!
Very interesting question.
I think there is not flag or "stamp" or attribute set or added anywhere. So this can be a misread:
"When you specify a time zone, SAS adds a time-zone-specific timestamp to data sets and SAS catalogs when the data set or catalog is created or modified".
What happens as far as I understand is that if time zones are known in the SAS session, SAS uses that information to shift the datetime values. This shift takes place both when storing values (as you have shown) and when retrieving them (as @PGStats showed).
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.