Hi Community,
I am trying to work on adding a datetime variable to adjust for a time zone.
DateTime Zone
31OCT19:18:08:00 +02
05NOV19:06:00:00 +02
05NOV19:12:15:00 -05
08NOV19:20:30:00 +02
13NOV19:12:52:00 +08
15NOV19:17:44:00 +02
18NOV19:14:33:00 -05
20NOV19:06:21:00 +02
20NOV19:14:22:00 +08
22NOV19:14:25:00 +02
The issue is that the observations already have the arithmetic operators in the values. I need help in adding the Zone variable(Hours) to the Datetime variable.
Any help is appreciated.
Thank you.
Please show what you expect the value results to be.
I might guess that you want something similar to this assuming your current datetime variable is actually a SAS datetime value, numeric with a datetime format assigned.
data have; input DateTime datetime. Zone $; format datetime datetime16.; datalines; 31OCT19:18:08:00 +02 05NOV19:06:00:00 +02 05NOV19:12:15:00 -05 08NOV19:20:30:00 +02 13NOV19:12:52:00 +08 15NOV19:17:44:00 +02 18NOV19:14:33:00 -05 20NOV19:06:21:00 +02 20NOV19:14:22:00 +08 22NOV19:14:25:00 +02 ; data want; set have; datetime = intnx('hour',datetime,input(zone,best.),'s'); run;
The INTNX function increments time, date or datetime values by a given interval, 'hour' in this case. The Input turns the zone into a numeric value assuming hours, and the 'S' parameter means "same" for keeping minutes.
is zone numeric or character?
data have;
input DateTime :datetime20. Zone $;
format datetime datetime20.;
cards;
31OCT19:18:08:00 +02
05NOV19:06:00:00 +02
05NOV19:12:15:00 -05
08NOV19:20:30:00 +02
13NOV19:12:52:00 +08
15NOV19:17:44:00 +02
18NOV19:14:33:00 -05
20NOV19:06:21:00 +02
20NOV19:14:22:00 +08
22NOV19:14:25:00 +02
;
data want;
set have;
k=sign(input(zone,8.));
New_DateTime=sum(datetime,k,hms(abs(k),0,0));
format New_datetime datetime20.;
drop k;
run;
Hi @shasank I overlooked the data by mistake and so I have edited
data want;
set test;
length Zone $3.;
Zone = substr(DF_datetime,max(1,length(DF_datetime)-2));
k = sign(input(Zone,8.));
DF_deldttm =sum(DF_deldttm,a,hms(abs(k),0,0));
format DF_deldttm datetime.;
run;
Output:
Date/time of delivery, DF Zone a Datetime
31OCT19:18:08:00 2 1 31OCT19:19:08:01
05NOV19:06:00:00 2 1 05NOV19:07:00:01
05NOV19:12:15:00 -5 -1 05NOV19:13:14:59
08NOV19:20:30:00 2 1 08NOV19:21:30:01
13NOV19:12:52:00 8 1 13NOV19:13:52:01
15NOV19:17:44:00 2 1 15NOV19:18:44:01
18NOV19:14:33:00 -5 -1 18NOV19:15:32:59
20NOV19:06:21:00 2 1 20NOV19:07:21:01
20NOV19:14:22:00 8 1 20NOV19:15:22:01
22NOV19:14:25:00 2 1 22NOV19:15:25:01
The zone variable is going off.
Please show what you expect the value results to be.
I might guess that you want something similar to this assuming your current datetime variable is actually a SAS datetime value, numeric with a datetime format assigned.
data have; input DateTime datetime. Zone $; format datetime datetime16.; datalines; 31OCT19:18:08:00 +02 05NOV19:06:00:00 +02 05NOV19:12:15:00 -05 08NOV19:20:30:00 +02 13NOV19:12:52:00 +08 15NOV19:17:44:00 +02 18NOV19:14:33:00 -05 20NOV19:06:21:00 +02 20NOV19:14:22:00 +08 22NOV19:14:25:00 +02 ; data want; set have; datetime = intnx('hour',datetime,input(zone,best.),'s'); run;
The INTNX function increments time, date or datetime values by a given interval, 'hour' in this case. The Input turns the zone into a numeric value assuming hours, and the 'S' parameter means "same" for keeping minutes.
The normal numeric informat will convert those strings into numbers just fine. So this will create a numeric variable OFFSET with the number of hours (positive or negative).
offset=input(zone,32.);
You could then use INTNX() function or simple arithmetic to add the offset value to your current datetime value.
universal_dt = intnx('hour',datetime,offset,'s');
universal_dt = datetime + offset*'01:00't ;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.