BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shasank
Quartz | Level 8

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

 

 

 

View solution in original post

8 REPLIES 8
novinosrin
Tourmaline | Level 20

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

shasank
Quartz | Level 8
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. 

novinosrin
Tourmaline | Level 20

Take the solution given by @Tom  or @ballardw 

ballardw
Super User

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.

 

 

 

 

Peter_C
Rhodochrosite | Level 12
Have a look at how SAS supports adjusting for timezones at documentation.sas.com .... programming in sas9.4
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=nlsref&docsetTarget=p12pv...
Tom
Super User Tom
Super User

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 ;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 1734 views
  • 0 likes
  • 6 in conversation